可能重复:
How to get the primary key of the last row inserted into the table
我有两张桌子:
Downloaded(Id,MovieName,DownloadedBy)
Movie(ID,Rating,...)
Downloaded(id)
为identity (1,1)
,Movie(Id)
为Downloaded(id)
的外键
现在我需要同时插入两个程序
alter proc sp_Insert
@name varchar (50),
@year int,
@type varchar (50),
@rating int,
@available bit
AS
Insert into Downloaded (MovieName) values (@name)
declare @id int
select Id=@id
from Downloaded
where MovieName=@name
Insert into Movie values (@id,@year,@type,@rating,@available)
现在我的问题是从SELECT
@id
获取null
的{{1}}值,这意味着id
没有获取新的身份值。
那么为了使Downloaded(id)
获取@id
的标识值,该怎么做是有任何类型的块执行或我不知道可能交易
请耐心等待我,我还是新人