While I came here looking for the answer to your direct question, that is "How do I capture the output of the print command", I want to expand upon the prior answer.
If you are building an sql command using multiple print statements you can
concatenate the pieces in a set command
SET @sqlCommand = @sqlCommand + @newPart;
It's unfortunate but sometimes I run into problems because sqlserver thinks I've exceeded the max size for @sqlCommand when I have not. I get around this by holding the pieces separately, I often need varations of the same piece anyway, then concatenate them in the exec command like this:
EXEC (
'MERGE ' + @targetTable + ' t ' +
'USING ' + @sourceTable + ' s ' +
'ON ' + @keycondition + ' ' +
'WHEN MATCHED AND ' + @updateCondition + ' THEN ' +
'UPDATE SET ' + @updateColumns + ' ' +
'WHEN NOT MATCHED BY TARGET THEN ' +
'INSERT (' + @insertTargetColumns + ') ' +
'VALUES (' + @insertSourceColumns + ') ' +
'WHEN NOT MATCHED BY SOURCE THEN '
'UPDATE SET deleteFlag = ''D'', updDtTm = GETDATE()'
)
Hope this helps. I'm moving away from snapshots to incremental updates so the sourceTable has deleteFlag in it and I don't use the last clause.