Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Location
    India
    Posts
    11

    Unhappy Unanswered: No. of records returned by a query.

    Dear all
    i want to run a query on sql server based on whether the no. of records returned by another query is 0 or >0. what will be the sql code for this.
    Pankaj Garg

  2. #2
    Join Date
    Aug 2003
    Location
    Delft, The Netherlands (EU)
    Posts
    447

    Re: No. of records returned by a query.

    Assume your SELECT statement is called Q. Than you can write

    if (select count(*) from (Q) q)>0
    BEGIN
    SELECT 'Your TRUE block'
    END
    ELSE
    BEGIN
    SELECT 'Your block for count(*) = 0'
    END
    Make everything as simple as possible, but not simpler! - A. Einstein
    DB Problems? DB Explorer, BTrieve Re-engineering, DB Conversions & ETL? Conversion Tool

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    ...or, if you have already run your select statement as part of your procedure, use this so you don't have to execute it a second time:

    if @@ROWCOUNT > 0
    BEGIN
    SELECT 'Your TRUE block'
    END
    ELSE
    BEGIN
    SELECT 'Your block for count(*) = 0'
    END

    @@ROWCOUNT stores the number of records returned by the last executed statement. If you don't use the value immediately, you will need to store it in a variable because it will change with your next statement.

    blindman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •