Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Select and Update from CTE
Message
From
30/06/2015 13:44:30
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Select and Update from CTE
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01621599
Message ID:
01621599
Views:
58
I have this:
;WITH InvoiceTemp AS
 (SELECT Invoice.Numero,Invoice.NoClient,Invoice.NoStatus,Invoice.AddDate,
  ROW_NUMBER() OVER (PARTITION BY Invoice.NoClient ORDER BY AddDate DESC) AS RowNumber
  FROM Invoice)

UPDATE Client SET Status=InvoiceTemp3.AddDate
 FROM Client
 INNER JOIN 
(SELECT InvoiceTemp.NoClient,InvoiceTemp.AddDate FROM InvoiceTemp
 WHERE InvoiceTemp.RowNumber=2 AND InvoiceTemp.NoStatus=7 AND
  EXISTS (SELECT InvoiceTemp.Numero FROM InvoiceTemp AS InvoiceTemp2
   WHERE InvoiceTemp2.RowNumber=2 AND InvoiceTemp2.NoStatus=7 AND InvoiceTemp.NoClient=InvoiceTemp2.NoClient)) InvoiceTemp3
   ON Client.Numero=InvoiceTemp3.NoClient

;WITH InvoiceTemp AS
 (SELECT Invoice.Numero,Invoice.NoClient,Invoice.NoStatus,
  ROW_NUMBER() OVER (PARTITION BY Invoice.NoClient ORDER BY AddDate DESC) AS RowNumber
  FROM Invoice)

DELETE FROM InvoiceTemp
 WHERE InvoiceTemp.RowNumber=1 AND InvoiceTemp.NoStatus=7 AND
  EXISTS (SELECT InvoiceTemp.Numero FROM InvoiceTemp AS InvoiceTemp2
   WHERE InvoiceTemp2.RowNumber=2 AND InvoiceTemp2.NoStatus=7 AND InvoiceTemp.NoClient=InvoiceTemp2.NoClient)
So, basically, the goal is to update something in the parent, from a record which has to be deleted in the child. I am wondering why I ended up 910440 records updated and 902325 records deleted. Anyone could explain this?
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform