SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Throw an Error on a procedure


Throw an Error on a procedure

Author
Message
hoseam
hoseam
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4853 Visits: 505
Hi,

I have a procedure that does MERGE. however I have new changes, please see below, these are some of the variables I have:


CREATE PROCEDURE procedure_name
@procedureNameID OUT
@MemberID
@StudentID
@ErrorCode OUT
@ErrorDesc OUT
AS
BEGIN

BEGIN TRY

END TRY

BEGIN CATCH
END CATCH

END


I am getting Member and Student ID as parameters, but I want to Test them first to see if they exist in my tables, if they don't, I want the procedure and I want it return a message that Member or Student ID does not exist.

I have to test MemerID alone first;

IF( SELECT MemberID
FROM database.dbo.view_MyView
WHERE MemberID = @MemberID ) IS NULL

If MemberID is null, I have to throw an error and return the @MemberID with a message back to the users that the @MemberID they sent doesn't exist, the same with StudentID.

I've been trying RAISERROR but I don't been to get it right, instead it runs the whole proc. please help.
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)

Group: General Forum Members
Points: 207761 Visits: 24222
Try something like this

BEGIN TRY
IF NOT EXISTS (
SELECT 1
FROM dbo.view_MyView
WHERE MemberID = @MemberID
)
BEGIN
THROW 50000
,'MemberId does not exist'
,1;
END
END TRY

BEGIN CATCH
THROW
END CATCH



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
drew.allen
drew.allen
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60621 Visits: 16019
What severity are you using in your RAISERROR?

It sounds like you don't have your BEGIN/END statements placed properly. You should never be able to process both sides of an IF...THEN...ELSE statement.

Also, I would use IF NOT EXISTS ( <subquery> ) rather than IF ( <subquery> ) IS NULL, for several reasons.
1) It's more widely used.
2) It's more accurate. It can distinguish between failing to return a record and returning a record whose column contains a NULL value.
3) It doesn't fail if the subquery returns multiple records
4) It doesn't fail if the subquery returns multiple fields.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
hoseam
hoseam
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4853 Visits: 505
Phil Parkin - Tuesday, February 6, 2018 9:03 AM
Try something like this

BEGIN TRY
IF NOT EXISTS (
SELECT 1
FROM dbo.view_MyView
WHERE MemberID = @MemberID
)
BEGIN
THROW 50000
,'MemberId does not exist'
,1;
END
END TRY

BEGIN CATCH
THROW
END CATCH

Thank you Phil. How do I then return that "MemberID does not exist" back to users who calls my proc?

Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)

Group: General Forum Members
Points: 207761 Visits: 24222
hoseam - Tuesday, February 6, 2018 10:05 PM

Thank you Phil. How do I then return that "MemberID does not exist" back to users who calls my proc?

How are they calling the proc? Web form? Windows app? Something else?



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search