5

I'm looking for a way to loop through the columns of a table to generate an output as described below.

The table looks like that:

ID  Name     OPTION1 OPTION2 OPTION3 OPTION4 OPTION5
1   MyName1  1       0       1       1       0
2   MyName2  0       0       1       0       0

And the output looks like that:

MyName1 -> OPTION1, OPTION3, OPTION4
MyName2 -> OPTION3

Any directions of doing this simply would be greatly appreciated. Otherwise, I suppose I'll have to use a cursor or a temporary table... The database engine is MSSQL. The reason I'm doing formatting at the database level is to feed its output into a limited programmable environment.

Update: the ouput can by in any form, a string or rows of strings.

Update: Would the be a way to accomplish that by building a string using @str = @str + ... ?

Update: I changed the output... this should be easier.

Thanks!

4
  • Do you have a fixed number of columns? (also "Option4 -> MyName1" ?) Commented Mar 30, 2009 at 14:18
  • Hi Ian, Yes let's assume we have a fixed number of columns. Commented Mar 30, 2009 at 14:21
  • I think its unclear what the output should be. Do you want to take a table and generate text strings? A "pivot table" solution would result in a resultset or another table. Commented Mar 30, 2009 at 14:38
  • I've updated again. The output can be in any form, as a string or rows of strings. Commented Mar 30, 2009 at 14:44

6 Answers 6

4

Well, in case of a known number of columns, you can do:

SELECT  
  MyName + " ->"
  + case OPTION1 when 1 then ' OPTION1' else '' end
  + case OPTION2 when 1 then ' OPTION2' else '' end
  + ...
FROM
 Table

If columns are unknown when you create the query - I'd probably still go that way with some dynamically created SQL. The advantage is that the code probably does what you wants and is very simple.

Sign up to request clarification or add additional context in comments.

Comments

3

You might want to have a look at PIVOT Tables.

1 Comment

Right, pivot tables should be usable. Checking to see if there are other fresh ideas about a methodology I'm not aware of.
2

Since you don't go into the specific needs of why you want to be able to do this I can't be certain, but usually when I see this kind of question there are two things that I think of:

  1. You need to normalize your database. Maybe "Option1", "Option2" etc. have nothing in common, but there is also a good chance that they are a repeating group within your table.

  2. Handle display issues in the display layer of your application - i.e. the front end, not the database.

As I said, maybe these don't apply in your case for some specific reason, but it seems like it from what I've read of your question.

2 Comments

Right, Tom. The reason I'm trying to do formatting at the database level is to be able to feed the output directly in a report where I don't have much scripting flexibility.
Yep, that would be one of those cases where this kind of thing becomes necessary. Carry on. ;)
1

You could build a dynamic statement using the system catalog:

http://msdn.microsoft.com/en-us/library/ms189082.aspx

Comments

1

If using pivot table, you must make sure all of your "Option" columns have the same data type and length.

I would suggest the following answer:


IF NOT EXISTS( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME      
 = 'TABLE1' ) 
create table table1
(
   name nvarchar(50),       
   colvalue nvarchar(50)
)
else
  truncate table table1

declare @table nvarchar(50)
set @table = 'yourtable'

declare @column table
(
   ID integer identity,
   colname nvarchar(20)
)


insert into @column
SELECT c.name FROM sys.tables t 
JOIN sys.columns c ON t.Object_ID = c.Object_ID 
WHERE t.Name = @table 
and c.name in ('Option1','Option2','Option3','Option4','Option5')

declare @minID integer, @maxID integer
declare @cmd nvarchar(max)  
declare @col nvarchar(20)
declare @SQLStr nvarchar(max)

select @minID = MIN(ID), @maxID= MAX(ID)
from @column

while @minID <= @maxID
begin
    select @col = colname
    from @column
    where ID = @minID

    set @SQLStr =    
    'insert into table1 (name, colvalue)
    select name,' + @col + '
    from ' + @table + ' 
    where ' + @col + ' <> 0'    

    exec(@SQLStr)

    set @minID = @minID + 1
end

select distinct name, STUFF(
(SELECT  ',' + a.colvalue  AS [text()]
from Table1  a
where a.name = b.name
Order by a.colvalue
for xml PATH('')),1,1,''    ) AS Comments_Concatenated
from Table1 b
group by name, colvalue
ORDER BY name

You just have to modify the @table by putting in your table name and the list of the column you need before insret into @column.

No matter what data type you are, it will working fine.

Comments

-1

Edit:

Is there a way to create a MSSQL function to “join” multiple rows from a subquery into a single delimited field?

Search for various solutions to previous questions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.