Home All Groups Group Topic Archive Search About
Author
1 Sep 2006 2:59 PM
Stephen Lynch
ALL;

How do I DO Nothing if my IF statement is false?

For example:

IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
DO NOTHING
Else
Insert Records into table

Thanks

Steve

Author
1 Sep 2006 3:14 PM
Roy Harvey
IF NOT (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
Insert Records into table

Roy Harvey
Beacon Falls, CT

On Fri, 1 Sep 2006 10:59:58 -0400, "Stephen Lynch"
<raider1rai***@yahoo.com> wrote:

Show quote
>ALL;
>
>How do I DO Nothing if my IF statement is false?
>
>For example:
>
>IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
>DO NOTHING
>Else
>Insert Records into table
>
>Thanks
>
>Steve
Author
1 Sep 2006 3:15 PM
Immy
IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0)
Insert Records into table


You don't need the else in this occassion.


Show quote
"Stephen Lynch" <raider1rai***@yahoo.com> wrote in message
news:44f84aee$0$3572$815e3792@news.qwest.net...
> ALL;
>
> How do I DO Nothing if my IF statement is false?
>
> For example:
>
> IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
> DO NOTHING
> Else
> Insert Records into table
>
> Thanks
>
> Steve
>
>
>
>
>
Author
1 Sep 2006 3:25 PM
Baj-SGC818
Hi Stephen

You have three options :
a. leave off the else altogether and only work with a positive

e.g.
IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution) > 0
-- What you want to happen to happen goes here
Insert Some Records

b. check for a negative result

e.g.
IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution) !> 0
-- Negative Action (if you must use this construct then leave blank)
ELSE
-- What you want to happen to happen goes here
Insert Some Records


c. change the order of your actions putting what you want first and the
opposite on the other side of the else

e.g.
IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution) > 0
-- What you want to happen to happen goes here
Insert Some Records
ELSE
-- Negative Action Goes Here

Hope this helps

Baj-SGC818



Stephen Lynch wrote:
Show quote
> ALL;
>
> How do I DO Nothing if my IF statement is false?
>
> For example:
>
> IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
> DO NOTHING
> Else
> Insert Records into table
>
> Thanks
>
> Steve
Author
1 Sep 2006 4:59 PM
Arnie Rowland
The count is either zero or greater than zero.

IF ( SELECT count(1) AS 'Number of Rows' FROM ManualContribution ) =  0
   Insert Records into table


--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"Stephen Lynch" <raider1rai***@yahoo.com> wrote in message
news:44f84aee$0$3572$815e3792@news.qwest.net...
> ALL;
>
> How do I DO Nothing if my IF statement is false?
>
> For example:
>
> IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
> DO NOTHING
> Else
> Insert Records into table
>
> Thanks
>
> Steve
>
>
>
>
>
Author
1 Sep 2006 6:34 PM
Stephen Lynch
THANKS

You Guys Rock!!!!!!!

Show quote
"Stephen Lynch" <raider1rai***@yahoo.com> wrote in message
news:44f84aee$0$3572$815e3792@news.qwest.net...
> ALL;
>
> How do I DO Nothing if my IF statement is false?
>
> For example:
>
> IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
> DO NOTHING
> Else
> Insert Records into table
>
> Thanks
>
> Steve
>
>
>
>
>
Author
2 Sep 2006 3:47 AM
Steve Kass
And one more:

INSERT rows into table
WHERE EXISTS (
  SELECT * FROM ManualContribution
) T

Steve Kass
Drew University
www.stevekass.com

Stephen Lynch wrote:

Show quote
>THANKS
>
>You Guys Rock!!!!!!!
>
>"Stephen Lynch" <raider1rai***@yahoo.com> wrote in message
>news:44f84aee$0$3572$815e3792@news.qwest.net...

>
>>ALL;
>>
>>How do I DO Nothing if my IF statement is false?
>>
>>For example:
>>
>>IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
>>DO NOTHING
>>Else
>>Insert Records into table
>>
>>Thanks
>>
>>Steve
>>
>>
>>
>>
>>
>>   
>>
>
>

>
Author
1 Sep 2006 11:16 PM
Hugo Kornelis
On Fri, 1 Sep 2006 10:59:58 -0400, Stephen Lynch wrote:

>ALL;
>
>How do I DO Nothing if my IF statement is false?
>
>For example:
>
>IF (SELECT COUNT(*) AS 'Number of Rows' FROM ManualContribution>0
>DO NOTHING
>Else
>Insert Records into table

Hi Steve,

Your question has been answered already, but here's a small performance
tip - instead of having SQL Server count all five million rows in your
table before comparing it to 0, why not use EXISTS - that will allow SQL
Server to stop execution as soon as the first row is found.

IF NOT EXISTS (SELECT * FROM ManualCOntribution)
  BEGIN;
  Insert rows into table;
  END;

If the insert is just one statement, the BEGIN and END are optional.

--
Hugo Kornelis, SQL Server MVP

AddThis Social Bookmark Button