求条语句!简单的!
我有这样一个表
表1
pers_id pers_name
00000001 小红
00000002 小张
00000003 小华
这个时候我想插入这样的记录 00000004 小陈
就变成了下面这样
pers_id pers_name
00000001 小红
00000002 小张
00000003 小华
00000004 小陈
请问怎么实现
------解决方案--------------------create table ta
( CorpID char(5),
Name varchar(100))
create trigger test_ta on dbo.ta
after insert
as
declare @i int
select @i=isnull(max(CorpID),0) from ta
update ta
set CorpID=right( '100000 '+@i,5),@i=@i+1
where CorpID is null
insert into ta (name) select 'aa '
insert into ta (name) select 'bb '
select * from ta
CorpID Name
------ ----------------------------------------------------------------
00001 aa
00002 bb
(2 行受影响)
------解决方案--------------------create table tb(pers_id nvarchar(10),pers_name nvarchar(100))
insert into tb(pers_id,pers_name)
select '00000001 ', '小红 ' union
select '00000002 ', '小张 ' union
select '00000003 ', '小华 '
insert into tb(pers_id,pers_name)
select
right( '00000000 ' + cast((select max(cast(pers_id as int)) from tb) + 1 as nvarchar(10)),8), '小陈 '
select * from tb
drop table tb
------解决方案--------------------select col=right( '00000000 '+cast(isnull(max(cast(pers_id as int)),0)+1 as varchar),8) from tb
--返回值即为最大id,
------解决方案--------------------没看明白
不是普通的插入么
------解决方案--------------------借用上面的数据
create table tb(pers_id nvarchar(10),pers_name nvarchar(100))
insert into tb(pers_id,pers_name)
select '00000001 ', '小红 ' union
select '00000002 ', '小张 ' union
select '00000003 ', '小华 '
select * from tb
drop table tb
insert into tb
select right( '0000000 '+ convert(varchar(50),convert(int, max(pers_id))+1),8) , '小王 '
from tb