Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai friends,

I am having a stored procedure that will insert the values into a table.Suppose if i want to do error handling means how can i do this.Can i use BEGIN TRY ENT TRY statement?Plz help me regarding this.
Below is the code for the insert stored procedure.


CREATEprocedure [dbo].[InsertProblemdata]
(
@problemnumber int,
@linkedincidents varchar(50),
@linkedbugs varchar(50),
@publishtopartner varchar(50),
@impactscale varchar
(50))
AS
BEGIN
INSERT INTO Problem(ProblemNumber,LinkedIncidents,LinkedBugs,PublishToPartner,ImpactScale)
values
(
@problemnumber ,
@linkedincidents ,
@linkedbugs,
@publishtopartner,
@impactscale)
END


Thanks in advance
Posted
Updated 8-Dec-13 19:21pm
v2

 
Share this answer
 
Hi
Put your insert statement inside Try block as follows,

SQL
BEGIN TRY 
// Insert Statement
END TRY
BEGIN CATCH
// Handle the exception. by storing in error logger table or printing to screen for displaying error details etc....
END CATCH


For more and detailed info , please check the below links:

detailed:
http://technet.microsoft.com/en-us/library/ms175976.aspx[^]


short info:
http://www.dotnet-tricks.com/Tutorial/sqlserver/O3P3120412-SQL-Server-Exception-Handling-by-TRY%E2%80%A6CATCH.html[^]
 
Share this answer
 
Comments
Member 10234093 9-Dec-13 2:00am    
hai karthik.Below is the code i have changed.Can u plz check this.It's too long :)

CREATE procedure [dbo].[InsertProblemdata1]
(
@problemnumber int,
@linkedincidents varchar(50),
@linkedbugs varchar(50),
@publishtopartner varchar(50),
@impactscale varchar
(50),
@primaryimpactedarea varchar(50),
@secondaryimpactedarea varchar(50),
@impactedentity varchar
(50),
@impacttype varchar(50),
@percentimpact int,
@title varchar(50),
@description varchar(50),
@problemreported datetime,
@rcastatus varchar(50),
@rcastatusdetails varchar(50),
@rcaclosetime datetime
)
AS

BEGIN TRY
INSERT INTO Problem(ProblemNumber,LinkedIncidents,LinkedBugs,PublishToPartner,ImpactScale,PrimaryImpactedArea,SecondaryImpactedArea,ImpactedEntity,ImpactType,PercentImpact,Title,Description,ProblemReported,RCAStatus,RCAStatusDetails,RCACloseTime)
values
(
@problemnumber ,
@linkedincidents ,
@linkedbugs,
@publishtopartner,
@impactscale
,
@primaryimpactedarea ,
@secondaryimpactedarea ,
@impactedentity
,
@impacttype ,
@percentimpact ,
@title ,
@description ,
@problemreported ,
@rcastatus ,
@rcastatusdetails ,
@rcaclosetime
)
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() as ERROR_NUMBER,

ERROR_SEVERITY() as ERROR_SEVERITY,

ERROR_STATE() as ERROR_STATE,

ERROR_MESSAGE() as ERROR_MESSAGE
END CATCH
Member 10234093 9-Dec-13 2:01am    
If we execute the above sp is it shown error in screen itself?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900