SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Query


Update Query

Author
Message
peacesells
peacesells
Old Hand
Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)

Group: General Forum Members
Points: 338 Visits: 1481
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.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7249 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
adrian.facio
adrian.facio
SSC-Addicted
SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)

Group: General Forum Members
Points: 438 Visits: 386
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 )




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search