BRING BACK TABLE NAME IN QUERY

  • I need to bring back a list that contains TABLENAME and COUNT with a where clause that is the same on every table (the same column appears in all tables)

     

    Should look like this

     

    table1     2500

    table2     123650

    etc...................

     

    Any help greatly appreciated. We are having tables where lines are showing as uncommitted inidcated by the common column in the tables.


    ------------------------------
    The Users are always right - when I'm not wrong!

  • SP_MSFOREACHTABLE "select '?', count(column_name) from ?"



    Shamless self promotion - read my blog http://sirsql.net

  • Thanks Nicholas. You are a star.

    I can get rid of my 500 line query now and replace it with 1 line.

    I could not find anything on BOL.


    ------------------------------
    The Users are always right - when I'm not wrong!

  • sp_msforeachtable is undocumented by Microsoft, but one of the best procs out there....as is it's companion sp_msforeachdb



    Shamless self promotion - read my blog http://sirsql.net

  • I know I am asking for the world, but one other thing.

    The code brings back a separate window for each query in QA. I was hoping to bring back the result in one window.

    Not too much of a problem, though. More of a nice to have.


    ------------------------------
    The Users are always right - when I'm not wrong!

  • create table #test (tablename varchar(100), count int)

    insert into #test exec SP_MSFOREACHTABLE "select '?', count(*) from ?"

    select * from #test

    --drop table #test



    Shamless self promotion - read my blog http://sirsql.net

  • Thanks again, Nicholas. I completely missed that one. I was looking for something more complicated rather than the obvious.


    ------------------------------
    The Users are always right - when I'm not wrong!

Viewing 7 posts - 1 through 6 (of 6 total)

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