2011-01-07 10 views
9

Come posso stampare le righe aggiornate da questa query in questa query:uscita T-SQL di risultati aggiornati

update 
    Table1.RecommendationLeg 
set 
    actualValue = (leg.actualprice * str.currentSize) 
from 
    Table1.RecommendationLeg leg 
    inner join Recommendation str 
     on leg.partofId = str.id 
where 
    leg.actualValue = 0 
    and datediff(n, timeOf, CURRENT_TIMESTAMP) > 30 
+2

Quale versione di SQL Server è attiva? – Oded

+0

SQL Server 2008 – PhilBrown

risposta

10
update 
    Table1.RecommendationLeg 
set 
    actualValue = (leg.actualprice * str.currentSize) 
OUTPUT INSERTED.actualValue -- <-- this. Edit, after SET not UPDATE. Oops. Sorry. 
from 
    Table1.RecommendationLeg leg 
    inner join Recommendation str 
     on leg.partofId = str.id 
where 
    leg.actualValue = 0 
    and datediff(n, timeOf, CURRENT_TIMESTAMP) > 30 
+0

Questo è vero, ma OUTPUT va nel SET. Ho avuto problemi fino a quando ho capito che INSERTED era Table1.RecommendationLeg. Puoi fare cose come "OUTPUT INSERTED.actualValue come av, inserted.actualPrezzo come ap, str.currentSize come cs" – PhilBrown

+3

Amo il trigliato. – PhilBrown

3

Se siete su SQL Server 2005 e, soprattutto, è possibile utilizzare il OUTPUT clause.