Solved

how to return exception or boolean or string from trigger in sql server 2005

Posted on 2011-03-18
3
470 Views
Last Modified: 2013-12-17
hi,

I have sql instead of trigger to eliminate a record to be inserted unless some condition is true,
if record is not inserted, I want to add something to the trigger to notify my program that this record has not been inserted, for example, to return string "not_inserted" or boolean or even generating exception (looks like when there is error in inserting record) or any other thing

I have this code in trigger:

if @nRow=0
Begin
   Insert into ProductType (EnProductType, ArProductType, EnDescription, ArDescription)
   Select dbo.TRIM(EnProductType), dbo.TRIM(ArProductType), EnDescription, ArDescription from Inserted
end
else
// what to add here to return false or exception or string to notify my program?
0
Comment
Question by:njgroup
3 Comments
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 200 total points
Comment Utility
try put this after else

RAISERROR ('Message Here', 16, 10)

Open in new window

0
 
LVL 9

Assisted Solution

by:sarabhai
sarabhai earned 150 total points
Comment Utility
this is a sample code for

CREATE TRIGGER trg_sample on T_Table
FOR INSERT
AS
BEGIN TRY
--Statements
RAISERROR('oops', 0, -1)
END TRY
BEGIN CATCH
PRINT 'oops'
SELECT 1 AS Exception
END CATCH
go
0
 
LVL 3

Assisted Solution

by:kumarnimavat
kumarnimavat earned 150 total points
Comment Utility
You may try using the below given query that uses try and catch constructs. Put your code in between the BEGIN TRY and END TRY

BEGIN TRY
     if @nRow=0
Begin
   Insert into ProductType (EnProductType, ArProductType, EnDescription, ArDescription)
   Select dbo.TRIM(EnProductType), dbo.TRIM(ArProductType), EnDescription, ArDescription from Inserted
end
else
END TRY

BEGIN CATCH
     SELECT
          ERROR_NUMBER() as ErrorNumber,
          ERROR_MESSAGE() as ErrorMessage;
END CATCH;
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now