首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

sql 中一个列是char类型设置为主键,自动增长 比如1,2,3.不要改变表的结构,怎么用sql语句实现

2012-05-31 
sql 中一个列是char类型设置为主键,自动增长 比如1,2,3....,不要改变表的结构,如何用sql语句实现sql 中一

sql 中一个列是char类型设置为主键,自动增长 比如1,2,3....,不要改变表的结构,如何用sql语句实现
sql 中一个列是char类型设置为主键,自动增长 比如1,2,3....,不要改变表的结构,如何用sql语句实现

[解决办法]

SQL code
create   table   test(SYSID   varchar(10), -- varchar字段name   varchar(10)) create   trigger   t_insert   on   test INSTEAD   OF   insert as declare   @id   varchar(10),@id1   int select   *   into   #tb   from   inserted select   @id=max(SYSID)   from   test if   @id   is   null set   @id1=0 else set   @id1=@id update   #tb   set   @id1=@id1+1 ,SYSID= @id1insert   into   test   select   *   from   #tb drop   table   #tb go insert   into   test(name) select   'aa ' union   all   select   'bb ' union   all   select   'cc ' select   *   from   testSYSID      name---------- ----------1          aa 2          bb 3          cc (3 行受影响)
[解决办法]
挺有趣的题目,花了点时间,研究了一下。
instead of insert 触发器。
SQL code
--表create table test (id char(10) not null,value char(10) not null,constraint pk_test primary key (id) )--触发器create trigger tri_inserton test instead of insertasdeclare @id intif exists(select top 1 id from test)   select @id = max(convert(int,id))+1 from test   else   select @id=1INSERT INTO test      SELECT @id,value FROM inserted--测试insert into test (id,value) values ('','a')insert into test (id,value) values ('','b')select * from test 

热点排行