Solved

SAMPLE PROCEDURES  WITH EXCEPTION HANDLING IN THE MSSQL

Posted on 2008-10-07
6
1,136 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 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Replication question 9 43
SQL Server 2012 - Merge Replication Issue 1 22
Need help how to find where my error is in UFD 6 29
T-SQL Default value in Select? 5 27
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

778 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