I frequently find myself writing SQL that generates SQL. On SQL Server, I like to build a string that contains the SQL statement followed by “GO”. To do that I use CHAR(13)+CHAR(10)+’GO’+CHAR(13)+CHAR(10) at the end of the string e.g.
{some_generated_SQL}+CHAR(13)+CHAR(10)+’GO’+CHAR(13)+CHAR(10)
That way, when I paste the query results into SSMS or a text editor I get:
1 2 3 4 5 | {first_generated_SQL} GO {second_generated_SQL} GO ... |