4

I'm trying to load incremental data from ODBC server to SQL server using common table expression. When running the query in the Dbeabver application, is executed correctly:

with test as
(
    SELECT userid,sum(goldbalance)
    FROM Server.events_live
    where eventTimestamp>=DATE '2016-01-01' + INTERVAL '-100 day'
    group by userid
    order by sum(goldbalance) desc)
)
select * from test

when running it from an sql command expression of the ODBC source, it fails due to wrong syntax. It looks as follow:

with test as
(
    SELECT userid,sum(goldbalance)
    FROM deltadna.events_live
    where eventTimestamp>=DATE '"+@[User::datestring]+"' + INTERVAL '-100 day'
    group by userid
    order by sum(goldbalance) desc)
)
select * from test"

the datestring variable is getting the server date and convert it to string in the format yyyy-mm-dd. I'm usually use this method to pull data from ADO.NET and it works properly.

Is there any other way to pull incremental data from ODBC server using ssis variables?

nimrod fisher
  • 41
  • 1
  • 1
  • 2
  • I don't know any other way to do this, that's how i usually do this too. Tip: Put the Sql Command Expression in a Variable for easier maintenance. – tobypls Mar 16 '16 at 12:08

1 Answers1

11
  • With OLE DB

Try this code, it works for me with my own tables with SQL Server :

SELECT userid,sum(goldbalance) AS SUMGOLD
FROM deltadna.events_live
WHERE eventTimestamp >= DATEADD(DAY, -100,CONVERT(DATE,?))
GROUP BY userid
ORDER BY SUMGOLD desc

You have to click on Parameters in the OLEDB Source Editor to configure what you need. Use the '?' to represent a variable in your query.

Parameters

If you query if too complicated, stored it in a stored procedure and call it like this:

EXEC shema.storedProcedureName ?

And map the '?' to your variable @user::DateString

  • With ODBC

The expressions are outside the data flow in Data Flow Properties. Select the expression property and add your dynamic query.

ODBC

And your expression will be

"SELECT userid,sum(goldbalance) AS SumGold
FROM deltadna.events_live
where eventTimestamp>=DATE "+@[User::datestring]+" +INTERVAL '-100 day'
group by userid
order by SumGold desc"
Huojian
  • 198
  • 2
  • 14
  • Besides OLE DB source, you can't use the Parameters when pulling data- only with an expression for SQLCommand. This is where I used my SSIS variable datestring. – nimrod fisher Mar 16 '16 at 11:24
  • If you can use only ODBC this is the only way to use expressions, i think that you have a problem with your quotes for your variable. Use mine to test, if doesn't work, add directly ' ' in your string variable expression – Huojian Mar 16 '16 at 12:11
  • Yeah i tried it as well. I just had to insert an hard-coded date in the datestring variable and my script would replace it's value in every new execution. Thanks anyways! – nimrod fisher Mar 16 '16 at 13:32
  • 3
    The pictures for the ODBC example were very helpful. I was having a hard time finding the 'Expressions' element in the properties tree. – John Atwood Feb 20 '19 at 18:20