How is the error handling in stored proc of T-SQL ?

1 How is the error handling in stored proc of T-SQL
2 What is clustered index and non-clustered index? How many clustered indexes and non-clustered indexes can be created in one table?
3-what is disconnected mode?

Questions by ajay_shu007

Showing Answers 1 - 21 of 21 Answers

In SQL Server 2000
 We can check by @@Error<>0 i.e. error.
ex:
     IF @@Error<>0
          BEGIN

             ROLLBACK TRANSACTIOn

             RETURN

          END

     ELSE

          BEGIN

             COMMIT TRANSACTION

             RETURN

          END

Note: You need to check @@Error at every DML statement and since it is Global Variable, So it reset every time.

In SQL Server 2005
We can check by using try catch block
ex:

BEGIN TRY

BEGIN TRANSACTION
/*statement*/

COMMIT TRANSACTION

END TRY

BEGIN CATCH

IF @@TRANCOUNT>0

ROLLBACK TRAN

END CATCH

Note: You can check the following functions in catch block
a)ERROR_NUMBER() returns no of the error
b)ERROR_SEVERITY() returns severity of the error
c)ERROR_STATE() returns error state no
d)ERROR_PROCEDURE() returns name of stored procedure
e)ERROR_LINE() returns line no.
f)ERROR_Message() returns the complete text of the message

kirangiet

  • Aug 16th, 2010
 

1) CLR is integrated in SQL Server 2005. Hence we can use below code for Exception Handling.



BEGIN TRY       
SELECT * FROM Employees;
END TRY
BEGIN CATCH
SELECT          
ERROR_NUMBER() AS ErrorNumber,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH

2) With SQL Server 2008 we can have 1 Clustered Index and 999 Non Clustered
indexes (Not 249)


3) As far as my knowledge is concern disconnected mode is a ADO.NET concept
and has to do nothing with SQL Server. This can be achieved using DataAdapter.
Hence when we require to interact with DB we will connect and once we are done
with it connection will be disconnected. In order to work with connected mode
use SQLCommand.

  Was this answer useful?  Yes

Pradeep Kumar Sharma

  • Dec 25th, 2011
 

Begin Try

--
--
End Try
Begin Catch

Declare @errMsg varchar(1000),@errSev int
select @errMsg=error_message(),@errSev=error_severity()
RaisError(@errMsg,@errSev,1)

End Catch

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions