如何处理 SSIS 包中存储过程返回的 NULL 值?

发布于 2024-11-04 06:16:18 字数 767 浏览 2 评论 0原文

我是 ssis 的新手,在使用 ssis 将包含 NULL 值的存储过程的结果加载到表中时遇到问题。我的情况如下:

步骤 1

在 Database1 上执行存储过程将返回完整结果集并放入 ADO 对象 User::CallResults

步骤 2

然后我循环遍历 User:: 的结果CallResults 映射 2 个变量:

Variable         Index ADO object Type     Nullable  
User::ID         0      Object              NO  
User::Result     1        Object              Yes    

步骤 3

然后在 Insert Row Into Database2 中获取每一行并执行“insert into dbo.myTable id, result value (?,?)”,

我将 ID 分别映射为 int,将 Result 分别映射为 long。

当我执行时,出现错误:

失败并出现以下错误:“提取文件时发生错误 结果转化为类型为(DBTYPE_I4)的变量”。可能的失败原因: 查询出现问题,“ResultSet”属性设置不正确, 参数未正确设置,或连接未正确建立。

似乎当 Result 中存在 null 时会出错。有什么建议可以让 ssis 允许空值吗?

I am a newbie to ssis and am having issues with the loading the results of a stored procedure that includes NULL values into a table with ssis. What I have is follows:

Step 1

Execute Stored Procedure on Database1 will return a Full Result Set and put into ADO object User::CallResults

Step 2

Then I Loop through the results of User::CallResults mapping 2 variables:

Variable         Index ADO object Type     Nullable  
User::ID         0      Object              NO  
User::Result     1        Object              Yes    

Step 3

Then in the Insert Row Into Database2 takes each row and executes "insert into dbo.myTable id, result values (?,?)"

I map ID as int and Result as long respectively.

When I execute I get the error:

failed with the following error: "An error occurred while extracting the
result into a variable of type (DBTYPE_I4)". Possible failure reasons:
Problems with the query, "ResultSet" property not set correctly,
parameters not set correctly, or connection not established correctly.

Seems like this it errors when there is a null in the Result. Any suggestions to make ssis allow nulls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

弱骨蛰伏 2024-11-11 06:16:18

您可以使用数据流任务实现问题中描述的流程。以下是使用存储过程获取数据然后插入表的分步描述,所有操作均在数据流任务内完成。此示例只是为了说明如何完成此操作,并且它仅使用 SQL Server 的单个实例。

分步过程:

  1. 创建两个名为 dbo.Sourcedbo.Destination 的表,并使用以下内容填充表 dbo.Source数据如屏幕截图#1所示。 脚本部分下提供了创建表脚本。

  2. 使用脚本部分下提供的脚本创建一个名为dbo.GetData的存储过程。

  3. 在 SSIS 包上,创建一个名为 StoredProcedure 的变量,如屏幕截图 #2 所示。该变量将包含存储过程执行语句。

  4. 在连接管理器中创建一个 OLE DB 连接 以连接到 SQL Server 实例。

  5. 在 SSIS 包的控制流选项卡上,放置一个数据流任务,如屏幕截图 #3 所示。

  6. 双击数据流任务以导航到数据流选项卡。在“数据流”选项卡内,放置一个 OLE DB Source 和一个 OLE DB Destination,如屏幕截图 #4 所示。

  7. 配置 OLE DB 源,如屏幕截图 #5 和 #6 所示。请注意,源正在使用在步骤 #3 中创建的变量。存储过程返回的数据将作为源输入。

  8. 配置“OLE DB 目标”,如屏幕截图 #7 和 #8 所示。这会将数据插入目标表中。

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

  10. 屏幕截图#10 显示包执行后表中的数据。请注意,目标表包含 NULL 值。这是可能的,因为列数量可以接受NULL值。但是,如果我们将 NULL 值传递给 ItemNumber 列,则包将会失败,因为该列不可为 null。

希望有帮助。

脚本:

CREATE TABLE [dbo].[Destination](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ItemNumber] [varchar](50) NOT NULL,
    [Qty] [int] 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] NULL,
CONSTRAINT [PK_Source] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

CREATE PROCEDURE [dbo].[GetData]
AS
BEGIN
    SET NOCOUNT ON;

    SELECT      Id
            ,   ItemNumber
            ,   Qty
    FROM        dbo.Source
END
GO

屏幕截图 #1:

1

屏幕截图 #2:

2

屏幕截图 #3:

3

屏幕截图 #4:

4

屏幕截图 #5 :

5

屏幕截图#6:

6

屏幕截图 #7:

7

屏幕截图 #8:

8

屏幕截图 #9:

< img src="https://i.sstatic.net/XDukn.png" alt="9">

屏幕截图 #10:

10

You can achieve the process that you have described in the question using Data Flow task. Here is a step by step description of fetching data using stored procedure and then inserting into a table, all done inside Data Flow task. This example is just to give an idea of how this can be done and it uses only a single instance of SQL Server.

Step-by-step process:

  1. Create two tables named dbo.Source and dbo.Destination and populate the table dbo.Source with data as shown in screenshot #1. Create table scripts are provided under Scripts section.

  2. Create a stored procedure named dbo.GetData using the script provided under Scripts section.

  3. On the SSIS package, create a variable named StoredProcedure as shown in screenshot #2. This variable will contain the stored procedure execution statement.

  4. Create an OLE DB Connection in the Connection manager to connect to the SQL Server instance.

  5. On the Control Flow tab of the SSIS package, place a Data Flow task as shown in screenshot #3.

  6. Double-click on the Data flow task to navigate to the Data Flow tab. Inside the Data Flow tab, place an OLE DB Source and an OLE DB Destination as shown in screenshot #4.

  7. Configure the OLE DB Source as shown in screenshots #5 and #6. Notice that the source is using the variable that was created in step #3. The data returned by the stored procedure will be the source input.

  8. Configure the 'OLE DB Destination` as shown in screenshots #7 and #8. This will insert the data into the destination table.

  9. Screenshot #9 displays sample package execution.

  10. Screenshot #10 shows the data in the tables after the package execution. Note that the destination table contains NULL values. This is possible because the column Qty can accept NULL values. However, if we had passed NULL values to the ItemNumber column, the package would have failed because the column is non-nullable.

Hope that helps.

Scripts:
.

CREATE TABLE [dbo].[Destination](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ItemNumber] [varchar](50) NOT NULL,
    [Qty] [int] 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] NULL,
CONSTRAINT [PK_Source] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

CREATE PROCEDURE [dbo].[GetData]
AS
BEGIN
    SET NOCOUNT ON;

    SELECT      Id
            ,   ItemNumber
            ,   Qty
    FROM        dbo.Source
END
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

Screenshot #9:

9

Screenshot #10:

10

烂柯人 2024-11-11 06:16:18

这并不是真正的答案,但由于代码无法在注释中很好地格式化,因此我将其放在这里。

您是否意识到,如果您的数据库位于同一服务器上,您可以执行以下操作:

INSERT INTO 
    database1.dbo.Results
EXEC 
    database2.dbo.SampleStoredProcedure @param1, @param2, @param3

Not really an answer, but since code can't be formatted nicely in a comment I'm putting it here.

Do you realize, that if your databases are on the same server, you can do this:

INSERT INTO 
    database1.dbo.Results
EXEC 
    database2.dbo.SampleStoredProcedure @param1, @param2, @param3
原来分手还会想你 2024-11-11 06:16:18

将此逻辑放入数据流中。完成后,只需进行批量插入,它应该允许空值通过。

SSIS - 无法在 BULK INSERT 中为空白字段插入 NULL

Put this logic into a Data Flow. After that is accomplished, just do a bulk insert and it should allow the nulls through.

SSIS - Unable to insert NULL for Blank fields in BULK INSERT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文