Want to Retrieve information of specific tables where table count is greater than zero

Odpovědět Want to Retrieve information of specific tables where table count is greater than zero

  • quinta-feira, 8 de dezembro de 2011 09:07
     
      Contém Código
    declare @table varchar(max)
    declare @num int
    declare cur cursor
    for select table_name from information_schema.tables where table_name like '%lku'
    open cur
    fetch next from cur into @table
    while @@FETCH_STATUS=0
    begin
    
    Set @num = exec('select count(*) from  '+@table) /* getting here error */
    
    fetch next from cur into @table
    end
    close cur
    deallocate cur
    
    


    Harsimranjeet Singh

Todas as Respostas

  • quinta-feira, 15 de dezembro de 2011 10:17
    Moderador
     
     Respondido

    Hi Harsimranjeet,

    Change the query like below:

    declare @table varchar(max)
    declare cur cursor
    for select table_name from information_schema.tables where table_name like '%lku'
    open cur
    fetch next from cur into @table
    while @@FETCH_STATUS=0
    begin
    exec ('select count(*) from  '+@table)
    fetch next from cur into @table
    end
    close cur
    deallocate cur

    the above query should be able to retrieve the correct information that you are after.

    Regards,
    Jerry