SSIS查表问题

发布于 2024-11-30 04:56:33 字数 693 浏览 0 评论 0原文

我正在使用 SSIS (SQL Server 2008) 从表中查找值。我想知道以下是否可能:

问题 1

查找表 1

Manager_Name    EMP_UNIT    Job_Profile_ID
AAA             SALES       27
BBB             HR          28
AAA             SALES       29

我必须在查找表中传递“Manager_Name”和“EMP_UNIT”值并获取 作业配置文件 ID,采用逗号分隔格式。 就像我将传递值“AAA”和“SALES”,我希望返回值为 27,29 我该怎么做?

问题 2

查找表 2

Job_Profile_ID  Job_Name
27              Jr. Salesman
28              Sales Manager
29              Sr. Salesman

我必须以逗号分隔格式 (27, 29) 传递作业配置文件 ID 值,并且 我想要以逗号分隔格式返回值(初级销售员、高级销售员)。 这在SSIS中可能吗?

环境详情: Windows 2008 服务器上的 MS SQL Server 集成服务(出价)2008

感谢任何帮助,

谢谢

I am using SSIS (SQL Server 2008) to lookup values from a table. I wanted to know if the following are possible:

Question 1

Lookup Table 1

Manager_Name    EMP_UNIT    Job_Profile_ID
AAA             SALES       27
BBB             HR          28
AAA             SALES       29

I have to pass the ‘Manager_Name ‘and ‘EMP_UNIT ‘value in the Lookup table and fetch
Job Profile ID in Comma separated format.
Like I will pass the Values ‘AAA’ and ‘SALES’ and I want the return value as 27,29
How will I do this?

Question 2

Lookup Table 2

Job_Profile_ID  Job_Name
27              Jr. Salesman
28              Sales Manager
29              Sr. Salesman

I have to pass the Job Profile ID values in Comma separated format (27, 29) and
I want the return value in Comma separated format (Jr. Salesman, Sr. Salesman).
Is this possible in SSIS?

Environment details:
MS SQL Server integration services (bids) 2008 on windows 2008 server

any help is appreciated,

Thanks

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

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

发布评论

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

评论(2

⒈起吃苦の倖褔 2024-12-07 04:56:33

如果我正确理解您的问题,您需要给定经理和员工单位组合的职位名称列表。如果是这种情况,您需要一个存储过程,它可以以逗号分隔值给出作业名称列表。

在我看来,两个查找转换任务似乎有点矫枉过正。

根据问题中提供的数据,创建和填充表格部分提供了示例数据。

创建以下存储过程 dbo.GetManagerJobProfiles,它将采用 Manager_NameEmp_Unit 作为输入参数,并返回给定的匹配作业名称列表参数作为逗号分隔列表。此存储过程使用 FOR XML 子句来生成逗号分隔值。由于逗号附加在开头,因此我们必须截断列表中的第一个逗号。因此,使用 substring 函数来完成这项工作,以提供更清晰的输出。

CREATE PROCEDURE dbo.GetManagerJobProfiles
(
        @Manager_Name   NVARCHAR(80)
    ,   @Emp_Unit       NVARCHAR(80)
)
AS
BEGIN   
    SET NOCOUNT ON;

    SELECT SUBSTRING(
            (
                SELECT          ', ' + J.Job_Name
                FROM            dbo.Managers        M
                LEFT OUTER JOIN dbo.Jobs            J
                ON              M.Job_Profile_Id    = J.Job_Profile_Id
                WHERE           M.Manager_Name      = @Manager_Name
                AND             M.Emp_Unit          = @Emp_Unit
                FOR XML PATH ('')
            )
        , 3, 1000) AS Job_Name
END
GO

屏幕截图 #1 显示了表 dbo.Managersdbo.Jobs 中的示例数据。

Table data

屏幕截图 #2 显示了两组不同参数的存储过程输出。

存储过程输出

如果我必须在 SSIS 包中使用它,我将使用以下命令获取不同 Manager_Name 和 Emp_Unit 组合的列表执行 SQL 任务并将结果集填充到数据类型对象的 SSIS 包变量中。

然后,我将使用 Foreach 循环容器Foreach ADO 枚举器 循环访问对象变量。在 Foreach 循环容器中,我将放置一个数据流任务。在数据流任务中,我将放置一个OLE DB Source,它将使用存储过程作为源。对于循环通过的每个 Manager_Name 和 Emp_Unit 组合,这些值将作为参数传递到 OLE DB 源以获取作业名称值。

希望有帮助。

创建并填充表格此结构基于问题中提供的数据。

CREATE TABLE [dbo].[Jobs](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Job_Profile_Id] [int] NOT NULL,
    [Job_Name] [nvarchar](40) NOT NULL,
CONSTRAINT [PK_Jobs] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Managers](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Manager_Name] [nvarchar](80) NOT NULL,
    [Emp_Unit] [nvarchar](80) NOT NULL,
    [Job_Profile_Id] [int] NOT NULL,
CONSTRAINT [PK_Managers] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

INSERT INTO dbo.Managers (Manager_Name, Emp_Unit, Job_Profile_Id) VALUES
    ('AAA', 'SALES', 27),
    ('BBB', 'HR', 28),
    ('AAA', 'SALES', 29);

INSERT INTO dbo.Jobs(Job_Profile_Id, Job_Name) VALUES
    (27, 'Jr. Salesman'),
    (28, 'Sales Manager'),
    (29, 'Sr. Salesman');

