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
 General SQL Server Forums
 New to SQL Server Programming
 not sure what this line is doing....

Author  Topic 

jefals
Starting Member

8 Posts

Posted - 2011-04-26 : 22:56:45
Hello;
I'm going thru "Beginning SQL Server 2008 for Developers"..It's probably not one of the best intro books, and there are many areas where the explanations for what the author is doing is either weak or non-existent....Here's a stored procedure from the book, and the line I'm having problem with is highlighted. The procedure is to demonstrate several things -- CASE, JOIN, WHILE, etc..and the context is getting a running total of someone's balance between two dates.

My only problem is I don't know what that SELECT line I've highlighted is doing; is it setting the 3 variables? The author comments that he is using @STILLCALC as a test for the WHILE loop, but that it could be any variable as he is using the CONTINUE and BREAK statements to determine when to exit the loop.

Thanks,
Jeff S

create procedure customerdetails.apf_custmovement @custid bigint,
@fromdate datetime, @todate datetime
as begin
declare @runningbal money, @stillcalc bit,@lasttran bigint
select @stillcalc = 1,@lasttran=0,@runningbal=0
while @stillcalc=1
begin
select top 1 @runningbal=@runningbal + case
when tt.credittype = 1 then t.amount
else t.amount * -1 end,
@lasttran = t.transactionid
from CustomerDetails.Customers c
join transactiondetails.transactions t on t.customerid=c.customerid
join transactiondetails.transactiontypes tt on
tt.transactiontypeid=t.transactiontype
where t.transactionid > @lasttran
and tt.affectcashbalance = 1
and dateentered between @fromdate and @todate
order by dateentered
if @@rowcount > 0
continue
else
break
end
select @runningbal as 'End Balance'
end
go

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-26 : 23:12:27
quote:
is it setting the 3 variables?
Yes. SELECT can assign values, like SET, except SET only assigns to one variable at a time, while SELECT can do multiple variables at once.
Go to Top of Page

jefals
Starting Member

8 Posts

Posted - 2011-04-27 : 02:07:48
got it. Thanks Rob
Go to Top of Page
   

- Advertisement -