Crazy idea, but we all know how to solve this if it was a SQL problem, so let's use the tools we have! I took a sql script I had lying around, in this case it was a missing index query that calculates an improvement measure and sorts the output to show you the top 10 highest impact missing indexes on your system, and I just did a FIND & REPLACE on ' with ''. Then I put the result into my var @stringtoReplaceGOs. The rest is just simple sql to manipulate it.DECLARE @segments table (id int identity(1,1),Segment varchar(max));DECLARE @x xml;DECLARE @stringtoReplaceGOs VARCHAR(MAX);DECLARE @OutputScript VARCHAR(MAX);SET @stringtoReplaceGos =-------------------------------------------------------------------------------------------- Paste your SQL script inside the string below, remember to replace ' with '' beforehand------------------------------------------------------------------------------------------'GOSELECT top 10 REPLACE(REPLACE(mid.statement,''['',''''),'']'','''') as dbtable_name, CONVERT(BIGINT,ROUND(migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans),0)) AS improvement_measure, object_name(mid.[object_id], mid.database_id) as table_name, ''IDX_'' + LEFT (PARSENAME(mid.statement, 1), 32) + ''_'' + LEFT(REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'',''''),CASE CHARINDEX('','',REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'','''')) WHEN 0 THEN LEN(REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'','''')) ELSE CHARINDEX('','',REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'',''''))-1 END) + ''_NDX'' + CONVERT(VARCHAR(50),ROW_NUMBER() OVER (PARTITION BY mid.statement ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC)) AS INDEX_NAME, ''NONCLUSTERED'' AS INDEX_TYPE, REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'','''') as index_columns, REPLACE(REPLACE(mid.included_columns,''['',''''),'']'','''') as included_columns, migs.user_seeks, migs.user_scans, 0 as User_lookups, '''' as User_updates, ''CREATE INDEX [IDX_'' + LEFT (PARSENAME(mid.statement, 1), 32) + ''_'' + LEFT(REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'',''''),CASE CHARINDEX('','',REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'','''')) WHEN 0 THEN LEN(REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'','''')) ELSE CHARINDEX('','',REPLACE(REPLACE(ISNULL(mid.equality_columns + CASE WHEN mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END,'''') + ISNULL (mid.inequality_columns, ''''),''['',''''),'']'',''''))-1 END) + ''_NDX'' + CONVERT(VARCHAR(50),ROW_NUMBER() OVER (PARTITION BY mid.statement ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC)) + '']'' + '' ON '' + mid.statement + '' ('' + ISNULL (mid.equality_columns,'''') + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN '','' ELSE '''' END + ISNULL (mid.inequality_columns, '''') + '')'' + ISNULL ('' INCLUDE ('' + mid.included_columns + '')'', '''') AS create_index_statement, migs.*, db_name(mid.database_id), object_name(mid.[object_id], mid.database_id)FROM sys.dm_db_missing_index_groups migINNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handleINNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handleWHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10 --and database_id = 10ORDER BY --mid.statement , migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC;goPRINT(''hi'');goPRINT(''more stuff!'');GOPRINT(''more stuff!'');GO'-------------------------------------------------------------------------------------------- Your SQL Script should end here with a '------------------------------------------------------------------------------------------SELECT @X = CONVERT(XML,'<root><s>' + REPLACE(@stringtoReplaceGOs,'','</s><s>') + '</s></root>')INSERT INTO @segments(segment)SELECT T.c.value('./text()[1]','varchar(max)') FROM @X.nodes('/root/s') T(c);UPDATE segSET Segment = 'SELECT ' + CONVERT(VARCHAR(MAX),id) + ' as Line, GETDATE() as CurrentDateTime;'FROM @segments as segWHERE Segment LIKE 'GO%';SELECT @OutputScript=COALESCE(@OutputScript,'') + COALESCE(Segment,'') + ''FROM @segmentsORDER BY id;SELECT @OutputScript;EXEC (@OutputScript);
* EDIT: Added comments to clarify where the code goes