Create stored procedure that contains newlines with a oneliner sql statement





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
1
down vote

favorite












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?










share|improve this question






















  • So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
    – George.Palacios
    13 hours ago










  • Yes, that is true.
    – Anders Lindén
    13 hours ago






  • 2




    May we ask why?
    – user1008646
    12 hours ago

















up vote
1
down vote

favorite












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?










share|improve this question






















  • So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
    – George.Palacios
    13 hours ago










  • Yes, that is true.
    – Anders Lindén
    13 hours ago






  • 2




    May we ask why?
    – user1008646
    12 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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?










share|improve this question













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?







sql-server






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 13 hours ago









Anders Lindén

2171312




2171312












  • So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
    – George.Palacios
    13 hours ago










  • Yes, that is true.
    – Anders Lindén
    13 hours ago






  • 2




    May we ask why?
    – user1008646
    12 hours ago


















  • So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
    – George.Palacios
    13 hours ago










  • Yes, that is true.
    – Anders Lindén
    13 hours ago






  • 2




    May we ask why?
    – user1008646
    12 hours ago
















So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
13 hours ago




So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
13 hours ago












Yes, that is true.
– Anders Lindén
13 hours ago




Yes, that is true.
– Anders Lindén
13 hours ago




2




2




May we ask why?
– user1008646
12 hours ago




May we ask why?
– user1008646
12 hours ago










1 Answer
1






active

oldest

votes

















up vote
6
down vote













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 > 4;' + @NewLine;

PRINT @SQL;
EXEC sys.sp_executesql @SQL;





share|improve this answer





















  • @AndersLindén yep, don't put a + before EXEC.
    – sp_BlitzErik
    13 hours ago










  • I was trying to put the + in the argument to exec, building the sql string there
    – Anders Lindén
    13 hours ago






  • 1




    @Anders um, why?
    – Aaron Bertrand
    13 hours ago










  • The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
    – user1008646
    12 hours ago










  • Is there no way of escaping characters in a t-sql string?
    – Anders Lindén
    11 hours ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f223095%2fcreate-stored-procedure-that-contains-newlines-with-a-oneliner-sql-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote













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 > 4;' + @NewLine;

PRINT @SQL;
EXEC sys.sp_executesql @SQL;





share|improve this answer





















  • @AndersLindén yep, don't put a + before EXEC.
    – sp_BlitzErik
    13 hours ago










  • I was trying to put the + in the argument to exec, building the sql string there
    – Anders Lindén
    13 hours ago






  • 1




    @Anders um, why?
    – Aaron Bertrand
    13 hours ago










  • The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
    – user1008646
    12 hours ago










  • Is there no way of escaping characters in a t-sql string?
    – Anders Lindén
    11 hours ago















up vote
6
down vote













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 > 4;' + @NewLine;

PRINT @SQL;
EXEC sys.sp_executesql @SQL;





share|improve this answer





















  • @AndersLindén yep, don't put a + before EXEC.
    – sp_BlitzErik
    13 hours ago










  • I was trying to put the + in the argument to exec, building the sql string there
    – Anders Lindén
    13 hours ago






  • 1




    @Anders um, why?
    – Aaron Bertrand
    13 hours ago










  • The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
    – user1008646
    12 hours ago










  • Is there no way of escaping characters in a t-sql string?
    – Anders Lindén
    11 hours ago













up vote
6
down vote










up vote
6
down vote









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 > 4;' + @NewLine;

PRINT @SQL;
EXEC sys.sp_executesql @SQL;





share|improve this answer












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 > 4;' + @NewLine;

PRINT @SQL;
EXEC sys.sp_executesql @SQL;






share|improve this answer












share|improve this answer



share|improve this answer










answered 13 hours ago









sp_BlitzErik

20.6k1262102




20.6k1262102












  • @AndersLindén yep, don't put a + before EXEC.
    – sp_BlitzErik
    13 hours ago










  • I was trying to put the + in the argument to exec, building the sql string there
    – Anders Lindén
    13 hours ago






  • 1




    @Anders um, why?
    – Aaron Bertrand
    13 hours ago










  • The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
    – user1008646
    12 hours ago










  • Is there no way of escaping characters in a t-sql string?
    – Anders Lindén
    11 hours ago


















  • @AndersLindén yep, don't put a + before EXEC.
    – sp_BlitzErik
    13 hours ago










  • I was trying to put the + in the argument to exec, building the sql string there
    – Anders Lindén
    13 hours ago






  • 1




    @Anders um, why?
    – Aaron Bertrand
    13 hours ago










  • The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
    – user1008646
    12 hours ago










  • Is there no way of escaping characters in a t-sql string?
    – Anders Lindén
    11 hours ago
















@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
13 hours ago




@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
13 hours ago












I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
13 hours ago




I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
13 hours ago




1




1




@Anders um, why?
– Aaron Bertrand
13 hours ago




@Anders um, why?
– Aaron Bertrand
13 hours ago












The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
12 hours ago




The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
12 hours ago












Is there no way of escaping characters in a t-sql string?
– Anders Lindén
11 hours ago




Is there no way of escaping characters in a t-sql string?
– Anders Lindén
11 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f223095%2fcreate-stored-procedure-that-contains-newlines-with-a-oneliner-sql-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks