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.
Author |
Topic |
Taragor
Starting Member
46 Posts |
Posted - 2012-07-05 : 15:05:37
|
Good afternoon all,I've written a query with a while statement but something is not working and I'm not sure why. (Although it might be the multiple if's in the while)here is the querydeclare @order_ctrl_code varchar(25)declare @accession_no intdeclare @ec tinyintset @order_ctrl_code = 'KDICTE'set @accession_no = 123set @ec = 0declare @order varchar(25)declare @count tinyintdeclare @index int/*check how many records are present in the error table for this procedure*/set @count = (select count(*) from riserrors where accession_no = @accession_no)/*Check if errors present in the error table*/if @count <> 0begin /*Delete records for this procedure from the error table if some are present*/ if @EC = 0 begin /*Check @order_ctrl_code, if value = IEXFIN automatically delete records from the error table as this would overwrite any record found there. If not IEXFIN see else statement*/ if @order_ctrl_code = 'IEXFIN' begin delete from riserrors where accession_no = @accession_no end else begin /*Check order_ctrl_code of errors in the error table. If errors have an IEXFIN order_ctrl_code, do not delete that error as it will update the record imported*/ while @count > 0 begin set @index = (select top 1 rec_index from riserrors where accession_no = @accession_no and rec_index > @index order by rec_index) set @order = (select order_ctrl_code from riserrors where rec_index = @index) if @order <> 'IEXFIN' begin if @order <> 'KDICTE' begin delete from riserrors where rec_index = @index set @count = @count - 1 if @count = 0 break else continue end else begin if @order_ctrl_code = 'KDICTE' begin delete from riserrors where rec_index = @index set @count = @count - 1 if @count = 0 break else continue end else begin set @count = @count - 1 if @count = 0 break else continue end end end else begin set @count = @count - 1 if @count = 0 break else continue end end end endend The test table has 2 rows in it. 1 row should be deleted when this is run but none are. If I put a print statement below the begin right below the while line it shows @count going 2 then 1 so part of the while is working.Anyone have any ideas / suggestions?Sincerely,Tar |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-05 : 15:09:29
|
i think issue is this where conditionrec_index > @index you've not assigned @index a value so it will be NULL by default and hence this condition wont return any value under default conditions as NULL is not stored as a value. try this insteadrec_index > coalesce(@index ,0)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Taragor
Starting Member
46 Posts |
Posted - 2012-07-05 : 15:16:28
|
quote: Originally posted by visakh16 i think issue is this where conditionrec_index > @index you've not assigned @index a value so it will be NULL by default and hence this condition wont return any value under default conditions as NULL is not stored as a value. try this insteadrec_index > coalesce(@index ,0)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks Visak,For some reason I wasn't taking into consideration that the first pass @index actually had a null and therefore the direct > wouldn't work on it. Greatly appreciated,Tar |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-05 : 15:20:54
|
welcome...hope it resolved your issue------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|