I am not able to successfully run a code with openquery to a oracle server.
Please do not take into account field names/data types, as I had to present only a part of the whole procedure. It's quite long. I believe the problem lies in quotation marks, etc...
The procedure compiles all right. Each time I execute the procedure, an error occurs:
Msg 105, Level 15, State 1, Line 53
Unclosed quotation mark after the character string 'SELECT TO_NUMBER(XYZ_1) XYZ_1,
XYZ_2,
cast (''''0'''' as number(5)) as B1,
cast(''''1753-01-01'''' as date) NULL_DATE
I am lost - where the heck should I put those missing quotation mark?
CREATE TABLE #tmpXYZ Header (
[XYZ_1] [int] PRIMARY KEY,
[XYZ_2] [varchar](20),
[XYZ_3] [varchar](20),
-- more fields
[XYZ_N] [varchar](50)
)
declare @sqlInv nvarchar(3000)
set @sqlInv =
'insert into #tmpXYZ Header (
[XYZ_1],
[XYZ_2],
[XYZ_3],
-- more fields
[XYZ_N]
)
select
* FROM OPENQUERY(XYZ_ORACLE,
''SELECT TO_NUMBER(XYZ_1) XYZ_1,
XYZ_2,
cast (''''0'''' as number(5)) as B1,
cast(''''1753-01-01'''' as date) NULL_DATE,
-- more fields
cast ('''' '''' as varchar(20)) as A19
from XYZ.V_HEADER
where
(DATE >= ''''TO_DATE(''''' + @startDate + ''''', ''''YYYYMMDD'''')'''' AND
DATE <= ''''TO_DATE(''''' + @endDate + ''''', ''''YYYYMMDD'''')'''' AND
QWE = ''''0'''' AND
ABC = ''''13'''' ) '' )
'
exec sp_executesql @sqlInv

cast (''''0'''' as number(5)) as B1seems useless. Just write0 as B1(orcast(0 as number(5)) as B1). And you don't need to enclose function names (to_date()in single quotes, only string literals. – a_horse_with_no_name Mar 12 at 8:49