HOW TO CHECK EMPTY TABLE

  • i want to check whether there is data in a particular table

     that query which is also coming from another table so i m fetching through cursor

    i wrote one batch but its giving error near exec can anybody of u correct this

    i dont know how many columns that table contains

    declare @query nvarchar(200)

    declare cs cursor for select query from order

    open cs

    while @@fetch_status=0

    begin

    if exists(exec(@query)

    /*some business logic*/

    fetch next from cs into @query

    end

    close cs

    deallocate cs

  • If we see the cursor that you have wrote then explicitly there are some errors,but first i would like to know what you would like to do.. i mean can you explain the logic with a simple example .. so that i can help you out...

  • if exists( select top 1 * from table)

    will return true if there is data.

  • I'm not sure I understand this at all but it seems to me that you want to know if your "exec(@query)" has returned any rows or not...if this is correct, then you would need to use @@rowcount...eg:

    declare @query varchar(50)
    set @query = 'select colName from myTable'
    exec(@query )
    if @@rowcount > 0
      begin
       process...
      end
    







    **ASCII stupid question, get a stupid ANSI !!!**

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply