Skip to content
Advertisement

Openquery error ‘Incorrect Syntax near keyword Transaction’

I have a stored procedure that uses openquery to fetch data from a table in a linked server. I need to pass a parameter in the query, so it as below

   Declare @query nvarchar(max)
   Declare @empRequestid nvarchar(max)
   
   Set @empRequestid = '100001' --(This is an input parameter)
   
   Set @query = 'SELECT * INTO [dbo].tblEmp_Report FROM OPENQUERY (SALESIT_SC3VIS_DEV, 
   ''SELECT EmployeeName, EmployeeNumber
   FROM   XXEmployee_Temp_ReportTable        
   WHERE  1=1        AND  description =''Transaction Employee Report''      
   and emp_requestid ='+@empRequestid +'        
   ORDER  BY period_id'')'

   EXEC(@query)

The error is get however is,
Incorrect syntax near the keyword ‘Transaction’.

What am I doing wrong?

Advertisement

Answer

Try this:

   Declare @query nvarchar(max)
   Declare @empRequestid nvarchar(max)
   
   Set @empRequestid = '100001' --(This is an input parameter)
   
   Set @query = 'SELECT * INTO [dbo].tblEmp_Report FROM OPENQUERY (SALESIT_SC3VIS_DEV, 
   ''SELECT EmployeeName, EmployeeNumber
   FROM   XXEmployee_Temp_ReportTable        
   WHERE  1=1        AND  description =''''Transaction Employee Report''''
   and emp_requestid ='+@empRequestid +'        
   ORDER  BY period_id'')'


   SELECT @query
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement