Question : Dynamic columns with dynamic column names

In a table we have 20 fields. In another table a client can flag up  to 13 fields to be shown on a report. How can we dynamically create the report with the fields requested by the client?

The report can either be a fixed 13 columns (leaving some blank if less than 13 flagged) or dynamically set the number of columns.

Answer : Dynamic columns with dynamic column names

so, to call that proc, do something like this (just an example, you'd be passing the field names in through code)

declare @fieldlist nvarchar(700), @tablename nvarchar(255)
select @fieldlist = 'firstname, phonenumber, lastname, address, city, state, zip'
select @tablename = 'mytable'

execute usp_mydynamicreport
@fieldlist, @talbename
Random Solutions  
 
programming4us programming4us