sql 中实现往表中插入一条记录并返回当前记录的ID


写一条存储过程,实现往User中插入一条记录并返回当前UserId(自增长id)

--推荐写法

if(Exists(select * from sys.objects where name=N'Usp_InsertedID'))

  drop proc Usp_InsertedID

go

create proc Usp_InsertedID

as

  insert into [User] output inserted.UserID values(N'张三蛋',3)

 

--另一种写法(SCOPE_IDENTITY()可以得到当前范围内最近插入行生成的标示值)

if(Exists(select * from sys.objects where name=N'Usp_InsertedID'))

    drop proc Usp_InsertedID

go

create proc Usp_InsertedID

as

    insert into [User] values(N'李狗蛋',1)

    select scope_Identity()

go


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM