Any SQL statements to return no records found?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poolboi
    New Member
    • Jan 2008
    • 170

    #1

    Any SQL statements to return no records found?

    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
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Code:
    SELECT 1 FROM [I]tablename[/I] WHERE [I]condition[/I]

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      Originally posted by code green
      Code:
      SELECT 1 FROM [I]tablename[/I] WHERE [I]condition[/I]

      To extend this code:
      Code:
      if exists(select 1 from yourtable where yourcondition)
      begin
        do this
        and this
        and this
      end
      else
      begin
         do this
         and this
         and that
      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

      Comment

      • poolboi
        New Member
        • Jan 2008
        • 170

        #4
        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

        [CODE=perl]

        if (exists SELECT 1 FROM `mml log` WHERE Commands LIKE '%$IMSI%')
        begin
        do
        while(my $ref = $sth->fetchrow_array ref){

        print "<tr></tr>";
        for (my $i=0; $i<$numFields; $i++){
        print "<td>", $$ref[$i], "</td>\n";

        }
        }
        end
        else
        begin
        do
        print "<p>Sorry! No Records have been found!</p>";
        end

        [/CODE]

        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?)

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          your if should be like

          [code=sql]
          if exists(SELECT 1 FROM `mml log` WHERE Commands LIKE '%$IMSI%')

          [/code]

          also what is `mml log`

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

          [code=sql]
          if exists(SELECT 1 FROM [mml log] WHERE Commands LIKE '%$IMSI%')

          [/code]

          Comment

          • poolboi
            New Member
            • Jan 2008
            • 170

            #6
            hm...if i put the exists outside i'm still getting a synta error
            i'm using perl anyway to process this code

            Comment

            • ck9663
              Recognized Expert Specialist
              • Jun 2007
              • 2878

              #7
              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

              Comment

              Working...