Concatenating rows from different tables into one field

Posted by Markus on Stack Overflow See other posts from Stack Overflow or by Markus
Published on 2009-06-10T08:57:28Z Indexed on 2010/03/14 2:05 UTC
Read the original article Hit count: 319

Filed under:
|
|
|

Hi!

In a project using a MSSQL 2005 Database we are required to log all data manipulating actions in a logging table. One field in that table is supposed to contain the row before it was changed. We have a lot of tables so I was trying to write a stored procedure that would gather up all the fields in one row of a table that was given to it, concatenate them somehow and then write a new log entry with that information.

I already tried using FOR XML PATH and it worked, but the client doesn't like the XML notation, they want a csv field.

Here's what I had with FOR XML PATH:

DECLARE @foo varchar(max);
SET @foo = (SELECT * FROM table WHERE id = 5775 FOR XML PATH(''));

The values for "table", "id" and the actual id (here: 5775) would later be passed in via the call to the stored procedure.

Is there any way to do this without getting XML notation and without knowing in advance which fields are going to be returned by the SELECT statement?

© Stack Overflow or respective owner

Related posts about concatenation

Related posts about Xml