Translate

Thursday, 23 April 2015

execute dynamic sql command in sql server 2008

DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75),@G_Id int
DECLARE @city varchar(75)
SET @columnList = 'a.Id,a.Name,a.City,a.G_Id'
SET @city = '''bbsr'''
set @G_Id=1
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM mytable a
 WHERE a.City='+@city+'and a.G_Id='+convert(varchar,@G_Id)
 --print @sqlCommand
EXEC (@sqlCommand)

No comments:

Post a Comment