简体   繁体   中英

Pulling a SQL Server date to be used in Oracle query within SSIS environment is ignored in the Oracle query

I am having trouble in my Oracle query that uses a variable stored in SSIS which has a date that is pulled from sql server .

I am using an execute sql task that simply gets a max date from a sql server table and stores it in a variable. Eg

SELECT MAX(t.Date) FROM table t;

I then want to use that variable in my Oracle query which is an ADO.NET source connection. I noticed you can't parameterize in those connections and found the work around where you use the sql expression with your user variable in it. So now my Oracle source query looks something like this:

"SELECT DISTINCT t.* FROM table t WHERE TO_CHAR(t.LastUpdateDate, 'YYYY-MM-DD') > " + "'@[User::LastUpdateDate]'"

The query syntax itself is fine, but when I run it, it is pulling all rows and seems to be completely ignoring the where clause of the date.

I've tried removing the TO_CHAR from LastUpdateDate. I've tried adding a TO_CHAR to my user variable @[User::LastUpdateDate] . I've tried using the CONVERSION() function from sql server on @[User::LastUpdateDate] .

Nothing seems to work and the query just runs and pulls in all data as if I don't have the WHERE clause on the query.

Does anyone know how to rectify this issue or point out what I might be doing wrong?

Thank you for any and all help!

**EDIT: My date being pulled from SQL Server is in this format: 2022-09-01 20:17:58.0000000

This is not an answer, just troubleshooting advice

You do not say what data type @[User::LastUpdateDate] is, I'll assume it's a datetime

Ideally all datetime data should be kept in datetime data types, then format becomes completely irrelevant. However since it's difficult to parameterise Oracle queries in SSIS, you have to concoct a string to be submitted. Now date format does become important.

On to something a little different, it is a very good habit performancewise, to not put functions around columns that you are searching on. This is called sargability - look it up.

Given these things, I suggest that you concoct your required SQL query bit by bit and troubleshoot.

First, format your date parameter as an Oracle date literal. Remember this is normally a bad and unecessary thing. We are only doing it because we have to concoct a SQL string.

So create another SSIS variable called strLastUpdateDate and put this hideous expression in it:

RIGHT("0" + (DT_STR,2,1252)DATEPART( "dd" , @[User::LastUpdateDate] ), 2) + '-' +
(DT_STR,3,1252)DATEPART( "mmm" , @[User::LastUpdateDate] ) + '-' +
(DT_STR,4,1252)DATEPART("yyyy" , @[User::LastUpdateDate] )

Yes this is ludicrously long code but it will turn your date variable into a Oracle string literal. You could simplify this by putting it into your original max query but lets not go there. Use whatever debugging technique you have to confirm that it works as expected.

Now you should be able to use this:

"SELECT t.*, '"+@[User::LastUpdateDate]+"' As MyStrDate FROM table t WHERE 
t.LastUpdateDate > '" @[User::strLastUpdateDate] + "'"

You can try running that and see if it makes any difference. Make sure you use this https://dba.stackexchange.com/questions/8828/how-do-you-show-sql-executing-on-an-oracle-database to monitor what is actually being submitted to Oracle.

This is all from memory and googling - I haven't done SSIS for many years now

I suspect after all this you may still have the same problem because I recall from many years having the same mysterious issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM