Wednesday, September 10, 2014

Dynamic sql query



Use dynamic sql query like below


declare @TableName varchar(max)
declare @type varchar(max)
  set @TableName='test'
  set @type ='''abc'''
  declare @SQLQuery varchar(max)
   SET @SQLQuery = 'SELECT LETTER_ID FROM ' + @TableName  + ' WHERE LETTER_TYPE =  ' + @type
  print @SQLQuery
 execute(@SQLQuery)

No comments:

Post a Comment