2012年5月10日 星期四

ms-sql try catch 練丹

TRY...CATCH (Transact-SQL) 
--
--練丹1
begin try
  select 1/0
 
end try
begin catch
  select
  ERROR_NUMBER() AS ErrorNumber
  ,ERROR_SEVERITY() AS ErrorSeverity
  ,ERROR_STATE() AS ErrorState
  ,ERROR_PROCEDURE() AS ErrorProcedure
  ,ERROR_LINE() AS ErrorLine
  ,ERROR_MESSAGE() AS ErrorMessage
 
end catch


--練丹2
begin transaction
declare @tcount int
set @tcount=0
begin try
  set @tcount+=1  
  select * from books
  select 1/0    
end try
begin catch
  select
  ERROR_NUMBER() AS ErrorNumber
  ,ERROR_SEVERITY() AS ErrorSeverity
  ,ERROR_STATE() AS ErrorState
  ,ERROR_PROCEDURE() AS ErrorProcedure
  ,ERROR_LINE() AS ErrorLine
  ,ERROR_MESSAGE() AS ErrorMessage
  --if @tcount>1
  set @tcount=0
  rollback transaction
end catch
if @tcount >1
begin
  commit transaction
end

沒有留言:

張貼留言