OpenQuery开始和结束日期SQL

时间:2017-07-31 07:14:58

标签: sql sql-server sql-server-2008

我在运行查询时收到空白数据,如果我只使用两个''然后数据是正确的

DECLARE @strdate date,@endate date
set @strdate = '2017-06-24'
SEt @endate = '2017-06-26'
SELECT *
        FROM Openquery(E2E,'Select 
        Order_Created_at,
        Order_Number
        ,Shipping_Postcode
        ,Payment_Method
        ,Order_item
        ,Item_sku
        ,Item_Unit_Price 
        from sales_order_export_summary Where Date(Order_Created_at) >= '''' + @strdate + '''' AND Date(Order_Created_at) <= ''''+@endate + ''''')

3 个答案:

答案 0 :(得分:1)

在连接之前,您需要将日期转换为字符串,最好是ISO格式

UnityPlayer.UnitySendMessage("Canvas", "PushReceived", "This is your push message");

答案 1 :(得分:0)

如果要在OPENQUERY中使用变量,则必须使用动态SQL。我已将变量@strdate@enddate更改为varchar(10),因为我无法连接字符串。

DECLARE @strdate varchar(10),@endate varchar(10)
SET @strdate = '2017-06-24'
SET @endate = '2017-06-26'
DECLARE @SQL varchar(MAX) = '
SELECT *
        FROM Openquery(E2E,''Select 
        Order_Created_at,
        Order_Number
        ,Shipping_Postcode
        ,Payment_Method
        ,Order_item
        ,Item_sku
        ,Item_Unit_Price 
        from sales_order_export_summary Where Date(Order_Created_at) >= ''''' + @strdate + ''''' AND Date(Order_Created_at) <= '''''+@endate + ''''''')'
EXEC (@SQL)       
OPENQUERY

Documentation

答案 2 :(得分:0)

其他答案解释了格式问题。这是另一种简单的方法,但可能效率不高

DECLARE @strdate date,@endate date
set @strdate = '2017-06-24'
SEt @endate = '2017-06-26'
SELECT *
FROM   Openquery(E2E, 'Select 
        Order_Created_at,
        Order_Number
        ,Shipping_Postcode
        ,Payment_Method
        ,Order_item
        ,Item_sku
        ,Item_Unit_Price 
        from sales_order_export_summary')
WHERE  Order_Created_at >= @strdate
       AND Order_Created_at < Dateadd(dd, 1, @endate)