I am attempting to dynamically create a document in SSIS. The rows colunms vary from time to time. I was wondering how this can be done. I just need an instruction as to how this can be done. The Execute TSQL task is necessary because I am using dynamic SQL and the old Execute SQL task doesn't allow for dynamic SQL.
Below are my steps I'm taking within the TSQL Task:
Declare @dynaView varchar(max)
Declare @SQLQuestions varchar(max)
Declare @SQLQuestions2 varchar(max)
Declare @SQLQuestions3 varchar(max)
Set @SQLQuestions = ''
Set @SQLQuestions2 = ''
Set @SQLQuestions3 = ''
SELECT @SQLQuestions = @SQLQuestions + '
isnull(['+ shortdesc +'], ''' + response + ''') as ''' + replace(PrintDesc,'''','''''') + ''',',
@SQLQuestions2 = @SQLQuestions2 + '
['+ shortdesc +'],',
@SQLQuestions3 = @SQLQuestions3 + '
isnull(['+ PrintDesc +'], ''' + response + ''') as ''' + replace(PrintDesc,'''','''''') + ''','
FROM [Questions]
SELECT @SQLQuestions = SUBSTRING(@SQLQuestions, 1, LEN(@SQLQuestions)-1)
SELECT @SQLQuestions2 = SUBSTRING(@SQLQuestions2, 1, LEN(@SQLQuestions2)-1)
SELECT @SQLQuestions3 = SUBSTRING(@SQLQuestions3, 1, LEN(@SQLQuestions3)-1)
Set @dynaView = ''
Set @dynaView = '
Select locationid as fklocationid,' + @SQLQuestions + '
From
(select * from Tmp_ProfileDF) S
PIVOT
(Max(Response)
FOR ShortDesc
IN (' + @SQLQuestions2 + '))P
Group By LocationID, ' + @SQLQuestions2 + ''
Declare @SQL1 varchar(max)
Set @SQL1 = ''
SELECT @SQL1 ='SELECT location_id,
' + @SQLQuestions3 + '
FROM Tmp_Location_Insert as l
left join (' + @dynaView + ') p on p.fklocationid = l.location_id
Order BY l.Location_Id
'
INSERT INTO [DealerDestination]
EXEC(@sql1)
The Final Insert into DealerDestination may vary in columns. The DealerDestination is going to be used to create a flat file destination after the TSQL task is done.