Sorry I'm a little bit late posting my answer to the "homework". Lot's of people had posted correct answers already so I didn't feel all that rushed to post my answer :-).
DECLARE @topsql nvarchar(200)
SET @topsql =
'DECLARE @quotedvar nvarchar(100) ' + char(13) +
'DECLARE @sql nvarchar(1000) ' + char(13) +
'' + char(13) +
'SET @quotedvar = ''O''''Neil''' + char(13) +
'' + char(13) +
'SET @sql = ''PRINT '''''' + REPLACE(@quotedvar,'''''''','''''''''''') + ''''''''' + char(13) +
'' + char(13) +
'PRINT @sql' + char(13) +
'' + char(13) +
'EXEC sp_executesql @sql'
PRINT @topsql
PRINT '-------'
EXEC sp_executesql @topsql
The best test for the correct answer is of course to run it and see if the output works 🙂
Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]