SELECT with a variable Tablename

  • I am rying to run a TSQL SELECT Count(*) that has a @Tablename variable in it.

    My Tablename name needs to be variable and I will build it on the fly. All I want to know if if there is data in the table.

    --Tried this but it did not work because of the @Database

    Declare @Database varchar(20)

    Set @Database = 'TDSTEP'

    Declare @RowCount int

    SELECT @RowCount = Count(*) FROM @Database

    Print @RowCount

    --Tried this but it did not work because of the @RowCount

    Declare @Database varchar(20)

    Set @Database = 'TDSTEP'

    Declare @RowCount int

    declare @sql as varchar(max)

    set @sql = 'SELECT @RowCount = Count(*) FROM ' + @Database

    exec (@SQL)

    How can i get a variable TableName and yet interigate the number of rows returned?

  • bgrossnickle (8/13/2013)


    I am rying to run a TSQL SELECT Count(*) that has a @Tablename variable in it.

    My Tablename name needs to be variable and I will build it on the fly. All I want to know if if there is data in the table.

    --Tried this but it did not work because of the @Database

    Declare @Database varchar(20)

    Set @Database = 'TDSTEP'

    Declare @RowCount int

    SELECT @RowCount = Count(*) FROM @Database

    Print @RowCount

    --Tried this but it did not work because of the @RowCount

    Declare @Database varchar(20)

    Set @Database = 'TDSTEP'

    Declare @RowCount int

    declare @sql as varchar(max)

    set @sql = 'SELECT @RowCount = Count(*) FROM ' + @Database

    exec (@SQL)

    How can i get a variable TableName and yet interigate the number of rows returned?

    Do you need this to use a variable for some reason?

    You can do this with something like this.

    Declare @Database varchar(20)

    Set @Database = 'YourTableNameHere'

    Declare @RowCount int

    declare @sql as varchar(max)

    set @sql = 'SELECT Count(*) FROM ' + @Database

    declare @MyCount table(MyRowCount int)

    insert @MyCount

    exec (@SQL)

    select * from @MyCount

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • declare @TableName varchar(128);

    set @TableName = 'sysdiagrams';

    if exists

    (

    select i.rowcnt

    from sys.tables t

    join sys.sysindexes i on t.object_id = i.id

    where indid in (1,0)

    and t.name = @TableName

    )

    print 'yes'

    ;

  • Have you tried something like this?

    Declare @Database varchar(20)

    Set @Database = 'TDSTEP'

    Declare @RowCount int

    declare @sql as nvarchar(max)

    set @sql = ' SELECT @RowCountOUT = Count(*) FROM ' + @Database

    exec sp_executesql @sql, N'@RowCountOUT int OUTPUT', @RowCountOUT=@RowCount OUTPUT;

    SELECT @RowCount

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • FYI - I wanted the FROM @tablename to be a variable. Not a variable in the WHERE clause.

  • bgrossnickle (8/13/2013)


    FYI - I wanted the FROM @tablename to be a variable. Not a variable in the WHERE clause.

    But you got a different way of doing things that will give you what you need.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Both Sean and Luis code worked great. thanks.

  • Could you use something like this?

    USE [TDStep]

    GO

    SELECT TableName=OBJECT_NAME(OBJECT_ID), st.row_count

    FROM sys.dm_db_partition_stats st

    WHERE index_id < 2

    ORDER BY st.row_count DESC


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

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

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