By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,379 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,379 IT Pros & Developers. It's quick & easy.

Any SQL statements to return no records found?

100+
P: 170
hey guys,

just wanna know if there's any SQL statements that will help u to determine if a match is found when u use the SELECT statement?
so if the SELECT statement don't match anything it will return false statement or something like that
May 23 '08 #1
Share this Question
Share on Google+
6 Replies


code green
Expert 100+
P: 1,726
Expand|Select|Wrap|Line Numbers
  1. SELECT 1 FROM tablename WHERE condition
May 23 '08 #2

ck9663
Expert 2.5K+
P: 2,878
Expand|Select|Wrap|Line Numbers
  1. SELECT 1 FROM tablename WHERE condition

To extend this code:
Expand|Select|Wrap|Line Numbers
  1. if exists(select 1 from yourtable where yourcondition)
  2. begin
  3.   do this
  4.   and this
  5.   and this
  6. end
  7. else
  8. begin
  9.    do this
  10.    and this
  11.    and that
  12. end
You only need to enclosed it with BEGIN..END if there are two or more statements that needs to be executed in IF or in ELSE.

-- CK
May 23 '08 #3

100+
P: 170
i'm not sure if the syntax is right
but i think the exists should be enclose within the brackets
my code as said above is

Expand|Select|Wrap|Line Numbers
  1.  
  2.    if (exists SELECT 1 FROM `mml log` WHERE Commands LIKE '%$IMSI%')
  3.    begin
  4.       do  
  5.         while(my $ref = $sth->fetchrow_arrayref){
  6.  
  7.                        print "<tr></tr>";
  8.                        for (my $i=0; $i<$numFields; $i++){
  9.                         print "<td>", $$ref[$i], "</td>\n";  
  10.  
  11.    }
  12. }
  13. end  
  14. else
  15.     begin
  16.      do
  17.         print "<p>Sorry! No Records have been found!</p>";
  18.    end
  19.  
  20.  
and i'm getting errors like

a) exist arguement is not a hash or an array
b) number found where operator expected near "SELECT 1" (Do you need to predeclare SELECT?)
May 26 '08 #4

Delerna
Expert 100+
P: 1,134
your if should be like

Expand|Select|Wrap|Line Numbers
  1. if exists(SELECT 1 FROM `mml log` WHERE Commands LIKE '%$IMSI%')
  2.  
  3.  
also what is `mml log`

if it is a table or view name then it should be like this

Expand|Select|Wrap|Line Numbers
  1. if exists(SELECT 1 FROM [mml log] WHERE Commands LIKE '%$IMSI%')
  2.  
  3.  
May 26 '08 #5

100+
P: 170
hm...if i put the exists outside i'm still getting a synta error
i'm using perl anyway to process this code
May 26 '08 #6

ck9663
Expert 2.5K+
P: 2,878
You're mixing PERL and SQL. Check with PERL forum how to connect to sql-server using PERL and how to define data object in PERL and how to process it.Then you pass your select on that connection and process it.

-- CK
May 26 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.