From time to time I need to run the same SQL Server script, every time changing one or two letters in each table name to a value which is different each time I run the script.
How can I set once at the beginning of the script, so that I do not need to change each table name?
Here is a simplified example of the script, which in reality contains a lot more tables:
SELECT * FROM T_Atable1 
    WHERE ref IN (SELECT ref FROM T_Atable2)
         AND customer IN (SELECT customer FROM T_Atable3)
Table names are of the form T_ where:
- "T_" is fixed
- <id>is the variable part of the table name. In the above example = "A"
- <table name>is the fixed name of the table. In the above example, the fixed parts are "table1", "table2", "table3"
I suspect I need to use dynamic SQL but do not know how to do this.
SQL Server could be any version 2003+, depending on the system containing the data.
 
     
     
     
     
     
    