Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My Adventures as a SQL Server DBA
Browse by Tags · View All
BRH 16
#SQL Server 5
SQL server 4
realviews 3
sqlserver tools and utilities 3
sql server tools in my environment 3
TSQL 3
#DBA 3
audit 2
tools 2

Archive · View All
April 2011 4
March 2011 3
February 2011 3
November 2010 3
August 2010 2
May 2011 2
September 2010 2
May 2010 1
December 2011 1
July 2011 1

Assign a variable while changing the column – a little known trick

Apr 18 2011 2:26PM by Malathi Mahadevan   

 There are lot of scenarios we encounter where we need to know the value of something that was just updated – a row id, a field…the easiest and most common way to do this is to run a select statement soon after the update to get the value, but there is a simpler alternative.

 Consider below (tried on Adventureworks)

DECLARE @PRICEINCREASE INT, @NEWUNITPRICE MONEY

SELECT @PRICEINCREASE = 10

BEGIN TRANSACTION

UPDATE Sales.SalesOrderDetail SET 
    UnitPrice = UnitPrice + @PRICEINCREASE  
     WHERE SalesOrderID = 43659 AND SalesOrderDetailID = 1 AND ProductID = 776

COMMIT TRANSACTION

SELECT @NEWUNITPRICE = UnitPrice 
    FROM Sales.SalesOrderDetail   
    WHERE SalesOrderID = 43659 AND SalesOrderDetailID = 1 AND ProductID = 776

 The same can be easily accomplished in one statement as below

DECLARE @PRICEINCREASE INT, @NEWUNITPRICE MONEY

SELECT @PRICEINCREASE = 10

 BEGIN TRANSACTION

 UPDATE Sales.SalesOrderDetail SET 
    @NEWUNITPRICE = UnitPrice = UnitPrice + @PRICEINCREASE 
    WHERE SalesOrderID = 43659 AND SalesOrderDetailID = 1 AND ProductID = 776

COMMIT TRANSACTION

 The first statement took 2 seconds and the second one, less than a second.

 To be aware that only one value is returnable this way, so if you are updating more than one row, there is no real guarantee what will be returned, although in most cases it appears to be the value of the last row that was updated – in order of clustered index.

But it is a neat trick to master when you need one value out of the update and need to use two statements to do it. As of now there is no real name for the = = usage.

Tags: BRH, variable update, setting value of variable after update, update variable,


Malathi Mahadevan
74 · 2% · 772
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]