How do I avoid using cursors in Sybase (T-SQL)?

Imagine the scene, you're updating some legacy Sybase code and come across a cursor. The stored procedure builds up a result set in a #temporary table which is all ready to be returned except that one of columns isn't terribly human readable, it's an alphanumeric code.

What we need to do, is figure out the possible distinct values of this code, call another stored procedure to cross reference these discreet values and then update the result set with the newly deciphered values:

declare c_lookup_codes for
select distinct lookup_code
from #workinprogress

    fetch c_lookup_codes into @lookup_code

    if @@sqlstatus<>0

    exec proc_code_xref @lookup_code @xref_code OUTPUT

    update #workinprogress
    set xref = @xref_code
    where lookup_code = @lookup_code

Now then, whilst this may give some folks palpitations, it does work. My question is, how best would one avoid this kind of thing?

NB: for the purposes of this example you can also imagine that the result set is in the region of 500k rows and that there are 100 distinct values of lookupcode and finally, that it is not possible to have a table with the xref values in as the logic in proccodexref is too arcane.


You have to have a XRef table if you want to take out the cursor. Assuming you know the 100 distinct lookup values (and that they're static) it's simple to generate one by calling proccodexref 100 times and inserting the results into a table

Unless you are willing to duplicate the code in the xref proc, there is no way to avoid using a cursor.

Need Your Help

Swift mailer sender name with french chars

php hotmail swiftmailer massmail

I am building a system to send bulk emails. So far, while sending emails one by one I achieve success with all the mail services i tried (microsoft exchange, gmail, yahoo) but one, hotmail.

Order items in MongoDB according to the size of an array with MongoMapper?

ruby mongodb mongomapper

I'd like to select a collection of items ordered based on the number of items within an array. Hopefully the following example will clarify my rather poor explanation:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.