notes/sql_server/mssql.md

39 lines
907 B
Markdown
Raw Normal View History

2023-01-16 10:47:48 -05:00
Error Handling
===============================================================
```
2023-01-12 20:12:24 -05:00
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
2023-01-16 10:47:48 -05:00
SELECT * FROM #temp
```