SSIS使用日期时间输入参数执行SQL查询错误
我花了几天时间试图解决这个问题。我有一个 SSIS 包,在序列容器内有 2 个执行 SQL 任务,一个是从表中简单删除,下一个是简单插入(删除先于插入)。删除工作正常,因此连接等正常。
插入失败,并显示以下模糊且无用的消息。
failed with the following error: "Syntax error, permission violation, or other nonspecific error". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
该插入有 1 个输入参数,该参数是绑定到设置为 01/01/2011 的日期时间变量的日期。当我更换?在具有硬编码日期的 SQL 任务查询中,任务有效。我还查看了插入任务上预执行事件断点处的局部变量,并且变量正常。
我还启动了 SQL Profiler 会话,我看到删除查询命中了数据库,但没有插入任何内容(当它使用输入参数时)。
我正在使用 Visual Studio 2005 Pro SP1(不是我的选择)和 SQL Server 2005 SP3。
问候
马克
I have spent a few days trying to fix this problem. I have a SSIS package with 2 execute SQL tasks within a sequence container, one is a simple delete from table and the next one an simple insert the delete precedes the insert. The delete works fine so the connection etc is ok.
The Insert is failing with the following vague and unhelpful message.
failed with the following error: "Syntax error, permission violation, or other nonspecific error". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
The insert has 1 input parameter which is a date which is bound to a datetime variable set to 01/01/2011. When I replace the ? in the sql task query with a hard coded date the task works. I have also looked at the locals at a pre-execute event break point on the insert task and the variable is ok.
I also fired up a SQL Profiler session and I see the delete query hitting the DB but nothing for the insert (when it uses the input parameter).
I am using Visual Studio 2005 Pro SP1 (Not my choice) and SQL Server 2005 SP3.
Regards
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道您已经找到了问题的答案。但是,我想在此澄清一下,您使用 OleDb 连接执行的以下查询是有效的,并且它确实在
SSIS
中的执行 SQL 任务
中工作代码> 包。以下示例显示了使用 SSIS 2005 (
Business Intelligence Development Studio (BIDS) 2005
) 成功实现上述查询分步过程:
使用脚本部分下提供的脚本创建两个名为
dbo.Source
和dbo.Destination
的表。使用屏幕截图 #1 中所示的数据填充表dbo.Source
。表dbo.Destination
最初为空,并将使用执行 SQL 任务填充源数据。在 SSIS 包上,在
连接管理器
中创建一个名为 SQLServer 的 OLE DB 连接,指向您首选的 SQL Server 实例。请参阅屏幕截图 #2。在 SSIS 包上,创建一个名为 RecordDate 的变量,如屏幕截图 #3 所示。
在 SSIS 包上,放置一个
执行 SQL 任务
,如屏幕截图 #4 所示。配置任务,如屏幕截图 #5 和 #6 所示。屏幕截图 #7 显示示例包执行。
Screenshot #8 显示包执行后表
dbo.Source
和dbo.Destination
中的数据。希望有帮助。
脚本:
。
屏幕截图 #1:
屏幕截图 #2:
屏幕截图 #3:
屏幕截图 #4:
屏幕截图 #5 :
屏幕截图#6:
屏幕截图 #7:
屏幕截图#8:
I know that you have found an answer to your question. However, I would like to clarify here that the following query you were executing using OleDb connection is valid and it does work within the
Execute SQL Task
inSSIS
package.Following example shows a successful implementation of the above mentioned query using SSIS 2005 (
Business Intelligence Development Studio (BIDS) 2005
)Step-by-step process:
Create two tables named
dbo.Source
anddbo.Destination
using the scripts provided under the Scripts section. Populate the tabledbo.Source
with data as shown in screenshot #1. Tabledbo.Destination
will initially be empty and will be populated with source data using an Execute SQL Task.On the SSIS package, create an OLE DB Connection named SQLServer in the
Connections Managers
pointing to a SQL Server instance of your preference. Refer screenshot #2.On the SSIS package, create a variable named RecordDate as shown in screenshot #3.
On the SSIS package, place an
Execute SQL Task
as shown in screenshot #4. Configure the task as shown in screenshots #5 and #6.Screenshot #7 shows sample package execution.
Screenshot #8 shows data in the tables
dbo.Source
anddbo.Destination
after package execution.Hope that helps.
Scripts:
.
Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
您需要确保您的 SQL 语句的类型正确,以便根据您的连接管理器进行参数化。
如果您使用的是 OLE DB - 您的插入语句需要是某种类似
INSERT INTO Tbl(col) VALUES (?)
结果集应该是“None”(因为您的 INSERT 上没有任何可返回的内容) ),并且“参数映射”选项卡应该有一个参数(或与您拥有的一样多的?,并且参数名称应该(对于 OLE DB)从 0 开始,然后是 1, 2 ... ,n。如果您使用的是对于 ADO 连接,您必须将参数命名为 Param1、Param2、... ParamN。
您可以在此处查看将变量传递到相应连接管理器的参考:http://technet.microsoft.com/en-us/library/cc280502.aspx
根据您的评论给出正确答案:
您不能在此类查询中使用参数映射。但有一个简单的方法可以做到这一点。
创建一个新变量 sqlCommand(字符串类型)。在变量列表中单击它,然后按 F4 查看属性。将“EvaluateAsExpression”更改为true,然后单击表达式框以打开表达式窗口。在这里以如下格式进行查询
"INSERT INTO tbl(dateCol,intCol,charCol) SELECT CONVERT(DATETIME,'" + (DT_STR,20,1252)@[User::dateVar] + "', 104) AS dateCol, intCol, charCol from anotherTbl"
当您单击“评估表达式”时,您将看到正在形成的有效 SQL 语句,您可以使用它。
现在返回执行 SQL 任务,删除参数映射。关闭对话框,单击执行 SQL 任务,按 F4,找到表达式行,单击 ... 并在属性“SqlStatementSource”上添加一个表达式,其中表达式为 @[User::sqlCommand](或您为变量命名的任何内容) )。
现在运行该任务,它应该可以正常工作。
You need to make sure your SQL Statement is of the correct type to be parameterized according to your connection manager.
If you're using OLE DB - your Insert statement needs to be of some kind like
INSERT INTO Tbl(col) VALUES (?)
Resultset should be "None" (As there's nothing to return on your INSERT), and the Parameter Mapping tab should have a single parameter (or as many as ?'s you have, and Parameter Name should (for OLE DB) start on 0, then 1, 2 ... ,n. If you were using an ADO connection you would have to name the parameters Param1, Param2, ... ParamN.
You can see the reference for passing variables to the respective connection managers here: http://technet.microsoft.com/en-us/library/cc280502.aspx
Proper answer as per your comment:
You cannot use a parameter mapping in a query of that kind. But there is an easy way to do it.
Make a new variable sqlCommand (type string). click it in the list of variables, and press F4 to see properties. Change "EvaluateAsExpression" to true, and click the expression box to get up the expression window. In here make your query in a format like this
"INSERT INTO tbl(dateCol,intCol,charCol) SELECT CONVERT(DATETIME,'" + (DT_STR,20,1252)@[User::dateVar] + "',104) AS dateCol, intCol, charCol from anotherTbl"
When you click Evaluate Expression you'll see a valid SQL statement being formed that you can use.
Now go back to the Execute SQL task, remove the parameter mapping. Close the dialog box, click the Execute SQL task, press F4, find the expressions line, click the ... and add an expression on the Property "SqlStatementSource" with expression @[User::sqlCommand] (or whatever you named your variable).
Now run the task and it should work without a problem.
您可以在这里找到该表达式:
You can find the expression here: