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