Dynamic SQL in SQL Server - Introduction
Dynamic SQL is nothing but SQL query that is generated programmatically.
For example, we can get records of any table using the below Stored Procedure which implements Dynamic SQL
CREATE PROC GETDETAILSBYTABLENAME(@TableName varchar(50))
AS
BEGIN
Declare @SQLQuery VarChar(5000)
SELECT @SQLQuery = 'SELECT * FROM ' + @TableName
Exec ( @SQLQuery)
END
For example, we can get records of any table using the below Stored Procedure which implements Dynamic SQL
CREATE PROC GETDETAILSBYTABLENAME(@TableName varchar(50))
AS
BEGIN
Declare @SQLQuery VarChar(5000)
SELECT @SQLQuery = 'SELECT * FROM ' + @TableName
Exec ( @SQLQuery)
END
Comments