SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 While statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Taragor
Starting Member

41 Posts

Posted - 07/05/2012 :  15:05:37  Show Profile  Reply with Quote

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 query

declare @order_ctrl_code varchar(25)
declare @accession_no int
declare @ec tinyint

set @order_ctrl_code = 'KDICTE'
set @accession_no = 123
set @ec = 0

declare @order varchar(25)
declare @count tinyint
declare @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 <> 0
begin
  /*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
   end
end


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

India
47115 Posts

Posted - 07/05/2012 :  15:09:29  Show Profile  Reply with Quote
i think issue is this where condition

rec_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 instead

rec_index > coalesce(@index ,0)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 07/05/2012 15:10:08
Go to Top of Page

Taragor
Starting Member

41 Posts

Posted - 07/05/2012 :  15:16:28  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

i think issue is this where condition

rec_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 instead

rec_index > coalesce(@index ,0)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47115 Posts

Posted - 07/05/2012 :  15:20:54  Show Profile  Reply with Quote
welcome...hope it resolved your issue

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000