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 2008 Forums
 Transact-SQL (2008)
 Modifying a variable within a while loop

Author  Topic 

ATG
Starting Member

35 Posts

Posted - 2011-04-04 : 17:36:48
Hey everyone.

I have a variable called @UnIn which is basically a flag that I want to be a '1' if there is an entry that has a null PMSL.InterfaceDate value.

I've never written a while loop in SQL so I'm not quite sure that I have it right.

Here is my code thus far:

declare @UnIn int

set @UnIn = 0

while (select PMSL.SLItem from PMSL where PMSL.PMCo=1 and PMSL.SL='094280.005') > 0 and not(@UnIn=1)
begin
select @UnIn = (case when PMSL.InterfaceDate is null then 1 else 0 end) from PMSL where PMSL.SL='094280.005' and PMSL.PMCo=1 order by PMSL.SLItem
break
end
print(@UnIn)

PMSL.SLItems are the items that I want to be traversing. Each item has an InterfaceDate field and the purpose of my loop is to see if at least 1 item has a null interfacedate. If it does, make the @UnIn variable equal to 1 and break out of the loop

Thanks!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-04 : 17:44:33
What's wrong with:

SELECT SLItem
FROM PMSL
WHERE PMSL.SL='094280.005' AND PMSL.PMCo=1 AND PMSL.InterfaceDate IS NULL
ORDER BY PMSL.SLItem
Go to Top of Page

ATG
Starting Member

35 Posts

Posted - 2011-04-04 : 17:53:45
Ah. I should have clarified. This section of code will be within a stored procedure so the standard where clause will not work.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-04 : 18:20:24
Why won't a WHERE clause work in a stored procedure?
Go to Top of Page

ATG
Starting Member

35 Posts

Posted - 2011-04-04 : 18:24:13
It will. Just not the way I had hoped. I did however, figure it out. Thanks!
Go to Top of Page
   

- Advertisement -