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 2005 Forums
 Transact-SQL (2005)
 WHILE... IF... ELSE

Author  Topic 

Kobojunkie
Starting Member

8 Posts

Posted - 2008-11-10 : 23:50:01
I am not exactly sure why but the code below does not seem to work right. Anyone with any idea why the while loop below would only process some and not all rows in my table?


Declare @Ttid as integer

Select @Ttid = min(t_tid) from tempproducttable

While @Ttid IS NOT NULL
BEGIN
IF EXISTS( Select * from tempproducttable inner join tproduct on t_code =tp_code and t_tid = @Ttid)
BEGIN
Update tProduct
Set
tp_productcode = t_Productcode
From
tProduct inner join tempproducttable
on t_code = tp_code
and t_tid = @Ttid
END
ELSE
BEGIN
Insert Into tProduct
(tp_code, tp_productcode, tp_groupcode)
select t_code, tempdb_Productcode, t_groupcode
from tempproducttable
Where t_tid = @Ttid
END

Select @Ttid = min(t_tid) from tempproducttable Where t_tid > @Ttid
END -- End while Loop

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-10 : 23:56:55
can you illustrate the problem you're facing with some sample data? which records do you think were missed out?
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2008-11-11 : 00:39:14
Code seems perfect, possibly problem is on your data side
Go to Top of Page
   

- Advertisement -