If I understand your question correctly, you need the list of job names for a given manager and employee unit combination. If that is the case, you need a stored procedure that can give the list of job names in a comma separated value.

In my opinion, two lookup transformation tasks seems to be a overkill.

Based on the data provided in the question, the section Create and populate tables provide the sample data.

Create the following stored procedure dbo.GetManagerJobProfiles that will take Manager_Name and Emp_Unit as input parameters and will return the list of matching job names for the given parameters as a comma separated list. This stored procedure uses FOR XML clause to generate the comma separated values. Since the comma is appended at the beginning, we have to truncate the first comma from the list. Hence, the substring function is used to do that job to give a cleaner output.

CREATE PROCEDURE dbo.GetManagerJobProfiles
(
        @Manager_Name   NVARCHAR(80)
    ,   @Emp_Unit       NVARCHAR(80)
)
AS
BEGIN   
    SET NOCOUNT ON;

    SELECT SUBSTRING(
            (
                SELECT          ', ' + J.Job_Name
                FROM            dbo.Managers        M
                LEFT OUTER JOIN dbo.Jobs            J
                ON              M.Job_Profile_Id    = J.Job_Profile_Id
                WHERE           M.Manager_Name      = @Manager_Name
                AND             M.Emp_Unit          = @Emp_Unit
                FOR XML PATH ('')
            )
        , 3, 1000) AS Job_Name
END
GO

Screenshot #1 shows the sample data in the tables dbo.Managers and dbo.Jobs.

Table data

Screenshot #2 shows the stored procedure output for two different sets of parameters.

Stored procedure output

If I have to use this in an SSIS package, I would get the list of distinct Manager_Name and Emp_Unit combinations using an Execute SQL Task and will populate the resultset into an SSIS Package variable of data type Object.

I will then loop through the object variable using Foreach loop container with Foreach ADO enumerator. Within the Foreach loop container, I will place a Data Flow Task. Within the data flow task, I will place an OLE DB Source which will use the stored procedure as the source. For each Manager_Name and Emp_Unit combination being looped through, the values will be passed as parameters to the OLE DB Source to fetch the Job name values.

Hope that helps.

Create and populate tables: This structure is based on the data provided in the question.

CREATE TABLE [dbo].[Jobs](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Job_Profile_Id] [int] NOT NULL,
    [Job_Name] [nvarchar](40) NOT NULL,
CONSTRAINT [PK_Jobs] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Managers](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Manager_Name] [nvarchar](80) NOT NULL,
    [Emp_Unit] [nvarchar](80) NOT NULL,
    [Job_Profile_Id] [int] NOT NULL,
CONSTRAINT [PK_Managers] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

INSERT INTO dbo.Managers (Manager_Name, Emp_Unit, Job_Profile_Id) VALUES
    ('AAA', 'SALES', 27),
    ('BBB', 'HR', 28),
    ('AAA', 'SALES', 29);

INSERT INTO dbo.Jobs(Job_Profile_Id, Job_Name) VALUES
    (27, 'Jr. Salesman'),
    (28, 'Sales Manager'),
    (29, 'Sr. Salesman');
思慕 2024-12-07 04:56:33

我可以帮助你解决问题1,但我不确定你的第二个问题。

要查找单个列,您通常会使用 LookUp 组件,相应地匹配搜索列并返回相应的值。

SSIS LookUp component

但是,由于您期望返回多个值,因此这不会是您的选择,因为LookUp 组件只会返回第一个匹配的值。

要在每个起始行获得多个结果,您必须使用两个源控件(一个用于主表,另一个用于查找表)以及合并联接控件

SSIS 合并联接控件

合并联接控件将使您能够合并两组数据,但是当存在多个匹配项时,您将获得多行。因此,在您的示例中,AAA 和 Sales 作为输入将提供两行;

  • AAA,Sales,27
  • AAA,Sales,29

然后您可以将此数据传递到目标表。

对于第二个问题,在 SQL Server 中查询并期望使用逗号分隔值的综合响应是不常见的。

如果您能够利用多个搜索和结果行的数据(即首先搜索 id 27 的行,然后搜索 id 29 的行),那么使用查找任务就足够了。

否则,可能会进行第二遍(新数据流任务)并使用第一遍临时表将数据(使用脚本?)合并为您所需的 csv 格式。

I can help you with question 1, but i'm not sure about your second question.

To look up a single column, you would normally use the LookUp component, matching the search columns accordingly and returning the corresponding value.

SSIS LookUp component

However, as you are expecting multiple values to be returned, this is not going to be an option for you as the LookUp component will only return the first matching value.

To achieve multiple results per starting row, you will have to use two source controls (one for main table and other for lookup table) along with the Merge Join control

SSIS Merge Join control

The merge join control will enable you to combine both sets of data, however when multiple matches exist you will get multiple rows. So in your example, AAA and Sales as input will provide two rows;

  • AAA, Sales, 27
  • AAA, Sales, 29

You can then pass this data to a destination table.

For the second question, it is unusual to query and expect a consolidated response with comma separated values in SQL Server.

If you able to utilise the data with multiple search and result rows (i.e. search for row id 27 first and then row with id 29) then use the lookup task will suffice.

Otherwise, maybe make a second pass (new data flow task) and use the first pass temporary table to consolidate the data (using a script?) into your desired csv format.

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