我有如下的sql语句:
Declare @Inserted_FileID as int
insert into EH_PP_TeacherEvalArtifacts (EH_PP_TEA_OriginalUploadDateTime,EH_PP_TEA_LastModidiedDateTime,EH_PP_TEA_FileVersionNum,EH_PP_TEA_FileName,EH_PP_TEA_FileDesc,EH_PP_TE_TeacherEvalID,EH_PP_TEA_UploadedBy)
OUTPUT INSERTED.EH_PP_TEA_FileID into @Inserted_FileID
values(getdate(),getdate(),@EH_PP_TEA_FileVersionNum,@EH_PP_TEA_FileName,@EH_PP_TEA_FileDesc,@EH_PP_TE_TeacherEvalID,@EH_PP_TEA_UploadedByAccountID)
on:
OUTPUT INSERTED.EH_PP_TEA_FileID into @Inserted_FileID
它向我显示错误:
必须声明表变量@Inserted_FileID
我只想将当前插入的fileid存储到@Inserted_FileID
整数变量中。
我该怎么做?
答案 0 :(得分:1)
如果@Inserted_FileID
是标识列,则使用
**SET Inserted_FileID = @@identity**
如果没有,请使用以下:
Declare @Inserted_FileID as int
DECLARE @TempTable TABLE (RowID int not null primary key identity(1,1),FILE_ID VARCHAR(4))
insert into EH_PP_TeacherEvalArtifacts
(EH_PP_TEA_OriginalUploadDateTime,
EH_PP_TEA_LastModidiedDateTime,
EH_PP_TEA_FileVersionNum,
EH_PP_TEA_FileName,
EH_PP_TEA_FileDesc,
EH_PP_TE_TeacherEvalID,
EH_PP_TEA_UploadedBy)
values
(getdate(),
getdate(),
@EH_PP_TEA_FileVersionNum,
@EH_PP_TEA_FileName,
@EH_PP_TEA_FileDesc,
@EH_PP_TE_TeacherEvalID,
@EH_PP_TEA_UploadedByAccountID)
INSERT INTO @TempTable
Select top 1 EH_PP_TEA_FileID
from EH_PP_TeacherEvalArtifacts
order by EH_PP_TEA_LastModidiedDateTime DESC
Select @Inserted_FileID = FILE_ID from @TempTable