Link to home
Create AccountLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Revise an existing stored procedure to...

Using a wonderful solution from Sharath_123 at:

(also see:!3/35033/3 for sample to test it)

This solution works with tblsource having only two fields f1 and f2.

New requirement: Put the content of tblSource to tblTarget (distinct and sorted, ignoring blank entries) as shown below.

Now, tblSource has unknown number of fields f1, f2, f3,... fn and tblTarget doesn't exist (the procedure is to build it and populate it).

tblSource (exists in the database)
f1         f2              fn
-----   -------   ... ----------
C          R               Q
B          X              
B          P              P
A          X

 tblTarget (doesn't exist, the stored procedure expected build and populated it)
f1         f2            fn
-----   -------  ... ----------
A          P             P
B          R             Q
C          X
Note, in tblSource C matches R where as in tblTarget C doesn't match R and that is okay.

Question: Is something like this possible?

Thank you.
Avatar of Sharath S
Sharath S
Flag of United States of America image

Its possible. but jsut want to know the max. no. of columns?
Avatar of Mike Eghtebas


Lets say 10 fields. It will be wonderful if we pass it via a parameter to the stored procedure. Like:

exec abcd 'tblSource','tblTarget', 10  

try this sproc. I am creating the target table inside the sproc. no need to create it.
when you execute the sproc, it will create the target table and load the data into it.
create procedure abcd
 @tblsource VARCHAR(100)
,@tbltarget varchar(100)
declare @select varchar(max),@where varchar(max),@sql varchar(max)

-- Create the target table
select @sql = '
IF OBJECT_ID('''+@tblTarget+''') IS NOT NULL
    DROP TABLE ' + @tblTarget +'
	select * into '+@tblTarget+' from ' + @tblSource + ' where 1 = 0'

                FROM  INFORMATION_SCHEMA.columns where table_name = @tblsource
               ORDER BY   ',['+ COLUMN_NAME+']'
                 FOR XML PATH('')), 1, 1, '') 

select @select = substring(select_list,1,len(select_list)-5)
  from (
select stuff((select 'SELECT ''' + COLUMN_NAME + ''' colname,'+COLUMN_NAME+' data FROM ' + @tblsource + ' WHERE ISNULL('+COLUMN_NAME+','''') <> '''' UNION '  
                from INFORMATION_SCHEMA.columns where table_name = @tblsource
               order by COLUMN_NAME 
                 for xml path(''), 
               , 1, 0, '') as select_list) as t1 

select @sql = '
;with cte as (
select *,ROW_NUMBER() over (partition by colname order by data) rn
from ('+@select+') t1)
insert ' + @tblTarget + '
 select '+@where+'
  from cte
pivot (max(data) for colname in ('+@where+') ) p'

Open in new window

tested here:!3/1c84c/4
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Wow, this is wonderful. Thank you very much.
Hi  Sharath_123,

Once again to improve it a bit, I have a foolow up question at:

If you have couple of minutes to take a look at it.