ABC/0001ABC/002为什么是002而不是0002?
解决方案 »
- mssql,如何查询对数据库的操作日志
- 在SQL2000中,如何打开映射盘符上的1个DBF文件?
- 如何从SQL SERVER中复制一个表结构到ACESS?在线等!
- 在不联网的情况下,怎样才能把别的Sql server2000服务器上的数据库变为我本机Sql server2000服务器上的数据库
- SQL Update中CASE的用法问题
- 为什么SQL数据库查询非常慢!!!
- SQL Server 大批量插入导致PF高居不下,请指教
- dtsrun的格式该怎样写啊?
- 请问怎样用sql server中数据库生成结构相同的access文件?
- 我是不是有点狂妄?(散分100)
- insert into 能不能这样写
- SQL卸载后,重新安装失败(急,若嫌分不够尽管说)
insert 你的表 values('ABC/0001', 'ABC')
insert 你的表 values(null, 'ABC')
insert 你的表 values(null, 'ABC')
insert 你的表 values('AB/0002', 'AB')
insert 你的表 values(null, 'AB')
insert 你的表 values(null, 'AB')
declare @a varchar(100),@b varchar(100),@i int
update 你的表 set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from 你的表 tem where tem.style=你的表.style),0)+@i,4),@b=style,productno=@a
where ProductNo is null
select * from 你的表
go
drop table 你的表
declare @a varchar(100),@b varchar(100),@i intupdate 你的表 set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from 你的表 tem where tem.style=你的表.style),0)+@i,4),@b=style,productno=@a where ProductNo is null
每插入一条记录,首先根据 Style 的值查询前面所有具有相同 Style 的记录,找出其中 ProductionNo 序号最大的,在此基础上加 1这些最好写在触发器里面,这样,不管你一次插入多少条记录,都可以。
思路,
每插入一条记录,首先根据 Style 的值查询前面所有具有相同 Style 的记录,找出其中 ProductionNo 序号最大的,在此基础上加 1这些最好写在触发器里面,这样,不管你一次插入多少条记录,都可以。
那你就錯了
一次插入多條只會觸發一次大力的語句我可是分析了一兩個小時才明白
好蠢哦我
有一點小問題
這是你的答案
我稍微改了一下插入的順序create table 你的表 (ProductNo varchar(30),Style varchar(25))
insert 你的表 values(null, 'ABC')
insert 你的表 values(null, 'AB')
insert 你的表 values(null, 'ABC')
insert 你的表 values(null, 'AB')
insert 你的表 values(null, 'ABC')
declare @a varchar(100),@b varchar(100),@i int
update 你的表 set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from 你的表 tem where tem.style=你的表.style),0)+@i,4),@b=style,productno=@a
where ProductNo is null
select * from 你的表
go
drop table 你的表和插入順序有關
也和又沒有productNo種子有關這樣再看看
insert 你的表 values('ABC/0001', 'ABC')
insert 你的表 values('AB/0001', 'AB')
insert 你的表 values(null, 'ABC')
insert 你的表 values(null, 'AB')
insert 你的表 values(null, 'ABC')
萬望百忙之中再次關注一下這個問題
謝謝
productno都是null還有isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1)
這一句有點問題
應該改成isnull((select right(max(ProductNo),4)
要不然ProductNo太長的時候就有問題了.
insert #a (ProductNo,Style) values(null, 'ABC')
insert #a (ProductNo,Style) values(null, 'AB')
insert #a (ProductNo,Style) values(null, 'ABC')
insert #a (ProductNo,Style) values(null, 'AB')
insert #a (ProductNo,Style) values(null, 'ABC')select * into # from #a where ProductNo is null order by style,id
declare @a varchar(100),@b varchar(100),@i int
update # set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from # tem where tem.style=#.style),0)+@i,4),@b=style,productno=@aupdate #a set productno=#.productno from # where #a.id=#.idselect * from #a
go
drop table #a,#