Solved

SAMPLE PROCEDURES  WITH EXCEPTION HANDLING IN THE MSSQL

Posted on 2008-10-07
6
1,142 Views
Last Modified: 2012-05-07
SAMPLE PROCEDURES WITH EXCEPTION HANDLING IN THE MS SQL
0
Comment
Question by:pavanark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

617 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