Generate parent_id using identity column compare name with parent name: Need to help

  • Actual Table:

    IdName Parent_nameParent_id

    1A.B A

    2A.B(1).C B

    3A.B(2).D B

    4A.B.C.D.E D

    5A.B.C.D(1).E D

    6A.B.C.D(2).F D

    Above table Name column having rows like(A.B,A.B(1).C,A.B.C.D.E,A.B.C.D(1).E,A.B.C.D(2).F)

    Parent_Name column having rows like(A,B,B,D,D,D)

    Requirement: The above table id is identity column based upon identity column compare Name with Parent_name need to generate Parent_id column.

    I have write SP like the below:

    CREATE Procedure [dbo].[SSIS_Parent_attr_list_seq_id]

    as

    begin

    set nocount on;

    WITH Cte_ATTR_LIST_LKP(Attribute_Name, Parent_Attribute_Name, ATTR_LIST_SEQ_ID, PARENT_ATTR_LIST_SEQ_ID)

    AS (SELECT ast.Attribute_Name,

    ast.Parent_Attribute_Name,

    alk.ATTR_LIST_SEQ_ID,

    alk.PARENT_ATTR_LIST_SEQ_ID

    FROM dbo.ATTR_LIST_STG AS ast INNER JOIN

    dbo.ATTR_LIST_LKP AS alk ON

    ast.Attribute_Name = alk.Attribute_Name)

    update ATTR_LIST_LKP set PARENT_ATTR_LIST_SEQ_ID=c_PARENT_ATTR_LIST_SEQ_ID from(

    SELECT a1.ATTR_LIST_SEQ_ID, a1.Attribute_Name, a2.ATTR_LIST_SEQ_ID AS c_PARENT_ATTR_LIST_SEQ_ID

    FROM Cte_ATTR_LIST_LKP AS a1 inner JOIN

    Cte_ATTR_LIST_LKP AS a2 ON a1.Parent_Attribute_Name = a2.Attribute_Name) a3 inner join ATTR_LIST_LKP a4

    on a3.Attribute_Name=a4.Attribute_Name

    end

  • Duplicate post, no replies here please.

    Replies here: http://www.sqlservercentral.com/Forums/Topic1353333-392-1.aspx


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 2 posts - 1 through 1 (of 1 total)

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