Update Query

  • USE [tempdb]

    CREATE TABLE [dbo].[hubCustomer](

    [CustVid] [int] IDENTITY(1,1) NOT NULL,

    [CustSourceGid] [nvarchar](30) NULL

    )

    INSERT INTO HUBCustomer SELECT '103' UNION ALL

    SELECT '105' UNION ALL

    SELECT '107'

    CREATE TABLE [dbo].[hubProductSale](

    [ProductSaleVid] [int] IDENTITY(1,1) NOT NULL,

    [CustSourceGid] [nvarchar](30) NULL,

    [ProductSourceGid] [nvarchar](100) NULL

    )

    INSERT INTO hubProductSale SELECT '103','001' UNION ALL

    SELECT '105','007'UNION ALL

    SELECT '107','008'UNION ALL

    SELECT '109','006'UNION ALL

    SELECT '104','007'

    CREATE TABLE [dbo].[LnkCustProduct](

    [LinkVid] [int] IDENTITY(1,1) NOT NULL,

    [CustVid] [int] NULL,

    [ProductSaleVid] [int] NULL

    )

    INSERT INTO LnkCustProduct SELECT '0','5' UNION ALL

    SELECT '0','6' UNION ALL

    SELECT '0','5' UNION ALL

    SELECT '1','1' UNION ALL

    SELECT '1','2' UNION ALL

    SELECT '2','3'

    CREATE TABLE [dbo].[SuspectRecords](

    [SuspetRecordsID] [int] IDENTITY(1,1) NOT NULL,

    [SourceParentGID] [nvarchar](100) NULL,

    [SourceChildGid] [nvarchar](100) NULL

    )

    INSERT INTO SuspectRecords SELECT '104','007' UNION ALL

    SELECT '109','006'

    update LnkCustProduct

    set CustVid=

    ( case when LCP.CustVid =0 THEN hc.CustVid

    else LCP.CustVid end)

    FROM SuspectRecords SR inner join hubCustomer hc on SR.SourceParentGID=hc.CustSourceGid

    INNER JOIN hubProductSale HPS ON SR.SourceChildGid=HPS.ProductSourceGid

    INNER JOIN LnkCustProduct LCP ON HPS.ProductSaleVid=LCP.ProductSaleVid

    HubCustomer and hubProductSale has parent child relationship.

    linkCustProduct records the relationship between hubcustomer and hubProductSale.

    Whenever there is a sale visible of a customer in LnkCustProduct table but the custID is not yet visible in hubCustomer the CustVid for those records in the linkTable (LnkCustProduct) is recroded as null converted to 0 and the CustSourceGid and ProductSourceGid of that record is recorded in a separate table called suspect records.

    If the parentID of that record is visible later in time, I want to update the CustVid of that record in the linktable and remove it from SuspectTable.

    In this example lets say, there was an insert of 104 in hubCustomer so that is no longer a suspect record. So I would like to update its corresponding CustVid into the link table from 0 and remove it from suspect record. My query is doesn’t quite do that. Please shed some light.

  • Can you provide expected results in each of the 4 tables after the update you want to perform is applied.

    I looked at your description and maybe my brain is just fuzzy this morning but I couldn't put the pieces together.

    A MERGE may work for this but I'd need to see the expected results to make it clear to me.


    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

  • Marbe this helps.

    UPDATE LnkCustProduct SET LnkCustProduct.CustVid = hc.CustVid

    FROM SuspectRecords SR inner join hubCustomer hc on SR.SourceParentGID=hc.CustSourceGid

    INNER JOIN hubProductSale HPS ON SR.SourceChildGid=HPS.ProductSourceGid AND

    SR.SourceParentGID=HPS.CustSourceGid

    INNER JOIN LnkCustProduct LCP ON HPS.ProductSaleVid=LCP.ProductSaleVid

    DELETE SuspectRecords WHERE EXISTS(SELECT CustVid FROM HubCustomer HC WHERE SuspectRecords.SourceParentGID=hc.CustSourceGid )

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

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