Create stored procedure that contains newlines with a oneliner sql statement I want to put code in a single line that will create a stored procedure that contains newlines.


Is that possible?
Do I need to use sp_executesql?
How do I escape newlines in a sql statement?
How do I escape newlines in a string?
Yes, you can do something like this with dynamic SQL:

DECLARE @SQL NVARCHAR(MAX) = N'';
DECLARE @NewLine NCHAR(1) = NCHAR(10);

SET @SQL = @SQL + N'SELECT * ' + @NewLine + N'FROM sys.databases AS d ' + @NewLine + N'WHERE d.database_id &gt; 4;' + @NewLine;

PRINT @SQL;
EXEC sys.sp_executesql @SQL;