📅  最后修改于: 2023-12-03 15:03:03.439000             🧑  作者: Mango
In MS SQL, the PRINT statement is used to return messages to the client. By default, each message is printed on a new line. However, there may be times when you want to concatenate multiple messages into a single line. In this case, you can use the CHAR(13) and CHAR(10) characters to insert a line break.
PRINT 'Message 1' + CHAR(13) + CHAR(10) + 'Message 2'
In the above syntax, the PRINT statement is used to concatenate two messages - 'Message 1' and 'Message 2' - into a single line. CHAR(13) represents a carriage return, which moves the cursor to the beginning of the line, and CHAR(10) represents a line feed, which moves the cursor to the next line.
DECLARE @Message AS VARCHAR(100) = 'Hello World!'
PRINT 'Message 1: ' + @Message
PRINT 'Message 2: ' + @Message + CHAR(13) + CHAR(10) + 'This is a new line'
Message 1: Hello World!
Message 2: Hello World!
This is a new line
In the above example, the PRINT statement is used twice to output two different messages. In the second PRINT statement, the messages 'Hello World!' and 'This is a new line' are concatenated using the CHAR(13) and CHAR(10) characters. The resulting output shows that the second message is printed on a new line.
The PRINT statement is a useful tool for returning messages to the client in MS SQL. By using the CHAR(13) and CHAR(10) characters, you can control how the messages are displayed, concatenating them into a single line or printing them on separate lines.