我尝试在打开的查询中传递日期变量,但它显示错误,如
“+ +附近的语法不正确”
这是我的问题:
DECLARE @fromdt DATETIME = '2018-04-07';
DECLARE @EndDate1 DATETIME = '2018-04-07';
Select * from openquery(TIMEV,
'SELECT REPLACE(LTRIM(REPLACE(badgenumber,"0"," "))," ","0") badgenumber,
checktime as dt
from checkinout a
join USERINFO c on c.userid=a.userid
WHERE checktime >= '''''+CONVERT(CHAR(10), @fromdt, 120)+''''' AND ''''' + CONVERT(CHAR(10), @EndDate1, 120) + ''''' ')
我被困在这里。谢谢你提前......
答案 0 :(得分:1)
OPENQUERY
documentation明确说:
OPENQUERY不接受其参数的变量。
因此,使用来自this site的技巧,您应该可以这样做:
DECLARE @fromdt DATETIME = '2018-04-07';
DECLARE @EndDate1 DATETIME = '2018-04-07';
DECLARE @query nvarchar(max) =
'SELECT * FROM OPENQUERY(TIMEV,
'' SELECT REPLACE(LTRIM(REPLACE(badgenumber,"0"," "))," ","0") badgenumber,
checktime AS dt
FROM checkinout a
JOIN USERINFO c on c.userid=a.userid
WHERE checktime >= ''''' + CONVERT(CHAR(10), @fromdt, 120) + ''''' AND ''''' + CONVERT(CHAR(10), @EndDate1, 120) + ''''' ''
)';
EXEC(@query);
编辑:如果您希望尝试使用我在代码段上方提供的链接,还建议使用其他两种方法。