【问题标题】:Error with dates in stored procedure calling Oracle database in SQL Server在 SQL Server 中调用 Oracle 数据库的存储过程中的日期错误
【发布时间】:2021-09-27 21:04:06
【问题描述】:

我在 SQL Server 中有一个存储过程,它调用一个 Oracle 数据库并返回一个结果集。

存储过程是

DECLARE
    @IntStartDate Varchar(50),
    @IntEndDate Varchar(50),
    @ConStartDate Varchar(50),
    @ConEndDate Varchar(50),
    @Query Varchar(Max)

--Convert SQL to UNIX Time
SET @IntStartDate = CONVERT(varchar(50), @StartDate, 23)
SET @IntEndDate = CONVERT(varchar(50), DATEADD(day, 1, @EndDate), 23)

SET @ConStartDate = (DATEDIFF(SECOND,'1970-01-01', @IntStartDate))
SET @ConEndDate = (DATEDIFF(SECOND,'1970-01-01', @IntEndDate ))

SET @Query = 'select * 
              from openquery([linkedserver],
''
SELECT 
sm.movement_type as "Movement Type",
sm.reference as "RMA Number",
cri.reason_code as "RMA Reason",
cri.sales_document_num as "Order Number",
sh.customer_account as "Customer Account",
sm.product_code as "SKU",
cri.long_description_1 as "Product Name",
to_char(trunc(date ''1970-01-01'' + sm.movement_date * interval ''1'' second),''DD/MM/YYYY'') as "Movement Date",
sm.quantity as "Quantity",
sm.change_user as "User",
sm.warehouse_code as "Warehouse",
sm.receipt_location as "Location"
FROM
STOCK_MOVEMENT SM
left join CUSTOMER_RETURN_ITEM CRI on sm.reference = cri.rma_num
left join sales_header sh on cri.sales_document_num = sh.sales_document_num
WHERE sm.reference like ''5%''
AND sm.change_user not in (''l.hill'', ''d.newell'', ''p.willman'', ''s.parkinson'')
AND sm.movement_type = ''RMASTK''
AND sm.movement_date  BETWEEN ' + cast(@ConStartDate as varchar)+ ' and ' + cast(@conEndDate as varchar) + '

'')
'

但是我得到了这个错误:

消息 102,第 15 级,状态 1,第 16 行
'1970' 附近的语法不正确。

日期部分似乎在另一个存储过程中工作,我已经比较了它们..但对于我的生活,我看不出有什么区别。

任何帮助表示赞赏。

C

【问题讨论】:

  • 请尝试创建 minimal 可重现的示例,您将找到执行失败的步骤。以select date '1970-01-01'开头,然后添加其他表达式,你就会找到原因。

标签: sql-server oracle tsql


【解决方案1】:

错误与引号的数量有关,它需要是四个而不是两个,否则它会转义该字段。

【讨论】:

    猜你喜欢
    • 2020-09-30
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2018-10-04
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多