在openquery SQL中使用getdate()到Oracle链接服务器

时间:2016-02-09 18:43:17

标签: sql sql-server oracle

有谁能告诉我我做错了什么?我正在使用Microsoft SQL Server Management Studio ... Microsoft SQL Server 2012 .. Management Studio版本11.0.2100.60 ..我正在连接到Oracle Server以获取数据。

错误在@StartDate突出显示之前有+ ....

我的错误是:

Msg 102, Level 15, State 1, Line 17 
Incorrect syntax near '+'.

我的代码是:

Declare @cmd as varchar(8000)
Declare @StartDate varchar(30)
Declare @EndDate varchar(30)

set @StartDate = getdate() - 1
set @EndDate = getdate()


INSERT INTO dex_smallprojects..ShiftSeqData (actual) Select qty FROM OPENQUERY (jitprod, 'SELECT Count(*) AS qty FROM j_prodstep, j_prodevent, j_subject, j_subjecttype, j_prodeventset 
WHERE pre_prodstep_id = prs_id 
AND pre_subject_id = s_id 
AND s_subjecttype_id = sty_id 
AND pre_prodeventset_id = pres_id 
AND prs_key = ( ''GM:Misc:Fascia:Rear:GP12'' ) 
AND sty_keyincompany IN (''1100:GM:561MCE:Assembled:Fascia:Rear'',''1101:GM:967MCE:Assembled:Fascia:Rear'',''1102:GM:968MCE:Assembled:Fascia:Rear'') 
AND pre_tcreation BETWEEN ( To_date(''' + @StartDate @ + ''', ''MM/DD/YYYY HH:MI:SS AM'') ) 
AND ( To_date(''' + @EndDate @ + ''', ''MM/DD/YYYY HH:MI:SS AM'') )
AND pres_key = ''Default'' ')

2 个答案:

答案 0 :(得分:1)

getDate()的oracle等价物是sysdate。改变这个:

AND pre_tcreation BETWEEN ( To_date(''' + @StartDate  + ''', ''MM/DD/YYYY HH:MI:SS AM'') ) 
AND ( To_date(''' + @EndDate  + ''', ''MM/DD/YYYY HH:MI:SS AM'') )

到这个

and pre_tcreation between sysdate - 1 and sysdate

oracle会理解它。

答案 1 :(得分:-1)

你有一个额外的@符号,其中startdate和enddate参数是

试试这个:

Declare @cmd as varchar(8000)
Declare @StartDate varchar(30)
Declare @EndDate varchar(30)

set @StartDate = getdate() - 1
set @EndDate = getdate()


INSERT INTO dex_smallprojects..ShiftSeqData (actual) Select qty FROM OPENQUERY (jitprod, 'SELECT Count(*) AS qty FROM j_prodstep, j_prodevent, j_subject, j_subjecttype, j_prodeventset 
WHERE pre_prodstep_id = prs_id 
AND pre_subject_id = s_id 
AND s_subjecttype_id = sty_id 
AND pre_prodeventset_id = pres_id 
AND prs_key = ( ''GM:Misc:Fascia:Rear:GP12'' ) 
AND sty_keyincompany IN (''1100:GM:561MCE:Assembled:Fascia:Rear'',''1101:GM:967MCE:Assembled:Fascia:Rear'',''1102:GM:968MCE:Assembled:Fascia:Rear'') 
AND pre_tcreation BETWEEN ( To_date(''' + @StartDate  + ''', ''MM/DD/YYYY HH:MI:SS AM'') ) 
AND ( To_date(''' + @EndDate  + ''', ''MM/DD/YYYY HH:MI:SS AM'') )
AND pres_key = ''Default'' ')