SQL服务器:使用日期时间变量OPENQUERY [复制]

问题描述 投票:0回答:1

这个问题已经在这里有一个答案:

我试图链接的服务器查询中的Postgres的服务器上的SQL Server环境中发送DATETIME变量。

我的代码:

DECLARE @start_date DATETIME;
SET @start_date ='2019-02-01';

SELECT *
FROM OPENQUERY (postgresDB, 'SELECT id, action_time
                             FROM call_history
                             WHERE action_time > ''' + @start_date + ''' ')

我试着用不同的号码周围的变量引号的实验,但我总是得到这个错误:

不正确的语法附近“+”

sql-server postgresql tsql linked-server
1个回答
0
投票

要么转换你的日期时间为一个字符串的串接,或者声明@startdate作为一个nvarchar或varchar。

当我尝试选择您的字符串:

DECLARE @start_date datetime;
SET @start_date ='2019-02-01';

select 'SELECT
                    id,
                    action_time
                FROM call_history
                WHERE action_time > ''' + @start_date + '''
        '

我得到:

Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.

但是,这工作得很好:

DECLARE @start_date nvarchar(10);
SET @start_date ='2019-02-01';

select 'SELECT
                    id,
                    action_time
                FROM call_history
                WHERE action_time > ''' + @start_date + '''
        '
© www.soinside.com 2019 - 2024. All rights reserved.