views:

13

answers:

0

In my travels in Oracle, the 'stragg' function, or 'String Aggregator' was life-saving when I had to create dynamic SQL queries on the fly.

You can read up about it here: http://www.oratechinfo.co.uk/delimited_lists_to_collections.html

The basic use of it was:

select stragg(fruit) from food;

fruit
-----------
apple,pear,banana,strawberry

1 row(s) returned

So simple to use, concatenating chr(13) turned it into a long list, and selecting information from system tables gave a 5 minute solution to dynamically generated SQL, e.g. auditing triggers.

Now I've been charged with transferring oracle functionality related to auditing into Sybase, and a function similar to Stragg would be ideal for this purpose.

E.g.

select 'insert into '+@mytable+' (' +char(10)
   + stragg(c.name) +char(10)
   + 'select '
   + stragg('inserted.'+c.name) + char(10)
   + 'from inserted'
from syscolumns c
where objectid(@mytable) = c.id


------------------------------------------
insert into table_of_fruit_copy
(fruit, sweetness, price) 
select fruit_sweetness_price 
from inserted

Done. Simple.

Except I don't know how to get a string-aggregation function working in Sybase.

Does anyone know of an attempt to do this kind of thing, or code that could work the same as stragg that could be used in this way?

The alternative at the moment is printing code based on complex cursors and such (sample LOC: 500), or select statements combining static strings and columns from user tables (sample LOC: 200). Stragg would severely reduce the complexity of this code, and would be a great deal of help in the future (sample LOC: who knows, maybe 50?)

p.s. I'm calling these selects through a shell script then piping them to file, then running the file through iSQL. Not the nicest solution, but it's better than the alternatives.