I have one stored procedure proc_in which the insert data to tbl table
create table tbl(id int identity, val nvarchar(50))
create procedure proc_in
as
begin
insert into tbl(val)
values ('test')
end
and I have proc_out where I call proc_in
create procedure proc_out
as
begin
exec proc_in
DECLARE @MessageText NVARCHAR(100);
SET @MessageText = N'This is a raiserror %s';
RAISERROR(@MessageText, 16, 1, N'MSG')
end
How I can write proc_out that it return raiserror always to do insert in TBL table.
I calling proc_out like this
begin tran
declare @err int = 0
exec @err = proc_out
if @ERR = 0
commit tran
else
rollback tran
You are wrapping your call in a single transaction in the calling context, therefore:
will always roll back everything that has happened within that transaction.
One way to avoid this is to move the transaction inside your 'proc_out' SP e.g.
Alternatively, and I haven't tried this, you could try using a
savepointe.g.Then call it as:
I don't like this approach though, because knowledge of the inner workings of your SP has now leaked out to the calling context.
And some errors will roll back the entire transaction regardless.
Its important to be aware of the
XACT_ABORTsetting here.