我有由其他存儲過程調用的存儲過程TSQL - 從SP返回值當SP不包含返回
ALTER PROCEDURE [dbo].[usp_Test]
AS
begin
declare @errorCode int
declare @lastIdentity int
select @errorCode = @@ERROR
if @errorCode=0
begin
update Vehicle set model='1996----------'
where Make='MERC'
select @errorCode = @@ERROR
select @lastIdentity = @@IDENTITY
end
print 'usp_test lastIdentity=' + convert(varchar(10), isnull(@lastIdentity,0))
print 'usp_test errorCode=' + convert(varchar(10), @errorCode)
end
如果我這樣調用
declare @RetVal int
exec @RetVal=usp_Test
print 'return value is ' + convert(varchar(10), @RetVal)
我的存儲過程得到以下消息
消息8152,級別16,狀態14,過程usp_Test,第14行 字符串或二進制數據將被截斷。 該聲明已被終止。 usp_test lastIdentity = 0 usp_test錯誤碼= 8152 返回值爲-6
通過添加在選擇@errorCode後結束,返回@errorCode返回0 ......我會返回錯誤的一個很好的清潔方法並隨後處理它。我很驚訝,沒有任何返回,我得到-6的返回值。任何人都可以解釋爲什麼這是事實嗎?
不使用@@ IDENTITY:use scope_identity()。99.999超時100%你想要scope_identity()。 – 2011-02-18 09:47:09
是的我同意,我正在開發一個項目,其中的代碼是由其他人編寫的。但是,這與我詢問的問題並不真正相關 – Andrew 2011-02-18 09:50:54
SQL Server存儲過程返回代碼奇怪](http://stackoverflow.com/questions/2965211/sql-server-stored-procedure-return-code-oddity)。哪些鏈接http://www.sommarskog.se/error-handling-I.html #returnvalue – gbn 2011-02-18 09:51:04