Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SAMPLE PROCEDURES  WITH EXCEPTION HANDLING IN THE MSSQL

Posted on 2008-10-07
6
Medium Priority
?
1,146 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 1344 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 672 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 1344 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

705 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