Give a tables name as a strored procedures parameter

  • Hy, excuse my poor english (i'm french). I would like to know how to pass to a stored procedure, the name of a table: i explain: I made a stored procedure which return the sum of a column but as this column exist in various table, i would like to pass to this procedure the name of the table, but the 'Select @Total=SUM(Total) FROM @TableName' does not work... Is anyone can help me please

  • use pubs

    go

    declare @stmt nvarchar(100)

    declare @t_name varchar(10)

    set @t_name = 'authors'

    set @stmt = 'SELECT * FROM ' + @t_name

    exec sp_executesql @stmt

    Also read http://www.sommarskog.se/dynamic_sql.html

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

Viewing 2 posts - 1 through 2 (of 2 total)

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