在OpenQuery Error中使用先前日期搜索

时间:2013-02-26 13:40:24

标签: sql sql-server-2008 tsql

尝试使用昨天日期的where参数运行openquery但是收到错误..任何人都可以帮助

select top 10 * into #temp1 
from openquery(mysql1, 'select * from test.T1 where source = "Data" and 
  calcdate = SELECT REPLACE(CONVERT(VARCHAR, DATEADD(dd, -1, GETDATE()), 102), '.', '-') ')

由于

这是错误 Msg 102,Level 15,State 1,Line 2 '。'。

附近的语法不正确

3 个答案:

答案 0 :(得分:2)

您丢失了转义引号,而SELECT中的WHERE clause是多余的。

试试这个:

select top 10 * into #temp1 
from openquery(mysql1, 'select * from test.T1 where source = ''Data'' and 
  calcdate = DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), ''%Y-%m-%d'')')

答案 1 :(得分:0)

SELECT语句必须在括号中,否则不使用SELECT关键字

select top 10 * into #temp1 
from openquery(mysql1, 'select * from test.T1 where source = "Data" and 
  calcdate = (SELECT REPLACE(CONVERT(VARCHAR, DATEADD(dd, -1, GETDATE()), 102), '.', '-')) ')

答案 2 :(得分:0)

您不需要其他选择查询来使用转换函数。此外 to get the correct results 您应该 comparing dates not varchar values 中的 where clause 。如果calcdate不是日期类型字段,则在比较之前将其转换。试试这个;

Select top 10 * into #temp1 
From openquery(mysql1, 'Select * from test.T1 where source = ''Data'' and 
  calcdate =  dateadd(dd, -1, getdate())')

Demo您选择的查询字符串