Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Odd WHERE condition in UPDATE statment.

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2013-07-12 : 15:12:47
I am looking at some existing code and at the top of the code it creates a temp table using

Create Table #tmpDriverTime
(DriverName Varchar(53)
,EmployeeNo Int
,WorkDay1 Float
,WorkDay2 Float
,WorkDay3 Float
,WorkDay4 Float
,WorkDay5 Float
,WorkDay6 Float
,WorkDay7 Float
,WorkDay8 Float
,WorkDay9 Float
,Total6Day Float
,Total7Day Float
,Total8Day Float
,HrsAvailable Float
,StartDateTime DateTime Null
,EndDateTime DateTime Null
,Status Int Null
,DateWorked DateTime Null
,WorkDay1Status Int Null
,WorkDay2Status Int Null
,WorkDay3Status Int Null
,WorkDay4Status Int Null
,WorkDay5Status Int Null
,WorkDay6Status Int Null
,WorkDay7Status Int Null
,WorkDay8Status Int Null
)


but later in the code there is an UPDATE statement with a WHERE condition I can not figure out.

Update 	#tmpDriverTime
Set WorkDay7Status = @Result
Where EmployeeNo = @DrvrID
And @PreviousIndex = 7


I know it is updating the Temp table to set the column WorkDay7Status to the value in the local varable @Result for the EmployeeNo equal to the value in @DrvrId. What I can't figure out is the second condition of @PreviousIndex = 7 part. There is no column of @PreviousIndex and looking at the code @PreviousIndex is a local variable of type INT. So how exactly does this second condition work?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-07-12 : 15:55:23
If the variable @PreviousIndex is equal to 7 it will do the update (assuming the other parts of the predicate are also true) otherwise it will not.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-12 : 15:56:18
That acts sort of like a switch. For example, if this code was part of a stored procedure and @PreviousIndex was one of the parameters, the caller could control whether the update would happen or not by passing 7 or something other than 7. Perhaps it is designed so the update will be done only if it is the 7th day of the week.
Go to Top of Page
   

- Advertisement -