SSIS使用日期时间输入参数执行SQL查询错误

发布于 2024-10-27 18:59:13 字数 690 浏览 2 评论 0原文

我花了几天时间试图解决这个问题。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画中仙 2024-11-03 18:59:13

我知道您已经找到了问题的答案。但是,我想在此澄清一下,您使用 OleDb 连接执行的以下查询是有效的,并且它确实在 SSIS 中的执行 SQL 任务 中工作代码> 包。

INSERT INTO dbo.table1 (DateCol, OtherCol, OtherCol1) 
SELECT ?, SourceCol1, SourceCol2 FROM dbo.SourceTable

以下示例显示了使用 SSIS 2005 (Business Intelligence Development Studio (BIDS) 2005) 成功实现上述查询

分步过程:

  1. 使用脚本部分下提供的脚本创建两个名为dbo.Sourcedbo.Destination的表。使用屏幕截图 #1 中所示的数据填充表 dbo.Source。表 dbo.Destination 最初为空,并将使用执行 SQL 任务填充源数据。

  2. 在 SSIS 包上,在连接管理器 中创建一个名为 SQLServer 的 OLE DB 连接,指向您首选的 SQL Server 实例。请参阅屏幕截图 #2

  3. 在 SSIS 包上,创建一个名为 RecordDate 的变量,如屏幕截图 #3 所示。

  4. 在 SSIS 包上,放置一个执行 SQL 任务,如屏幕截图 #4 所示。配置任务,如屏幕截图 #5 和 #6 所示。

  5. 屏幕截图 #7 显示示例包执行。

  6. Screenshot #8 显示包执行后表 dbo.Sourcedbo.Destination 中的数据。

希望有帮助。

脚本:

CREATE TABLE [dbo].[Destination](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [DateValue] [datetime] NOT NULL,
    [ItemNumber] [varchar](50) NOT NULL,
    [Qty] [int] NOT NULL,
CONSTRAINT [PK_Destination] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Source](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ItemNumber] [varchar](50) NOT NULL,
    [Qty] [int] NOT NULL,
CONSTRAINT [PK_Source] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

屏幕截图 #1:

1

屏幕截图 #2:

2

屏幕截图 #3:

3

屏幕截图 #4:

4

屏幕截图 #5 :

5

屏幕截图#6:

6

屏幕截图 #7:

7

屏幕截图#8:

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 in SSIS package.

INSERT INTO dbo.table1 (DateCol, OtherCol, OtherCol1) 
SELECT ?, SourceCol1, SourceCol2 FROM dbo.SourceTable

Following example shows a successful implementation of the above mentioned query using SSIS 2005 (Business Intelligence Development Studio (BIDS) 2005)

Step-by-step process:

  1. Create two tables named dbo.Source and dbo.Destination using the scripts provided under the Scripts section. Populate the table dbo.Source with data as shown in screenshot #1. Table dbo.Destination will initially be empty and will be populated with source data using an Execute SQL Task.

  2. 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.

  3. On the SSIS package, create a variable named RecordDate as shown in screenshot #3.

  4. On the SSIS package, place an Execute SQL Task as shown in screenshot #4. Configure the task as shown in screenshots #5 and #6.

  5. Screenshot #7 shows sample package execution.

  6. Screenshot #8 shows data in the tables dbo.Source and dbo.Destination after package execution.

Hope that helps.

Scripts:
.

CREATE TABLE [dbo].[Destination](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [DateValue] [datetime] NOT NULL,
    [ItemNumber] [varchar](50) NOT NULL,
    [Qty] [int] NOT NULL,
CONSTRAINT [PK_Destination] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Source](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ItemNumber] [varchar](50) NOT NULL,
    [Qty] [int] NOT NULL,
CONSTRAINT [PK_Source] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

Screenshot #1:

1

Screenshot #2:

2

Screenshot #3:

3

Screenshot #4:

4

Screenshot #5:

5

Screenshot #6:

6

Screenshot #7:

7

Screenshot #8:

8

勿忘初心 2024-11-03 18:59:13

您需要确保您的 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.

要走干脆点 2024-11-03 18:59:13

您可以在这里找到该表达式:
在此处输入图像描述

You can find the expression here:
enter image description here

绿光 2024-11-03 18:59:13
Objective:
To update datetime field (date+time) in parameter in sql task editor of SSIS package.

Description:
Step01: Created a variable having datatype as string.
e.g: 
Variable name: ReconcileStartDateTime
Data Type: String

Step02: Assign value to a variable. create 'Execute SQL Task'
General:
SQL Command-> select cast(getdate() as nvarchar(100)) as StartDateTime
ResultSet-> Single Row
ByPassprepare-> True

Result set tab:
Result Name: StartDateTime
Variable Name: User::ReconcileStartDateTime

Step 03: Create Execute SQL Task and use query as below:
SqlStatement: Update OrderDetail set StartDate = cast(? as datetime) where ID= 101;
Parameter mapping: click ADD button, set variable name User::ReconcileStartDateTime; Datatype as Nvarchar; Parameter to 0

Result: When execute the SSIS package, datetime is set accordingly. SQL Profile would help to see the output.
Objective:
To update datetime field (date+time) in parameter in sql task editor of SSIS package.

Description:
Step01: Created a variable having datatype as string.
e.g: 
Variable name: ReconcileStartDateTime
Data Type: String

Step02: Assign value to a variable. create 'Execute SQL Task'
General:
SQL Command-> select cast(getdate() as nvarchar(100)) as StartDateTime
ResultSet-> Single Row
ByPassprepare-> True

Result set tab:
Result Name: StartDateTime
Variable Name: User::ReconcileStartDateTime

Step 03: Create Execute SQL Task and use query as below:
SqlStatement: Update OrderDetail set StartDate = cast(? as datetime) where ID= 101;
Parameter mapping: click ADD button, set variable name User::ReconcileStartDateTime; Datatype as Nvarchar; Parameter to 0

Result: When execute the SSIS package, datetime is set accordingly. SQL Profile would help to see the output.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文