try this:
create table jtr (id varchar(36), col_a tinyint, col_b tinyint, col_c tinyint)
insert into jtr values (1, 1, 0, 1), (2, 0, 0, 1), (3, 1, 0 ,0), (4, 0, 1, 0)
CREATE TABLE #app (res tinyint)
CREATE TABLE #outmess (message varchar(1000))
DECLARE @dynsql varchar(1000)
DECLARE @colname sysname
DECLARE @mess varchar(1000)
DECLARE @id int
DECLARE #crs_tab INSENSITIVE CURSOR FOR
SELECT id FROM jtr
FOR READ ONLY
OPEN #crs_tab
FETCH NEXT FROM #crs_tab INTO @id
WHILE (@@FETCH_STATUS = 0)
BEGIN
DECLARE #crs INSENSITIVE CURSOR FOR
SELECT c.name FROM syscolumns c
JOIN sysobjects o
ON o.id = c.id
WHERE o.name = 'jtr'
AND o.xtype = 'U'
AND c.type = 38
FOR READ ONLY
OPEN #crs
FETCH NEXT FROM #crs INTO @colname
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @dynsql = 'SELECT ' + @colname + ' FROM jtr where id = ' + CONVERT(varchar, @id)
insert into #app
exec (@dynsql)
if (select res from #app) = 1
begin
if (@mess != '')
begin
set @mess = @mess + ', ' + @colname
end
else
begin
set @mess = @colname
end
end
delete from #app
FETCH NEXT FROM #crs INTO @colname
END
CLOSE #crs
DEALLOCATE #crs
insert into #outmess values (@mess)
set @mess = ''
FETCH NEXT FROM #crs_tab INTO @id
END
CLOSE #crs_tab
DEALLOCATE #crs_tab
select * from #outmess
I've created an example table JTR with these fields: ID, COL_A, COL_B, COL_C if you want you can put how many fields you want.
I put 4 rows in that table.
So I've implemented two cursor, one to scroll JTR table and one to scroll all the column name from syscolumns.
For every column valued ONE i build a message with column name
In the temporary table #outmess you find all the column valued ONE concatenated by comma.
P.S. Instead boolean I've used tinyint. In syscolumns field type valued 38 about tinyint
Tell me if it's ok