Need to run stored procedure based on value of the column

  • I want to run the stored procedure only when value in the column called group is 1. If the value is 2 then exit the stored procedure.

    I tried to write the code like this

    declare @check int

    set @check = (select group from dbo.group_data );

    If @check <>2

    exit

    else run

    but it is giving the following error

    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

    I guess the select statement is returning more than one value. How can I change the statement such that more than 1 value is accepted for @check parameter?????

  • soemthing like this?

    If EXISTS(select [group] from dbo.group_data WHERE [group]<> 2)

    BEGIN

    EXEC MyProcedure @param

    --email that the proc was run?

    END

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I want to include this check within the stored procedure. I want sql to automatically check when stored proc is called by JOB...Can I do that?? or should I check the condition when I try to execute it??

  • @lowell

    Thanks lowell

    I created another stored procedure to check the condition and to call this stored proc..and I am able to check the condition....

  • lallu_jaya (8/5/2011)


    I want to include this check within the stored procedure. I want sql to automatically check when stored proc is called by JOB...Can I do that?? or should I check the condition when I try to execute it??

    still using the EXISTS idea above:

    CREATE PROCEDURE MyProc

    AS

    BEGIN

    If EXISTS(select [group] from dbo.group_data WHERE [group] <> 2)

    BEGIN

    RETURN--leaves the procedure

    END

    --if you got this far, you decided not to bail out....continue processing

    END

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • The if exists statement that I included performed the check but it also updated the column group values that were not equal to 2 (<>2) made 2.

    It updated the legitimate records that were having group value = 1

    How can I address this?

  • lallu_jaya (8/5/2011)


    The if exists statement that I included performed the check but it also updated the column group values that were not equal to 2 (<>2) made 2.

    It updated the legitimate records that were having group value = 1

    How can I address this?

    you'll have to show the eact code you used...if exists(), by itself, would not update anything. you probably need a BEGIN END block, as an If statement will conditioanlyly performa ONLY the NEXt command after the IF.

    IF EXISTS(...)

    BEGIN

    PRINT 'do something conditionally'

    PRINT 'do something else conditionally'

    END

    PRINT'always do this.'

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Forgot the else...

    IF EXISTS(...)

    BEGIN

    PRINT 'do something conditionally'

    ELSE

    PRINT 'do something else conditionally'

    END

    PRINT'always do this.'

  • i think the Op is doing two or more things ...not real sure now after rereading...

    IF EXISTS(...)

    BEGIN

    PRINT 'do something conditionally'

    PRINT 'do something else conditionally'

    END

    ELSE

    BEGIN

    PRINT 'do an else based something conditionally'

    PRINT 'do an else based something else conditionally'

    END

    PRINT 'always do this.'

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply