SQL IF ELSE with output params stored proc help

Posted by Kettenbach on Stack Overflow See other posts from Stack Overflow or by Kettenbach
Published on 2010-04-22T15:18:03Z Indexed on 2010/04/22 15:43 UTC
Read the original article Hit count: 239

Filed under:
|
|

Hi All,

I have a stored proc (SS2008) that takes a couple int ids and needs to look up if they exist in a table before adding a record. I have an int output param I would like to return and set its value based on what occrured. I have this so far, but it always returns 1. Can someone point me in the right direction?

BEGIN TRY

IF EXISTS
(
    SELECT * FROM tbMap WHERE (cId= @CId)
)
    SET @result = -1; -- This C User is already mapped

ELSE IF EXISTS
(
    SELECT * FROM tbMap WHERE (dId = @DId)

)

    SET @result = -2; -- This D User is already mapped

ELSE
    INSERT INTO tbMap (
        Login
        , Email
        , UserName
        , CId
        , DId)
    SELECT 
            @UserName
            , usr.EmailAddress
            , usr.UserName
            , @CId
            , @DId
        FROM tbUser usr WHERE usr.iUserID = @DId

    SET @result = 1;
RETURN

END TRY

What am I missing? Thanks for any tips.

Cheers, ~ck in San Diego

© Stack Overflow or respective owner

Related posts about sql

Related posts about insert