Cross Join Help

  • I have a massive table (company, plant, resource, part number) that I need to create a from part number, to part number for every part within "Company, Plant, Resource" combo.

    Trying to figure out what the best method would be.. using a cursor will put the load on one CPU and take way too long.

    Ideas?

  • A cursor is not the right approach for this scenario. Allthough it is not very clear what your situation is, it sounds like you can just insert the results from two tables joind together into a new table.

    Table_A holds the company, plant, part, etc. values

    Table_B holds the 'from part', 'to part', and maybe some more relevant columns

    The query would look like this:

    SELECT {specify the required columns here}

    INTO {new_table}

    FROM Table_A

    INNER JOIN Table_B

    ON Table_A.part = Table_B.from_part

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • dwilliscp (7/25/2014)


    I have a massive table (company, plant, resource, part number) that I need to create a from part number, to part number for every part within "Company, Plant, Resource" combo.

    Trying to figure out what the best method would be.. using a cursor will put the load on one CPU and take way too long.

    Ideas?

    SELECT company, plant, resource, MIN(partno) as [from part number], MAX(part number) as [to part number]

    FROM table

    GROUP BY company, plant, resource


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • dwilliscp (7/25/2014)


    I have a massive table (company, plant, resource, part number) that I need to create a from part number, to part number for every part within "Company, Plant, Resource" combo.

    Trying to figure out what the best method would be.. using a cursor will put the load on one CPU and take way too long.

    Ideas?

    You have one table... lets call it Resource_Mat. PK Company, Plant, Resource, Part Number. What I need to do is ..

    Get the first row from Resource_mat and for every row in the table create a data set that contains:

    Company, Plant, Resource, [Selected rows' part number], [current rows' part number]

    The only restriction is that the loop needs to only work on where Comany + Plant + Resournce matches between selected row and current row.

    This is why I thought about using a cursor.. but the data set is so large, and SQL will throw it all on one CPU... I expect it to take too long.

    The result of the above query then gets updated/inserted into a table that holds this information about from part .. to part and then we keep up how long it takes to transitition from one part number to a different part number.

  • I broke down and wrote it as a Cursor... Still running so I do not know if my code is correct yet... I will create a table and some dummy data in the next post.

    declare @plant as varchar(4)

    , @Resource_Code as varchar(8)

    , @Material_Number as varchar(18)

    set nocount on;

    declare i_cursor insensitive cursor

    for Select Plant, Resource_Code, Material_Number From Resource_Mat

    open i_Cursor

    while @@fetch_status = 0

    begin

    insert into trans_Matrix(Company, Plant, Resource_Code, From_Material, To_Material)

    select 'SPOR' as Company

    , Plant

    , Resource_Code

    , @Material_Number as From_Material

    , Material_Number as To_Material

    from Resource_Mat

    where @Plant = plant and @resource_code = resource_code

    end

    fetch next from i_Cursor into @Plant, @Resource_Code, @Material_Number

    close i_Cursor

    deallocate i_cursor

    set nocount off;

    go

  • CREATE TABLE [dbo].[Resource_Mat](

    [Plant] [varchar](4) NOT NULL,

    [Material_number] [varchar](18) NOT NULL,

    [Production_Version] [varchar](4) NOT NULL,

    [Resource_Code] [varchar](8) NOT NULL,

    [Std_Batch_Size] [varchar](18) NULL,

    [UOM] [varchar](3) NULL,

    [Std_Run_Time] [varchar](14) NULL,

    [Time_UOM] [varchar](3) NULL,

    [Batch_Min] [varchar](17) NULL,

    [Batch_Max] [varchar](17) NULL,

    [Stocking_Strategy] [varchar](2) NULL,

    [Trans_Type] [varchar](2) NULL,

    [Recipe_Status] [varchar](4) NULL,

    [Short_Description] [varchar](40) NULL,

    [Rounding_Value] [varchar](17) NULL,

    [Distribution_Key] [varchar](4) NULL,

    CONSTRAINT [PK_Resource_Mat] PRIMARY KEY CLUSTERED

    (

    [Plant] ASC,

    [Material_number] ASC,

    [Production_Version] ASC,

    [Resource_Code] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

  • dwilliscp (7/25/2014)


    dwilliscp (7/25/2014)


    I have a massive table (company, plant, resource, part number) that I need to create a from part number, to part number for every part within "Company, Plant, Resource" combo.

    Trying to figure out what the best method would be.. using a cursor will put the load on one CPU and take way too long.

    Ideas?

    You have one table... lets call it Resource_Mat. PK Company, Plant, Resource, Part Number. What I need to do is ..

    Get the first row from Resource_mat and for every row in the table create a data set that contains:

    Company, Plant, Resource, [Selected rows' part number], [current rows' part number]

    The only restriction is that the loop needs to only work on where Comany + Plant + Resournce matches between selected row and current row.

    This is why I thought about using a cursor.. but the data set is so large, and SQL will throw it all on one CPU... I expect it to take too long.

    The result of the above query then gets updated/inserted into a table that holds this information about from part .. to part and then we keep up how long it takes to transitition from one part number to a different part number.

    This is NOT the time to use a cursor. You really need to change the way you think about data. You have to think about what you want to do to a column, not a row.

    If you can post ddl and sample data along with desired output we can show you how to do this in a set based manner.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8260', N'0001', N'SIL', N'15.000', N'KG', N'0.100', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8260', N'0001', N'APAG', N'1000.000', N'KG', N'4.000', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'400.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8261', N'0001', N'APAG', N'1000.000', N'KG', N'4.138', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'600.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8261', N'0001', N'SIL', N'15.000', N'KG', N'0.100', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'21E-4440', N'0002', N'SIL', N'1000.000', N'KG', N'0.250', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8260', N'0001', N'SIL', N'1000.000', N'KG', N'1.200', N'H', N'0.000', N'999999999.000', N'FX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8261', N'0001', N'APAG', N'1000.000', N'KG', N'3.154', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'600.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'21E-4440', N'0001', N'SIL', N'985.000', N'KG', N'0.250', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'KG', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8262', N'1', N'SIL', N'1018.000', N'KG', N'0.250', N'STD', N'0.000', N'99999999.000', N'EX', N'2', N'4', N'KG', N'18.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8263', N'1', N'SIL', N'208.000', N'KG', N'0.100', N'STD', N'0.000', N'99999999.000', N'60', N'2', N'4', N'kg', N'8.000', N'')

  • Your insert statements don't work. Here is the results.

    String or binary data would be truncated.

    The statement has been terminated.

    Msg 8152, Level 16, State 2, Line 30

    String or binary data would be truncated.

    The statement has been terminated.

    Msg 8152, Level 16, State 2, Line 31

    String or binary data would be truncated.

    The statement has been terminated.

    Msg 8152, Level 16, State 2, Line 32

    String or binary data would be truncated.

    The statement has been terminated.

    Msg 8152, Level 16, State 2, Line 33

    String or binary data would be truncated.

    The statement has been terminated.

    (1 row(s) affected)

    (1 row(s) affected)

    (1 row(s) affected)

    (1 row(s) affected)

    (1 row(s) affected)

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I used APEX to create the inserts.. and then find and replace to turn it into dummy data.. will just key in some data later this afternoon when I get a chance.

  • dwilliscp (7/25/2014)


    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8260', N'0001', N'SIL', N'15.000', N'KG', N'0.100', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8260', N'0001', N'APAG', N'1000.000', N'KG', N'4.000', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'400.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8261', N'0001', N'APAG', N'1000.000', N'KG', N'4.138', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'600.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'11C-8261', N'0001', N'SIL', N'15.000', N'KG', N'0.100', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'CHANC', N'21E-4440', N'0002', N'SIL', N'1000.000', N'KG', N'0.250', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8260', N'0001', N'SIL', N'1000.000', N'KG', N'1.200', N'H', N'0.000', N'999999999.000', N'FX', N'2', N'4', N'', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8261', N'0001', N'APAG', N'1000.000', N'KG', N'3.154', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'', N'600.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'21E-4440', N'0001', N'SIL', N'985.000', N'KG', N'0.250', N'H', N'0.000', N'999999999.000', N'EX', N'2', N'4', N'KG', N'0.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8262', N'1', N'SIL', N'1018.000', N'KG', N'0.250', N'STD', N'0.000', N'99999999.000', N'EX', N'2', N'4', N'KG', N'18.000', N'')

    INSERT INTO [dbo].[Resource_Mat] ([Plant], [Material_number], [Production_Version], [Resource_Code], [Std_Batch_Size], [UOM], [Std_Run_Time], [Time_UOM], [Batch_Min], [Batch_Max], [Stocking_Strategy], [Trans_Type], [Recipe_Status], [Short_Description], [Rounding_Value], [Distribution_Key]) VALUES (N'LEX', N'11C-8263', N'1', N'SIL', N'208.000', N'KG', N'0.100', N'STD', N'0.000', N'99999999.000', N'60', N'2', N'4', N'kg', N'8.000', N'')

    NONE of your columns in the DDL provided are declared using NVARCHAR, all are VARCHAR. Why are all of your string values preceded by N to indicate UNICODE string values?

  • dwilliscp (7/25/2014)


    I have a massive table (company, plant, resource, part number) that I need to create a from part number, to part number for every part within "Company, Plant, Resource" combo.

    Trying to figure out what the best method would be.. using a cursor will put the load on one CPU and take way too long.

    Ideas?

    Define "Massive" because that's important here. A "Control Cursor" (as opposed to RBAR) may be appropriate here.

    Also, I'd personally like to know what it is you mmean by "from" and "to" part numbers because it doesn't make sense to me to reference a toilet paper holder with hot water heater unless they're attached to each other and in the same bathroom.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/25/2014)


    dwilliscp (7/25/2014)


    I have a massive table (company, plant, resource, part number) that I need to create a from part number, to part number for every part within "Company, Plant, Resource" combo.

    Trying to figure out what the best method would be.. using a cursor will put the load on one CPU and take way too long.

    Ideas?

    Define "Massive" because that's important here. A "Control Cursor" (as opposed to RBAR) may be appropriate here.

    Also, I'd personally like to know what it is you mmean by "from" and "to" part numbers because it doesn't make sense to me to reference a toilet paper holder with hot water heater unless they're attached to each other and in the same bathroom.

    Jeff... they are attached... Ok we have a file that shows all resources in a plant, and the materials that we use in them, during production. What we are trying to do, since our shop floor planning software does not do it, is to go in and generate a list of how long it takes to change a resource from producing part A to part B. So I need to take the list of parts, for each company + plant + resource... and create the from part ... to part. Then we can go in and assign how long it will take to transition from part A to Part B... before we can start to produce part B. Got it? The cursor... that I posted on friday.. ran until I killed it on Sunday Morning... and I was running for only one plant.. and parts that started with Z. This was a list of 362 rows.

  • Once we have sample data to work with it should be a straight forward approach.

    It looks like you're looking for the setup time per resource and product.

    If that's the case you'll also need the business hours in order to ignore off-time (e.g. breaks or shutdown time).

    Furthermore, the results need to be analyzed further to take additional down time into account (e.g. waiting for raw material).

    @jeff: the resource in a plant is the bathroom and the time is needed between the "usage of the toilet paper holder" ("part A") and the "hot water heater" ("Part B"). The "setup" that needs to be done is flush the toilet, put on your trousers, close toilet lid, leave the toilet and approach the sink to wash your hands 😉



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • dwilliscp (7/27/2014)


    Jeff Moden (7/25/2014)


    dwilliscp (7/25/2014)


    I have a massive table (company, plant, resource, part number) that I need to create a from part number, to part number for every part within "Company, Plant, Resource" combo.

    Trying to figure out what the best method would be.. using a cursor will put the load on one CPU and take way too long.

    Ideas?

    Define "Massive" because that's important here. A "Control Cursor" (as opposed to RBAR) may be appropriate here.

    Also, I'd personally like to know what it is you mean by "from" and "to" part numbers because it doesn't make sense to me to reference a toilet paper holder with hot water heater unless they're attached to each other and in the same bathroom.

    Jeff... they are attached... Ok we have a file that shows all resources in a plant, and the materials that we use in them, during production. What we are trying to do, since our shop floor planning software does not do it, is to go in and generate a list of how long it takes to change a resource from producing part A to part B. So I need to take the list of parts, for each company + plant + resource... and create the from part ... to part. Then we can go in and assign how long it will take to transition from part A to Part B... before we can start to produce part B. Got it? The cursor... that I posted on friday.. ran until I killed it on Sunday Morning... and I was running for only one plant.. and parts that started with Z. This was a list of 362 rows.

    Maybe it's just these old eyes but I see a cursor and sample data with "Material_Number" in them but no "Part_Number". Is "Material_Number" what you're talking about?

    Also, I'm still not bagging what you're raking about the From_Material and To_Material. To me, your cursor...

    declare @plant as varchar(4)

    , @Resource_Code as varchar(8)

    , @Material_Number as varchar(18)

    set nocount on;

    declare i_cursor insensitive cursor

    for Select Plant, Resource_Code, Material_Number From Resource_Mat

    open i_Cursor

    while @@fetch_status = 0

    begin

    insert into trans_Matrix(Company, Plant, Resource_Code, From_Material, To_Material)

    select 'SPOR' as Company

    , Plant

    , Resource_Code

    , @Material_Number as From_Material

    , Material_Number as To_Material

    from Resource_Mat

    where @Plant = plant and @resource_code = resource_code

    end

    fetch next from i_Cursor into @Plant, @Resource_Code, @Material_Number

    close i_Cursor

    deallocate i_cursor

    set nocount off;

    go

    ... looks like a massive CROSS JOIN of the Resource_Mat table to itself based on the "plant" and "resource_code". While that doesn't make sense to me (even though you mentioned that in the title of the post), if that's what you really need, the following code should do it for you...

    INSERT INTO dbo.Trans_Matrix

    (Company, Plant, Resource_Code, From_Material, To_Material)

    SELECT Company = 'SPOR'

    ,Plant = frommat.Plant

    ,Resource_Code = frommat.Resource_Code

    ,From_Material = frommat.Material_Number

    ,To_Material = tomat.Material_Number

    FROM dbo.Resource_Mat frommat

    JOIN dbo.Resource_Mat tomat --This is going to work like a conditional CROSS JOIN

    ON frommat.Plant = tomat.Plant

    AND frommat.Resource_Code = tomat.Resource_Code

    ;

    ... and, compared to the cursor, it will run like the wind generating millions of rows in just a couple of seconds if you have some decent indexing on the Resource_Mat table.

    Of course, you never answered my question about what you meant by a "massive" table. How many rows does it actually have in it???

    Last but not least, if you go back and look at HanShi's post (the second post on this thread), he suggested the exact same thing I just did. You just needed to study the code a bit to realize that it's probably exactly what you wanted sans some of your column names and the missing criteria for the Resource_Code.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 15 posts - 1 through 15 (of 21 total)

You must be logged in to reply to this topic. Login to reply