Solved

SAMPLE PROCEDURES  WITH EXCEPTION HANDLING IN THE MSSQL

Posted on 2008-10-07
6
1,137 Views
Last Modified: 2012-05-07
SAMPLE PROCEDURES WITH EXCEPTION HANDLING IN THE MS SQL
0
Comment
Question by:pavanark
6 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 336 total points
ID: 22657097
basically in sql server 2000 the only way to perform execption handling is by useing the @@Error variable which holds the return code of each executed statement
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22657098
could you please clarify what you are looking for, and for which version of mssql server?
0
 
LVL 14

Assisted Solution

by:rachitkohli
rachitkohli earned 168 total points
ID: 22657118
INSERT INTO TableName
(COL1, COL2) values
(VAL1, VAL2)

-- Error Handler
IF @@ERROR <> 0
BEGIN
PRINT "Error occurred "
Return
END
ELSE
BEGIN
PRINT "Success..!!"
RETURN(0)
END
GO


Check this link also
http://www.sommarskog.se/error-handling-II.html
0
 

Author Comment

by:pavanark
ID: 22657133
using raiserror()
0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 336 total points
ID: 22657143
CREATE TRIGGER employee_insupd
ON employee
FOR INSERT, UPDATE
AS
/* Get the range of level for this job type from the jobs table. */
DECLARE @@MIN_LVL tinyint,
   @@MAX_LVL tinyint,
   @@EMP_LVL tinyint,
   @@JOB_ID smallint
SELECT @@MIN_LVl = min_lvl,
   @@MAX_LV = max_lvl,
   @@ EMP_LVL = i.job_lvl,
   @@JOB_ID = i.job_id
FROM employee e, jobs j, inserted i
WHERE e.emp_id = i.emp_id AND i.job_id = j.job_id
IF (@@JOB_ID = 1) and (@@EMP_lVl <> 10)
BEGIN
   RAISERROR ('Job id 1 expects the default level of 10.', 16, 1)
   ROLLBACK TRANSACTION
END
ELSE
IF NOT @@ EMP_LVL BETWEEN @@MIN_LVL AND @@MAX_LVL)
BEGIN
   RAISERROR ('The level for job_id:%d should be between %d and %d.',
      16, 1, @@JOB_ID, @@MIN_LVL, @@MAX_LVL)
   ROLLBACK TRANSACTION
END

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question