I have a database where each table is named by standard conventions. I am building a SQL script to run periodically on various sets of similarly named tables.
I would like to declare a local variable that contains common part of table names between the tables and use that variable to fill in a part of the table name in FROM
statement of my SELECT
statements.
How could I abstract this to run on a table where 1234
is substituted out for the value of a local variable?
Select count(*) From [Database].[dbo].[Table_1234]
This obviously doesn't work:
Select count(*) From [Database].[dbo].['Table_'+@variable]
Is there a way to use local variables as table names in select statements or a way around this?