Sunday 16 July 2017

Dynamic T-SQL and Its Benefit

Dynamic T-SQL is essential when you need to create, modify, get data or objects, but values or names come as parameters.
We can execute a dynamic sql script in two ways
Ø  Using the EXEC keyword
Ø  Using the EXECUTE keyword.
Ø  Using the sp_executesql stored procedure.
DECLARE @sql varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @ID int
SET @columnList = 'Name, empAdd, Mobile'
SET @ID = 1
SELECT @sql = ' SELECT ' + @columnList +
              ' FROM EMP WHERE ID = ' + cast(@ID as varchar)
  
EXEC (@sql)
See the output


 
 

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts