|
|
| |

How can I use a variable for the columns/tables in the select statements?
Answer:
SQL Server can contain both static sql statements and dynamic sql statements.
In comparison with static sql statements, dynamic sql statements are not
completely embedded in the source code; instead, portions are stored in
program variables that can be modified at run time.
So, you can use dynamic sql statements (using EXECUTE command)
to execute a character string which contain a variable for the columns/tables
in the select statements.
Read about the EXECUTE command in SQL Server Books Online.
This is the example to make select from the table, which name was passed
into the stored procedure as variable:
CREATE PROCEDURE GetRec
( @tableName sysname )
AS
EXEC ('SELECT * from ' + @tableName)
|
|
|
|