|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
If Else StatementALL;
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 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 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 > > > > > 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 The count is either zero or greater than zero.
IF ( SELECT count(1) AS 'Number of Rows' FROM ManualContribution ) = 0 Insert Records into table -- Show quoteArnie Rowland, Ph.D. Westwood Consulting, Inc Most good judgment comes from experience. Most experience comes from bad judgment. - Anonymous "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 > > > > > 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 > > > > > 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 >> >> >> >> >> >> >> > > > > On Fri, 1 Sep 2006 10:59:58 -0400, Stephen Lynch wrote:
>ALL; Hi Steve,> >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 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 |
|||||||||||||||||||||||