我有這樣的SQL Server存儲過程:SQL找回錯@@身份
CREATE procedure [dbo].[sp_FinancialInstitutionsAdd]
@Name varchar(50)
AS
set nocount on
begin tran
declare @retval int = 0
declare @rmdID int
execute @rmdID = dbo.sp_MetaDataRecsAdd
select @rmdID as rmdID
if @@ERROR <> 0
begin
rollback tran
return -1
end
insert FinancialInstitutions (Name, rmdID)
values (@Name, @rmdID)
if @@ERROR <> 0
begin
rollback tran
return -1
end
return scope_identity()
commit tran
我只能拿回插入MetaDataRecs
表的ID,但我想FinacialInstitutions
表的ID。該過程在SQL中直接執行時可以正常工作,但在執行代碼中的ExecuteScalar
命令時不會執行。
備註:您應該**不要**爲存儲過程使用'sp_'前綴。微軟已經保留了這個前綴以供自己使用(參見*命名存儲過程*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx),以及你將來有可能冒着名字衝突的風險。 [這對你的存儲過程性能也是不利的](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)。最好只是簡單地避免使用'sp_'並使用別的東西作爲前綴 - 或者根本沒有前綴! –