drop table #a go create table #a (i int, j int, BlockType varchar(25), s varchar(1000)) delete #a declare @i int insert #a select top 1 id, id, 'DatabaseConnection', s from ExportPackageTest where s like '%DTS:ConnectionString=%' insert #a select top 1 id, id, 'FileConnection', s from ExportPackageTest where s like '%DTS:ConnectionString=%' and id > (select max(i) from #a) select top 1 @i = id+1 from ExportPackageTest where s like '% (select max(i) from #a) insert #a select top 1 id, id, 'FileCol1', s from ExportPackageTest where id = @i select top 1 @i = id from ExportPackageTest where s like '%%' and id > (select max(i) from #a) update #a set j = @i-1 where i = (select max(i) from #a) select top 1 @i = id+1 from ExportPackageTest where s like '% (select max(i) from #a) insert #a select top 1 id, id, 'TableCol1', s from ExportPackageTest where id = @i select top 1 @i = id from ExportPackageTest where s like '%%' and id > (select max(i) from #a) update #a set j = @i-1 where i = (select max(i) from #a) select top 1 @i = id+2 from ExportPackageTest where s like '% (select max(i) from #a) insert #a select top 1 id, id, 'FileCol2', s from ExportPackageTest where id = @i select top 1 @i = id from ExportPackageTest where s like '%%' and id > (select max(i) from #a) update #a set j = @i-1 where i = (select max(i) from #a) select top 1 @i = id+1 from ExportPackageTest where s like '%description="Specifies the name of the database object used to open a rowset."%' and id > (select max(i) from #a) insert #a select id, id, 'TableName', s from ExportPackageTest where id = @i select top 1 @i = id+1 from ExportPackageTest where s like '%%' and id > (select max(i) from #a) insert #a select id, id, 'ColMap1', s from ExportPackageTest where id = @i select top 1 @i = id from ExportPackageTest where s like '%%' and id > (select max(i) from #a) update #a set j = @i-1 where i = (select max(i) from #a) select top 1 @i = id+2 from ExportPackageTest where s like '% (select max(i) from #a) insert #a select id, id, 'ColMap2', s from ExportPackageTest where id = @i select top 1 @i = id from ExportPackageTest where s like '%%' and id > (select max(i) from #a) update #a set j = @i-1 where i = (select max(i) from #a) select top 1 @i = id+1 from ExportPackageTest where s like '%%' and id > (select max(i) from #a) insert #a select id, id, 'TableCol2', s from ExportPackageTest where id = @i select top 1 @i = id from ExportPackageTest where s like '% (select max(i) from #a) update #a set j = @i-1 where i = (select max(i) from #a) drop table Export_Package_Template go create table Export_Package_Template ( seq int primary key , s varchar(1000) ) go drop table Export_Package_Template_Replace go create table Export_Package_Template_Replace ( seq1 int , seq2 int , Type1 varchar(25) , Type2 varchar(25) , s varchar(1000) , primary key (seq1, seq2) ) go insert Export_Package_Template_Replace select b.i, a.id, b.BlockType , case when BlockType in ('FileCol1','FileCol2','ColMap1','ColMap2','TableCol1','TableCol2') then 'varchar' else '' end , case when BlockType = 'DatabaseConnection' then 'DTS:ConnectionString="Data Source=||ServerName||;Initial Catalog=||DatabaseName||;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;" />' when BlockType = 'FileConnection' then 'DTS:ConnectionString="||FileName||">' when BlockType = 'TableName' then 'name="OpenRowset">[||SchemName||].[||TableName||]' when BlockType in ('FileCol1','FileCol2','ColMap1','ColMap2','TableCol1','TableCol2') then replace(replace(replace(a.s,'c001','||colname||'),'20','||colsize||'),'_x000D__x000A_','||coldelim||') else a.s end from ExportPackageTest a join #a b on a.id between b.i and b.j insert Export_Package_Template select a.id, a.s from ExportPackageTest a left join #a b on a.id between b.i and b.j where b.i is null /* Results select * from #a i j BlockType s ----------- ----------- ------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------- 26 26 DatabaseConnection DTS:ConnectionString="Data Source=DESKTOP-D6M6JCB;Initial Catalog=SSIS_Test;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;" /> 43 43 FileConnection DTS:ConnectionString="C:\Users\nigel\OneDrive\Nigel\Articles\SSIS_FileExport\FileExport_c001.txt"> 45 53 FileCol1 [dbo].[ExportTest_c001] 187 197 ColMap1 2 16 8 18 19 24 25 28 29 34 35 54 55 56 57 58 59 60 61 69 70 71 73 74 81 82 true 86 91 92 93 98 99 100 104 113 114 122 123 124 125 126 134 135 0 139 147 152 156 1252 160 false 164 0 169 173 174 175 181 182 183 186 198 199 207 208 209 213 221 227 233 234 235 236 237 238 239 240 245 246 247 248 249 250 251 252 253 254 256 257 259 260 262 266 267 268 269 271 272 278 282 285 286 291 292 294 296 297 298 299 300 301 302 303 304 305 306 307 309 310 311 DataSourceViewID 312 313 314 TableInfoObjectType 315 Table 317 318 319 320 ]]> 321 select * from Export_Package_Template_Replace Result seq1 seq2 Type1 Type2 s ----------- ----------- ------------------------- ------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------- 26 26 DatabaseConnection DTS:ConnectionString="Data Source=||ServerName||;Initial Catalog=||DatabaseName||;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;" /> 43 43 FileConnection DTS:ConnectionString="||FileName||"> 45 45 FileCol1 varchar 105 105 TableCol1 varchar 116 116 FileCol2 varchar 142 142 TableName name="OpenRowset">[||SchemName||].[||TableName||] 187 187 ColMap1 varchar 201 201 ColMap2 varchar 214 214 TableCol2 varchar */