Create stored procedure that contains newlines with a oneliner sql statement
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}
up vote
2
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?
sql-server t-sql stored-procedures dynamic-sql
add a comment |
up vote
2
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?
sql-server t-sql stored-procedures dynamic-sql
So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
2 days ago
Yes, that is true.
– Anders Lindén
2 days ago
2
May we ask why?
– user1008646
2 days ago
To get one command per line resulting in a cleaner file.
– Anders Lindén
yesterday
add a comment |
up vote
2
down vote
favorite
up vote
2
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?
sql-server t-sql stored-procedures dynamic-sql
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 t-sql stored-procedures dynamic-sql
sql-server t-sql stored-procedures dynamic-sql
edited yesterday
Solomon Rutzky
47k579169
47k579169
asked 2 days ago
Anders Lindén
2211312
2211312
So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
2 days ago
Yes, that is true.
– Anders Lindén
2 days ago
2
May we ask why?
– user1008646
2 days ago
To get one command per line resulting in a cleaner file.
– Anders Lindén
yesterday
add a comment |
So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
2 days ago
Yes, that is true.
– Anders Lindén
2 days ago
2
May we ask why?
– user1008646
2 days ago
To get one command per line resulting in a cleaner file.
– Anders Lindén
yesterday
So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
2 days ago
So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
2 days ago
Yes, that is true.
– Anders Lindén
2 days ago
Yes, that is true.
– Anders Lindén
2 days ago
2
2
May we ask why?
– user1008646
2 days ago
May we ask why?
– user1008646
2 days ago
To get one command per line resulting in a cleaner file.
– Anders Lindén
yesterday
To get one command per line resulting in a cleaner file.
– Anders Lindén
yesterday
add a comment |
2 Answers
2
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;
@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
2 days ago
I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
2 days ago
1
@Anders um, why?
– Aaron Bertrand♦
2 days ago
The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
2 days ago
Is there no way of escaping characters in a t-sql string?
– Anders Lindén
2 days ago
|
show 1 more comment
up vote
0
down vote
No, T-SQL cannot escape newlines, tabs, etc. It only has an escape sequence for embedded string delimiters: ''
= '
. There is also an escape sequence for delimited identifiers: either ""
for "
, or ]]
for ]
, depending on which one is being used to delimit the identifier.
If you want to work with escape sequences, you can do so manually via the REPLACE
function.
Depending on how the "single line of code" is being interpreted, you can do either:
IF (OBJECT_ID(N'tempdb..#TestProc') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc;
END;
GO
-- The following is technically a single line (with multiple commands):
DECLARE @SQL NVARCHAR(MAX) = N'CREATE PROCEDURE #TestProcn(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''tempdb..'' + @Param2);'; SET @SQL = REPLACE(@SQL, N'n', NCHAR(10)); EXEC (@SQL);
-- View the definition:
PRINT @SQL;
-- Test the proc:
EXEC #TestProc 2, N'#TestProc';
Or, if you can only execute a single command/statement, then you can try the following:
IF (OBJECT_ID(N'tempdb..#TestProc2') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc2;
END;
GO
-- The following is a single line and a single statement:
EXEC (N'DECLARE @SQL NVARCHAR(MAX) = N''CREATE PROCEDURE #TestProc2n(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''''tempdb..'''' + @Param2);''; SET @SQL = REPLACE(@SQL, N''n'', NCHAR(10)); EXEC (@SQL);');
-- Test the proc:
EXEC #TestProc2 2, N'#TestProc2';
-- View the definition:
DECLARE @SQL2 NVARCHAR(MAX);
SELECT @SQL2 = [definition]
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..#TestProc2');
PRINT @SQL2;
-- returns (in "Messages" tab):
/*
CREATE PROCEDURE #TestProc2
(
@Param1 INT,
@Param2 NVARCHAR(128)
)
AS
SET NOCOUNT ON;
SELECT *
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..' + @Param2);
*/
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
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;
@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
2 days ago
I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
2 days ago
1
@Anders um, why?
– Aaron Bertrand♦
2 days ago
The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
2 days ago
Is there no way of escaping characters in a t-sql string?
– Anders Lindén
2 days ago
|
show 1 more comment
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;
@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
2 days ago
I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
2 days ago
1
@Anders um, why?
– Aaron Bertrand♦
2 days ago
The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
2 days ago
Is there no way of escaping characters in a t-sql string?
– Anders Lindén
2 days ago
|
show 1 more comment
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;
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;
answered 2 days ago
sp_BlitzErik
20.6k1262102
20.6k1262102
@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
2 days ago
I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
2 days ago
1
@Anders um, why?
– Aaron Bertrand♦
2 days ago
The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
2 days ago
Is there no way of escaping characters in a t-sql string?
– Anders Lindén
2 days ago
|
show 1 more comment
@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
2 days ago
I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
2 days ago
1
@Anders um, why?
– Aaron Bertrand♦
2 days ago
The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
2 days ago
Is there no way of escaping characters in a t-sql string?
– Anders Lindén
2 days ago
@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
2 days ago
@AndersLindén yep, don't put a + before EXEC.
– sp_BlitzErik
2 days ago
I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
2 days ago
I was trying to put the + in the argument to exec, building the sql string there
– Anders Lindén
2 days ago
1
1
@Anders um, why?
– Aaron Bertrand♦
2 days ago
@Anders um, why?
– Aaron Bertrand♦
2 days ago
The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
2 days ago
The parameter of sp_executesql must be either a variable or a constant. No expressions allowed.
– user1008646
2 days ago
Is there no way of escaping characters in a t-sql string?
– Anders Lindén
2 days ago
Is there no way of escaping characters in a t-sql string?
– Anders Lindén
2 days ago
|
show 1 more comment
up vote
0
down vote
No, T-SQL cannot escape newlines, tabs, etc. It only has an escape sequence for embedded string delimiters: ''
= '
. There is also an escape sequence for delimited identifiers: either ""
for "
, or ]]
for ]
, depending on which one is being used to delimit the identifier.
If you want to work with escape sequences, you can do so manually via the REPLACE
function.
Depending on how the "single line of code" is being interpreted, you can do either:
IF (OBJECT_ID(N'tempdb..#TestProc') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc;
END;
GO
-- The following is technically a single line (with multiple commands):
DECLARE @SQL NVARCHAR(MAX) = N'CREATE PROCEDURE #TestProcn(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''tempdb..'' + @Param2);'; SET @SQL = REPLACE(@SQL, N'n', NCHAR(10)); EXEC (@SQL);
-- View the definition:
PRINT @SQL;
-- Test the proc:
EXEC #TestProc 2, N'#TestProc';
Or, if you can only execute a single command/statement, then you can try the following:
IF (OBJECT_ID(N'tempdb..#TestProc2') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc2;
END;
GO
-- The following is a single line and a single statement:
EXEC (N'DECLARE @SQL NVARCHAR(MAX) = N''CREATE PROCEDURE #TestProc2n(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''''tempdb..'''' + @Param2);''; SET @SQL = REPLACE(@SQL, N''n'', NCHAR(10)); EXEC (@SQL);');
-- Test the proc:
EXEC #TestProc2 2, N'#TestProc2';
-- View the definition:
DECLARE @SQL2 NVARCHAR(MAX);
SELECT @SQL2 = [definition]
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..#TestProc2');
PRINT @SQL2;
-- returns (in "Messages" tab):
/*
CREATE PROCEDURE #TestProc2
(
@Param1 INT,
@Param2 NVARCHAR(128)
)
AS
SET NOCOUNT ON;
SELECT *
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..' + @Param2);
*/
add a comment |
up vote
0
down vote
No, T-SQL cannot escape newlines, tabs, etc. It only has an escape sequence for embedded string delimiters: ''
= '
. There is also an escape sequence for delimited identifiers: either ""
for "
, or ]]
for ]
, depending on which one is being used to delimit the identifier.
If you want to work with escape sequences, you can do so manually via the REPLACE
function.
Depending on how the "single line of code" is being interpreted, you can do either:
IF (OBJECT_ID(N'tempdb..#TestProc') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc;
END;
GO
-- The following is technically a single line (with multiple commands):
DECLARE @SQL NVARCHAR(MAX) = N'CREATE PROCEDURE #TestProcn(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''tempdb..'' + @Param2);'; SET @SQL = REPLACE(@SQL, N'n', NCHAR(10)); EXEC (@SQL);
-- View the definition:
PRINT @SQL;
-- Test the proc:
EXEC #TestProc 2, N'#TestProc';
Or, if you can only execute a single command/statement, then you can try the following:
IF (OBJECT_ID(N'tempdb..#TestProc2') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc2;
END;
GO
-- The following is a single line and a single statement:
EXEC (N'DECLARE @SQL NVARCHAR(MAX) = N''CREATE PROCEDURE #TestProc2n(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''''tempdb..'''' + @Param2);''; SET @SQL = REPLACE(@SQL, N''n'', NCHAR(10)); EXEC (@SQL);');
-- Test the proc:
EXEC #TestProc2 2, N'#TestProc2';
-- View the definition:
DECLARE @SQL2 NVARCHAR(MAX);
SELECT @SQL2 = [definition]
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..#TestProc2');
PRINT @SQL2;
-- returns (in "Messages" tab):
/*
CREATE PROCEDURE #TestProc2
(
@Param1 INT,
@Param2 NVARCHAR(128)
)
AS
SET NOCOUNT ON;
SELECT *
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..' + @Param2);
*/
add a comment |
up vote
0
down vote
up vote
0
down vote
No, T-SQL cannot escape newlines, tabs, etc. It only has an escape sequence for embedded string delimiters: ''
= '
. There is also an escape sequence for delimited identifiers: either ""
for "
, or ]]
for ]
, depending on which one is being used to delimit the identifier.
If you want to work with escape sequences, you can do so manually via the REPLACE
function.
Depending on how the "single line of code" is being interpreted, you can do either:
IF (OBJECT_ID(N'tempdb..#TestProc') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc;
END;
GO
-- The following is technically a single line (with multiple commands):
DECLARE @SQL NVARCHAR(MAX) = N'CREATE PROCEDURE #TestProcn(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''tempdb..'' + @Param2);'; SET @SQL = REPLACE(@SQL, N'n', NCHAR(10)); EXEC (@SQL);
-- View the definition:
PRINT @SQL;
-- Test the proc:
EXEC #TestProc 2, N'#TestProc';
Or, if you can only execute a single command/statement, then you can try the following:
IF (OBJECT_ID(N'tempdb..#TestProc2') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc2;
END;
GO
-- The following is a single line and a single statement:
EXEC (N'DECLARE @SQL NVARCHAR(MAX) = N''CREATE PROCEDURE #TestProc2n(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''''tempdb..'''' + @Param2);''; SET @SQL = REPLACE(@SQL, N''n'', NCHAR(10)); EXEC (@SQL);');
-- Test the proc:
EXEC #TestProc2 2, N'#TestProc2';
-- View the definition:
DECLARE @SQL2 NVARCHAR(MAX);
SELECT @SQL2 = [definition]
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..#TestProc2');
PRINT @SQL2;
-- returns (in "Messages" tab):
/*
CREATE PROCEDURE #TestProc2
(
@Param1 INT,
@Param2 NVARCHAR(128)
)
AS
SET NOCOUNT ON;
SELECT *
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..' + @Param2);
*/
No, T-SQL cannot escape newlines, tabs, etc. It only has an escape sequence for embedded string delimiters: ''
= '
. There is also an escape sequence for delimited identifiers: either ""
for "
, or ]]
for ]
, depending on which one is being used to delimit the identifier.
If you want to work with escape sequences, you can do so manually via the REPLACE
function.
Depending on how the "single line of code" is being interpreted, you can do either:
IF (OBJECT_ID(N'tempdb..#TestProc') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc;
END;
GO
-- The following is technically a single line (with multiple commands):
DECLARE @SQL NVARCHAR(MAX) = N'CREATE PROCEDURE #TestProcn(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''tempdb..'' + @Param2);'; SET @SQL = REPLACE(@SQL, N'n', NCHAR(10)); EXEC (@SQL);
-- View the definition:
PRINT @SQL;
-- Test the proc:
EXEC #TestProc 2, N'#TestProc';
Or, if you can only execute a single command/statement, then you can try the following:
IF (OBJECT_ID(N'tempdb..#TestProc2') IS NOT NULL)
BEGIN
DROP PROCEDURE #TestProc2;
END;
GO
-- The following is a single line and a single statement:
EXEC (N'DECLARE @SQL NVARCHAR(MAX) = N''CREATE PROCEDURE #TestProc2n(n @Param1 INT,n @Param2 NVARCHAR(128)n)nASnSET NOCOUNT ON;nnSELECT *nFROM tempdb.sys.sql_modulesnWHERE [object_id] = OBJECT_ID(N''''tempdb..'''' + @Param2);''; SET @SQL = REPLACE(@SQL, N''n'', NCHAR(10)); EXEC (@SQL);');
-- Test the proc:
EXEC #TestProc2 2, N'#TestProc2';
-- View the definition:
DECLARE @SQL2 NVARCHAR(MAX);
SELECT @SQL2 = [definition]
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..#TestProc2');
PRINT @SQL2;
-- returns (in "Messages" tab):
/*
CREATE PROCEDURE #TestProc2
(
@Param1 INT,
@Param2 NVARCHAR(128)
)
AS
SET NOCOUNT ON;
SELECT *
FROM tempdb.sys.sql_modules
WHERE [object_id] = OBJECT_ID(N'tempdb..' + @Param2);
*/
answered yesterday
Solomon Rutzky
47k579169
47k579169
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
So you need to include carriage return and new line characters in your stored procedure, but not in your CREATE statement?
– George.Palacios
2 days ago
Yes, that is true.
– Anders Lindén
2 days ago
2
May we ask why?
– user1008646
2 days ago
To get one command per line resulting in a cleaner file.
– Anders Lindén
yesterday