August 5, 2011 at 1:09 pm
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?????
August 5, 2011 at 1:41 pm
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
August 5, 2011 at 1:51 pm
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??
August 5, 2011 at 2:12 pm
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
August 5, 2011 at 2:19 pm
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?
August 5, 2011 at 2:24 pm
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
August 5, 2011 at 2:30 pm
Forgot the else...
IF EXISTS(...)
BEGIN
PRINT 'do something conditionally'
ELSE
PRINT 'do something else conditionally'
END
PRINT'always do this.'
August 5, 2011 at 2:34 pm
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
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply