I have a query that will return only columns with values. How do I add that to a function so I can use that with any query? Would it be a function in the where clause.
create table test1
(
s_no int not null,
name varchar(10) not null,
address varchar(10) null,
emailid varchar(100) null
)
insert into test1 (s_no, name)
values (1,'A'),(2,'B'),(3,'C')
declare @column_list varchar(8000),
@counter int
set @column_list = ''
set @counter = 0
while (Select max(colid) from syscolumns where id = object_id('test1') and isnullable= 0) > @counter
begin
select @counter = min(colid)
from syscolumns
where id = object_id('test1')
and isnullable = 0
and colid > @counter
select @column_list = @column_list + ',' + (Select name from syscolumns where id = object_id('test1') and isnullable= 0 and colid = @counter)
end
select @column_list = SUBSTRING(@column_list, 2, len(@column_list))
declare @sql varchar(8000)
select @sql = 'select ' + @column_list + ' from test1'
print @sql
exec (@sql)
SELECT * FROM [dbo].[test1]