notes/sql_server/error_handling.sql
Paul Trowbridge 2a6f1c5dcf redis
2020-01-12 21:28:28 -05:00

33 lines
850 B
Transact-SQL

CREATE PROC RLARP.TEST AS
BEGIN
PRINT 'Hi'; --non-erroring statement
create table #temp(x varchar(255)); --create a permanent object to call outside block after error
insert into #temp select 1/0;
insert into #temp select 'hi'; --fill it after error
--select * from #temp; --select it after error
PRINT ERROR_MESSAGE(); --error message is gone
END;
begin transaction x
declare @e int;
DECLARE @em varchar(max);
begin try
EXEC RLARP.TEST;
end TRY
begin CATCH
select @e = ERROR_NUMBER(), @em = ERROR_MESSAGE();
if @e <> 0
BEGIN
rollback transaction x;
print @em;
END
if @e = 0
BEGIN
commit transaction x;
print 'ok';
end
end catch
SELECT * FROM #temp