如何在 ODBC SQLCommand 表达式中传递 SSIS 变量?
我正在尝试使用公共表表达式将增量数据从 ODBC 服务器加载到 SQL 服务器.在 Dbeabver 应用程序中运行查询时,正确执行:
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
从 ODBC 源的 sql 命令表达式运行它时,由于语法错误而失败.它看起来如下:
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"
datestring 变量正在获取服务器日期并将其转换为 yyyy-mm-dd 格式的字符串.我通常使用这种方法从 ADO.NET 中提取数据并且它工作正常.
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.
还有其他方法可以使用 ssis 变量从 ODBC 服务器中提取增量数据吗?
Is there any other way to pull incremental data from ODBC server using ssis variables?
推荐答案
- 使用 OLE DB
试试这个代码,它适用于我自己的 SQL Server 表:
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
您必须在 OLEDB 源代码编辑器中单击参数以配置您需要的内容.使用 '?'表示查询中的变量.
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.
如果你查询太复杂,把它存储在一个存储过程中,像这样调用:
If you query if too complicated, stored it in a stored procedure and call it like this:
EXEC shema.storedProcedureName ?
并映射?"到您的变量 @user::DateString
And map the '?' to your variable @user::DateString
- 使用 ODBC
表达式在数据流属性中的数据流之外.选择表达式属性并添加您的动态查询.
The expressions are outside the data flow in Data Flow Properties. Select the expression property and add your dynamic query.
你的表情将是
"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"
相关文章