我有由其他存储过程调用的存储过程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