无法准备OpenQuery语句

时间:2018-12-24 17:09:52

标签: sql sql-server tsql openquery

我有一个使用openquery的动态SQL查询,给了我一个语法错误。在查询窗口中运行该语句时,该语句将运行良好(不使用动态SQL),但是在尝试通过SQL Agent运行时,该语句将失败-

  

无法准备声明。 [SQLSTATE 42000](错误8180)“ =”附近的语法不正确。 [SQLSTATE 42000](错误102)

     

链接服务器“ _________”的OLE DB访问接口“ SQLNCLI11”返回了消息“无法完成延迟的准备。”。 [SQLSTATE 01000](错误7412)。步骤失败。

SELECT @SQL = N'
Insert into ' + @ToDatabase + '.dbo.UserUsages
(Date, AppCode, UserName, EriAccountId, [Count], SubscriptionCode)

SELECT Timestamp
, Appcode
, Username
, EriAccountId
, [Count]
, SubscriptionCode
FROM openquery([' + @ServerSource + '],
''[Timestamp] = CAST([Timestamp] AS date)
, AppCode
, UserName
, EriAccountId
, [Count]= COUNT(MethodName) 
, SubscriptionCode
FROM ' + @FromDatabase + '.dbo.[Log]
'
SELECT @SQL2 = N'
WHERE   
(MethodName = ''''GetSalaryTable'''') OR            
(MethodName = ''''GetSalaryByLevelTable'''') OR     
(MethodName = ''''GetEmployees'''') OR              
(MethodName = ''''GetBenchmarkList'''') OR          
(MethodName = ''''GetAdvancedReport'''') OR         
(MethodName = ''''GetGeoList'''') OR            
(MethodName = ''''GetComparableCompanies'''') OR    
(MethodName = ''''GetComparisonList'''') OR     
(MethodName = ''''GetGAChart'''') OR            
(MethodName = ''''GetOesData'''') OR            
(MethodName = ''''GetTwoCityComparison'''') OR      
(MethodName = ''''GetSalaryFromC3Norms'''') OR      
(MethodName = ''''GetCoLs'''') OR                   
(MethodName = ''''GetPerDiems'''') OR               
(MethodName = ''''GetTwoCityValues'''')  OR         
(MethodName = ''''DisabilityFilterJobs'''') OR      
(MethodName = ''''ProcessAnalysis'''') OR           
(MethodName = ''''GetFormTabs'''') OR               
(MethodName = ''''GetReportDataForJob'''')          


GROUP BY CAST(Timestamp AS date), AppCode, UserName, EriAccountId, 
SubscriptionCode
HAVING      
(SubscriptionCode IS NOT NULL) 
AND (CAST(Timestamp AS date)) BETWEEN ''''' + 
CONVERT(NVARCHAR,CAST(DateAdd(dd, +1, @startDate) AS DATE))  +''''' and 
CAST(DateAdd(dd, -1, GETDATE()) AS DATE) '') 

'

1 个答案:

答案 0 :(得分:0)

SELECT之前需要[Timestamp]