寫一條存儲過程,實現往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