Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Select and Update from CTE
Message
De
30/06/2015 13:44:30
 
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Select and Update from CTE
Versions des environnements
SQL Server:
SQL Server 2012
Application:
Web
Divers
Thread ID:
01621599
Message ID:
01621599
Vues:
56
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
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform