Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old April 28th, 2008, 05:03 PM
AndyMNE's Avatar
AndyMNE AndyMNE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Location: Iowa
Posts: 187 AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 1 h 50 m 10 sec
Reputation Power: 74
MySpace
If-statements and count

I'm not particularly adept in SQL :|

I want to perform some SQL under the condition that the count of a table is 0.

Something like...

Code:
IF ( (SELECT COUNT(*) FROM table1) = 0 )


Could someone help me with the syntax of this?

Thanks in advance
__________________
www.myspace.com/andylowwww
Soft Dev - C++/SQL/MFC

Reply With Quote
  #2  
Old April 28th, 2008, 05:24 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 30 m 33 sec
Reputation Power: 891
that looks fine
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old April 29th, 2008, 08:46 AM
AndyMNE's Avatar
AndyMNE AndyMNE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Location: Iowa
Posts: 187 AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 1 h 50 m 10 sec
Reputation Power: 74
MySpace
It appears the if-block is ALWAYS being entered

Code:
IF ((SELECT COUNT(*) FROM table1) = 0)
BEGIN
  -- some SQL here
END


If I run the SELECT statement, I get 178683 returned. The SQL inside the BEGIN and END still gets ran.

Any ideas?

Last edited by AndyMNE : April 29th, 2008 at 10:44 AM.

Reply With Quote
  #4  
Old April 29th, 2008, 10:55 AM
AndyMNE's Avatar
AndyMNE AndyMNE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Location: Iowa
Posts: 187 AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 1 h 50 m 10 sec
Reputation Power: 74
MySpace
I've also tried...

Code:
IF ((SELECT COUNT(*) FROM table1) = 0)
THEN
  -- some SQL here
#LOG test
END IF

Reply With Quote
  #5  
Old April 29th, 2008, 11:17 AM
AndyMNE's Avatar
AndyMNE AndyMNE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Location: Iowa
Posts: 187 AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level)AndyMNE User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 1 h 50 m 10 sec
Reputation Power: 74
MySpace
I'm guessing #LOG isnt processed by the SQL and just runs no matter what.

Reply With Quote
  #6  
Old April 29th, 2008, 02:50 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 30 m 33 sec
Reputation Power: 891
you might want to begin by telling us which DBMS you're running

and how/where you are executing that SQL

and whether you are using an application programming language where possibly you should be doing the IF logic instead

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > If-statements and count


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway