使用 Oracle OLE DB 提供程序时如何解决 SQL 查询参数映射问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/58540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:28:09  来源:igfitidea点击:

How to resolve SQL query parameters mapping issues while using Oracle OLE DB provider?

oraclessis

提问by Rich Lawrence

When trying to enter a SQL query with parameters using the Oracle OLE DB provider I get the following error:

尝试使用 Oracle OLE DB 提供程序输入带参数的 SQL 查询时,出现以下错误:

Parameters cannot be extracted from the SQL command. The provider might not help to parse parameter information from the command. In that case, use the "SQL command from variable" access mode, in which the entire SQL command is stored in a variable.
ADDITIONAL INFORMATION:
Provider cannot derive parameter information and SetParameterInfo has not been called. (Microsoft OLE DB Provider for Oracle)

无法从 SQL 命令中提取参数。提供程序可能无法帮助解析命令中的参数信息。在这种情况下,请使用“来自变量的 SQL 命令”访问模式,在这种模式下,整个 SQL 命令都存储在一个变量中。
附加信息:
提供程序无法派生参数信息并且尚未调用 SetParameterInfo。(用于 Oracle 的 Microsoft OLE DB 提供程序)

I have tried following the suggestion here but don't quite understand what is required:Parameterized queries against Oracle

我尝试按照此处的建议进行操作,但不太明白需要什么:针对 Oracle 的参数化查询

Any ideas?

有任何想法吗?

回答by Rich Lawrence

To expand on the link given in the question:

要扩展问题中给出的链接:

  1. Create a package variable
  2. Double click on the package variable name. (This allows you to access the properties of the variable)
  3. Set the property 'EvaluateAsExpression' to true
  4. Enter the query in the expression builder.
  5. Set the OLE DB source query to SQL Command from Variable
  1. 创建包变量
  2. 双击包变量名称。(这允许您访问变量的属性)
  3. 将属性“EvaluateAsExpression”设置为 true
  4. 在表达式生成器中输入查询。
  5. 将 OLE DB 源查询从变量设置为 SQL 命令

The expression builder can dynamically create expressions using variable to create 'parametised queries'.
So the following 'normal' query:

表达式构建器可以使用变量动态创建表达式以创建“参数化查询”。
所以下面的“正常”查询:

select * from book where book.BOOK_ID = ?

Can be written in the expression builder as:

可以在表达式生成器中编写为:

"select * from book where book.BOOK_ID = " + @[User::BookID]

You can then do null handling and data conversion using the expression builder.

然后,您可以使用表达式构建器进行空值处理和数据转换。

回答by toha

If You use Data Flow Task and use OLE DB Source, and you need parameterize your Query :

如果您使用数据流任务并使用 OLE DB 源,并且您需要参数化您的查询:

  1. Create Variable to save "Full" of Query statement : Right Click on blank area outside the package - and Click Variables :
  1. 创建变量以保存查询语句的“完整”:右键单击包外的空白区域 - 然后单击变量:

Variables

变量

Click Add Variables on Variables Window :

单击变量窗口上的添加变量:

enter image description here

在此处输入图片说明

Make the name is SQL_DTFLOW_FULLor something that can you understand easily. The variable data typeis STRING

使名称是SQL_DTFLOW_FULL或您可以轻松理解的东西。的variable data typeSTRING

  1. Create Variable(s) to save your parameter(s).
  1. 创建变量以保存您的参数。

i.e, the full of Query stamements is :

即,完整的查询语句是:

SELECT * FROM BOOK WHERE BOOK_ID = @BookID --@BookID is SQL Parameter

at the sample above, I have just one parameter : @BookID, so I need to create one variable to save my parameter. Add more variables depends on your Queries.

在上面的示例中,我只有一个参数:@BookID,所以我需要创建一个变量来保存我的参数。添加更多变量取决于您的查询。

ParamAdd

参数添加

Give it name SQL_DTFLOW_BOOKID

给它起名字 SQL_DTFLOW_BOOKID

The variable data typeis STRING

variable data typeSTRING

So, you need make your SSIS neat, and the variables is sorted in understandable parts.

因此,您需要使您的 SSIS 整洁,并且变量按可理解的部分进行排序。

Try to make the variable name is SQL_{TASK NAME}_{VariableName}

尝试使变量名称为 SQL_{TASK NAME}_{VariableName}

  1. Make Expression for SQL_DTFLOW_FULLvariable, click on number 1, and start fill number 2. Make Your SQL Statements to be a correct SQL Statement using string block. String block usually using "Double Quote" at the beginning and the end. Concat the variables with the string block.
  1. SQL_DTFLOW_FULL变量创建表达式,单击数字 1,然后开始填充数字 2。使用字符串块使您的 SQL 语句成为正确的 SQL 语句。字符串块通常在开头和结尾使用“双引号”。用字符串块连接变量。

Expression

表达

Click evaluate Expression, to showing result, to make sure your query is correct, copy-paste the Query result at SSMS.

单击评估表达式,以显示结果,以确保您的查询正确,将查询结果复制粘贴到 SSMS。

Make sure by yourself that the variables is free from SQL Injection using your own logic. (Use your developer instinct)

使用您自己的逻辑确保变量不受 SQL 注入的影响。(利用你的开发者直觉)

  1. Open the Data Flow Task, open the OLE DB Source Editor by double click the item.
  1. 打开数据流任务,双击该项打开 OLE DB 源编辑器。

Data Flow

数据流

  • Select the Data Access Mode : SQL Command From Variable
  • Select the Variable Name : SQL_DTFLOW_FULL
  • Click Preview to make sure it works.
  • 选择数据访问模式: SQL Command From Variable
  • 选择变量名称: SQL_DTFLOW_FULL
  • 单击预览以确保它有效。

That is all, my way to prevent this SSIS failure case. Since I use this way, I never got that problem, you know, SSIS something is weird.

这就是我防止这种 SSIS 失败案例的方法。因为我使用这种方式,所以我从来没有遇到过这个问题,你知道,SSIS 的东西很奇怪。

To change the variable value, set it before Data Flow Task, the SQL Result of SQL_DTFLOW_FULLvariable will changed every you change your variable value.

要更改变量值,请将其设置在数据流任务之前,SQL_DTFLOW_FULL每次更改变量值时,变量的 SQL 结果都会更改。

回答by PanLondon

In my case the issue was that i had comments within the sql in the normal form of /* */ and i also had column aliases as "Column name" instead of [Column Name].

就我而言,问题是我在 sql 中以 /* */ 的正常形式进行了注释,并且我还有列别名作为“列名”而不是 [列名]。

Once i removed them it works.

一旦我删除了它们,它就起作用了。

Also try to have your parameter ? statement within the WHERE clause and not within the JOINS, that was part of the issue too.

也试着有你的参数?WHERE 子句中而不是 JOINS 中的语句,这也是问题的一部分。