writing an Dynamic query in sqlserver

Posted by prince23 on Stack Overflow See other posts from Stack Overflow or by prince23
Published on 2010-03-30T07:25:31Z Indexed on 2010/03/30 7:33 UTC
Read the original article Hit count: 247

Filed under:

hi,

DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)
DECLARE @region varchar(75)
SET @columnList = 'first_name, last_name, city'
SET @city = '''London'''
SET @region = '''South'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM dbo.employee WHERE City = ' + @city   and 'region = '+@region
--and 'region = '+@region
print(@sqlCommand)
EXEC (@sqlCommand)

when i run this command i get an error

Msg 156, Level 15, State 1, Line 8 Incorrect syntax near the keyword 'and'.

and help would great thank you

© Stack Overflow or respective owner

Related posts about sql-server