Computing.Net > Forums > Database > tricky sql loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

tricky sql loop

Reply to Message Icon

Name: alex_k
Date: August 17, 2007 at 04:11:14 Pacific
OS: sybase ASE 12.5/solaris 1
CPU/Ram: N/A
Product: N/A
Comment:

hi guys. thanks in advance for any help on this one.

so i am trying to get this semi tricksy loop running in ASE12.5 natively without having to resort to writing a perl script to do the work.

let me show you an example of the table:

filename routeids
xxxx 1;2
yyyy 3;1;2;4;5;6
zzzz NULL

now the output of the loop i want to look something like this:

NULL
1
2
3
4
5
6

so i need some sort of patindex or charindex loop that will get all the individual routeid's out of each row, and then move to the next row until it scans across the whole table.

this is what i am trying so far:

declare @voute varchar(20)
select @voute=''

While 1=1
begin

select @voute=(select substring(routeids,1,(select patindex('%;%',route)-1 from TABLE where feedname = 'xxxx')) from TABLE
where feedname = 'xxxx' and routeids>@voute)

if @voute is null
break
else
print @voute

continue

end


^ obviously the above example is me trying to get it working for a specific row 1st. it just seems to loop and loop and never break at the end of the row, so i can't get to the stage where i put the cursor in.

thanks again guys this is a real stinker for me....



Sponsored Link
Ads by Google

Response Number 1
Name: mdow
Date: August 17, 2007 at 13:43:05 Pacific
Reply:

Just curious. When does 1 not equal 1?


0

Response Number 2
Name: alex_k
Date: August 17, 2007 at 15:33:43 Pacific
Reply:

my logic there was to have an always true until it gets to the end of the row, and then it breaks. i was hoping the is null clause would break the loop.

it may be where the issue is...


0

Response Number 3
Name: mdow
Date: August 17, 2007 at 17:33:12 Pacific
Reply:

Ok. I don't know the syntax, but it would appear that you can't get to the null value because your WHERE clause states to only look at feedname 'xxxx' and in your example above the null value has a feedname/filename of 'zzzz'

Also, I believe the patindex function only returns the first occurrence; therefore, you should have an infinite loop that returns the value 1 over and over.

I would get rid of the patindex function and use the substring function exclusively.

Think about something like this.

SET @i = 1
WHILE (@i < 10)

SELECT @voute=(SELECT SUBSTRING(routeid,@i,1) FROM TABLE)

PRINT @voute
SET @i = @i + 2

Or

SET @voute = 1
SET @i = 1

WHILE (@voute IS NOT NULL)

SELECT @voute=(SELECT SUBSTRING(routeid,@i,1) FROM TABLE)

PRINT @voute
SET @i = @i + 2


0

Response Number 4
Name: alex_k
Date: August 18, 2007 at 03:20:10 Pacific
Reply:

oh yeah i did the where clause for file xxxx just tryna get one row working first. i'll try this once i have database access again thanks mdow.

one thing i might have missed in the example, the routeid's may be more than one character... anywhere from 1-7 characters so i need to use the semicolon as a separator.

but yeah i will look at your sql and adapt as necessary....


0

Response Number 5
Name: mdow
Date: August 18, 2007 at 08:09:22 Pacific
Reply:

Hopefully you are on the right track. Just keep in mind that I did know that you were trying to loop through one record. The ideas that I gave you were intended to do the same. To accomplish both tasks, you will need a nested loop.

Interesting problem with the routids. The patindex function would be useful there if you know the correct start position for each iteration of the loop.
--Mike



0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Database Forum Home


Sponsored links

Ads by Google


Results for: tricky sql loop

SQL Server Reporting Services www.computing.net/answers/dbase/sql-server-reporting-services/722.html

SQL Question www.computing.net/answers/dbase/sql-question/83.html

SQL Server 2000 Restore www.computing.net/answers/dbase/sql-server-2000-restore/251.html