code_junkie

Can I update/select from a table in one query?

Nov 10th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. Begin Tran
  2.  
  3. Update Pages Set Views = Views + 1 Where ID = @ID
  4. Select Columns From Pages Where ID = @ID
  5.  
  6. Commit Tran
  7.  
  8. USE AdventureWorks;
  9. GO
  10. DECLARE @MyTestVar table (
  11. OldScrapReasonID int NOT NULL,
  12. NewScrapReasonID int NOT NULL,
  13. WorkOrderID int NOT NULL,
  14. ProductID int NOT NULL,
  15. ProductName nvarchar(50)NOT NULL);
  16.  
  17. UPDATE Production.WorkOrder
  18. SET ScrapReasonID = 4
  19. OUTPUT DELETED.ScrapReasonID,
  20. INSERTED.ScrapReasonID,
  21. INSERTED.WorkOrderID,
  22. INSERTED.ProductID,
  23. p.Name
  24. INTO @MyTestVar
  25. FROM Production.WorkOrder AS wo
  26. INNER JOIN Production.Product AS p
  27. ON wo.ProductID = p.ProductID
  28. AND wo.ScrapReasonID= 16
  29. AND p.ProductID = 733;
  30. SELECT OldScrapReasonID, NewScrapReasonID, WorkOrderID,
  31. ProductID, ProductName
  32. FROM @MyTestVar;
  33. GO
  34.  
  35. UPDATE mytable
  36. SET views = 5
  37. WHERE id = 16
  38. RETURNING id, views, othercolumn;
  39.  
  40. update tablex set y=z where a=b rn select a,b,y,z from tablex
Add Comment
Please, Sign In to add comment