求助:存储过程调用存储过程如何返回错误?
在存储过程aaa中 调用存储过程bbb 如何在执行bbb时候返回错误。
我采用 bbb在执行成功后 output参数是 '返回成功!'来判断,不知道是否可行?
CREATE PROCEDURE aaa
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if tagid=1
begin
--处理
..
..
..
--调用
declare @msg1 varchar(100)
exec bbb @dt,21,@msg1
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error
exec bbb @dt,22,@msg1
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error exec bbb @dt,23,@msg1
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error
...
endcommit
return 1on_error:
rollback
return -1
GOCREATE PROCEDURE bbb
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if tagid=21
begin
set @msg='出错情况1'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif tagid=22
begin
set @msg='出错情况2'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif tagid=23
begin
set @msg='出错情况3'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endcommit
return 1on_error:
rollback
return -1
GO
在存储过程aaa中 调用存储过程bbb 如何在执行bbb时候返回错误。
我采用 bbb在执行成功后 output参数是 '返回成功!'来判断,不知道是否可行?
CREATE PROCEDURE aaa
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if tagid=1
begin
--处理
..
..
..
--调用
declare @msg1 varchar(100)
exec bbb @dt,21,@msg1
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error
exec bbb @dt,22,@msg1
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error exec bbb @dt,23,@msg1
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error
...
endcommit
return 1on_error:
rollback
return -1
GOCREATE PROCEDURE bbb
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if tagid=21
begin
set @msg='出错情况1'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif tagid=22
begin
set @msg='出错情况2'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif tagid=23
begin
set @msg='出错情况3'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endcommit
return 1on_error:
rollback
return -1
GO
http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx
目前的问题是,
我在存储过程中有一个
IF exists (SELECT top 1 * from .....)
begin
SELECT TOP 1 @msg=字段+'不正确' FROM ....
goto on_error
end
在存储过程在运行到这里,因为检查出来有错误数据,所以 goto on_error 并且@msg信息就是错误数据的地方。但是如何在存储过程aaa调用bbb的时候 把这个错误获取出来呢?
修改后的!CREATE PROCEDURE aaa
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if @Tagid=1
begin
--处理
--调用
declare @msg1 varchar(100)
exec bbb @dt,21,@msg1 output
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error
exec bbb @dt,22,@msg1 output
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error exec bbb @dt,23,@msg1 output
set @msg=@msg1
If @msg<>'返回成功!'
goto on_errorendcommit
return 1on_error:
rollback
return -1
GOCREATE PROCEDURE bbb
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if @Tagid=21
begin
set @msg='出错情况1'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif @Tagid=22
begin
set @msg='出错情况2'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif @Tagid=23
begin
set @msg='出错情况3'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endcommit
return 1on_error:
rollback
return -1
GO
=============declare @msg varchar(100)
set @msg = ''
exec aaa '',1, @msg output
select @msg
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if @Tagid=1
begin
--处理
--调用
declare @msg1 varchar(100)
exec bbb @dt,21,@msg1 output
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error
exec bbb @dt,22,@msg1 output
set @msg=@msg1
If @msg<>'返回成功!'
goto on_error exec bbb @dt,23,@msg1 output
set @msg=@msg1
If @msg<>'返回成功!'
goto on_errorendcommit
return 1on_error:
rollback
return -1
GOCREATE PROCEDURE bbb
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if @Tagid=21
begin
set @msg='出错情况1'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif @Tagid=22
begin
set @msg='出错情况2'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endif @Tagid=23
begin
set @msg='出错情况3'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功!'
endcommit
return 1on_error:
rollback
return -1
GO
declare @msg varchar(100)
set @msg = ''
exec aaa '',1, @msg output
select @msg
=========
返回成功3!alter PROCEDURE aaa
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if @Tagid=1
begin
--处理
--调用
declare @msg1 varchar(100)
set @msg1 = ''
exec bbb @dt,21,@msg1 output
set @msg=@msg1
If @msg<>'返回成功1!'
goto on_error
exec bbb @dt,22,@msg1 output
set @msg=@msg1
If @msg<>'返回成功2!'
goto on_error exec bbb @dt,23,@msg1 output
set @msg=@msg1
If @msg<>'返回成功3!'
goto on_errorendcommit
return 1on_error:
rollback
return -1
GOalter PROCEDURE bbb
@dt datetime,
@Tagid int,
@msg as varchar(100) output
AS
Begin transaction if @Tagid=21
begin
set @msg='出错情况1'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功1!'
endif @Tagid=22
begin
set @msg='出错情况2'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功2!'
endif @Tagid=23
begin
set @msg='出错情况3'
--处理过程
If @@error<>0
goto on_error
SET @MSG='返回成功3!'
endcommit
return 1on_error:
rollback
return -1
GO
SQL2005以后改成TRY CATCH获取错误了,有几个系统函数处理错误信息。
begin
SELECT TOP 1 @msg=字段+'不正确' FROM .... 条件<>0
goto on_error
end 条件那里写错了,找到错误,但是赋值时条件又换成另外一个了 导致 @msg=null
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
-- The message text returned is: This is message number 5.
GO