Selecting from a table where the name is passed as a variable

前端 未结 1 1362
生来不讨喜
生来不讨喜 2021-01-17 07:08

I am trying to write a simple stored proc which takes three arguments \'database name one\', \'database name two\' and \'table name\'. The sql will then perform a row count

1条回答
  •  梦毁少年i
    2021-01-17 07:48

    You'd have to use dynamic sql, and concatenate the table name into the SQL string to then execute via sp_executsql:

    select @sqlstring = 'select @numrowsOUT = count(*) from ' + QUOTENAME(@tablename)
    EXECUTE sp_executesql ....
    

    0 讨论(0)
提交回复
热议问题