`
I_am_kevin
  • 浏览: 141748 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类
最新评论

SQL2K中实现列的自增

阅读更多
一:数据库的列为整形是,我们可以使用关键字Identity(1,1)实现

二:数据库的列为字符串varchar时,我们仍可实现自增长
create table test001
(
id nvarchar(10),
content varchar(10)
)

go
--创建触发器
create trigger tg_test001
on test001
instead of insert
as

declare @content nvarchar(10)

select @content= content from inserted
insert into test001(id,content)
select cast(isnull(max(id),'0') as int)+1,@content
from test001
 
go

--向表中插入数据
insert into test001 (content) select 'a'
insert into test001 (content) select 'b'
go
--选择察看插入效果
select * from test001
go

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics