Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'select'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ')'.

durai raj s 20 Reputation points
2023-03-18T07:30:26.0766667+00:00

Query

Declare @Sql_Query nvarchar(max)

set @Sql_Query =('select Name,Age,Year,DateofBirth,City,Correction,Time,Id,No From Student.dbo.TestResult where Name=ABCD and Age=21');

set @Sql_Query ='select * from Openquery ([SESDATA],'+@SQL_QUERY+')'

exec sp_executesql @Sql_Query

error

Msg 156, Level 15, State 1, Line 1

Incorrect syntax near the keyword 'select'.

Msg 102, Level 15, State 1, Line 1

Incorrect syntax near ')'.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,598 questions
{count} votes

Accepted answer
  1. Viorel 111.5K Reputation points
    2023-03-18T08:02:04.45+00:00

    Try adjusting one of the lines:

    set @Sql_Query ='select * from Openquery ([SESDATA], ''' + @SQL_QUERY + ''')'
    

    Maybe the real query need some more “'” if parameterised statements cannot be used.


0 additional answers

Sort by: Most helpful