I am trying to pass a parameter [e.g. @X nvarchar(MAX)] into a variable [e.g. @message nvarchar(MAX)] inside a stored procedure. The variable is using CONCAT to combine string values and variables, and it ultimately becomes both the body of an email that is sent from SQL Server and the value of a column in a table. For some reason, when I set the variable, the CONCAT ends immediately after the first parameter is passed to it.
One extra detail is that it works fine when I run the stored procedure manually. In this particular case, this issue only appears when the stored procedure is called from an external application.
I have tried the following options without success:
- Setting the variable without
CONCATusing triple single quotes - Passing the parameter to a separate variable prior to the @message variable
- Setting the parameter to allow for OUTPUT to the original application (even though the original application does not need the OUTPUT)
ALTER PROCEDURE [dbo].[TEST]
(@param NVARCHAR(MAX))
AS
BEGIN
SET NOCOUNT ON;
DECLARE @message NVARCHAR(MAX),
@email NVARCHAR(MAX)
SET @email = 'test@test.com'
SET @message = CONCAT ('Some text.',CHAR(13) + CHAR(10)
,'Value of param: ', @param, CHAR(13) + CHAR(10)
,'Some more text.');
INSERT INTO [database].[dbo].[table] ([Message], [Param])
VALUES (@message, @param);
EXEC msdb.dbo.sp_send_dbmail
@recipients = @email,
@body = @message,
@subject = 'SUBJECT OF EMAIL',
@profile_name = 'profile_name';
The email is sent fine, and the message is stored to [table], but in both cases, the @message variable ends right after the @param is passed to it.
For example, if the value of @param was "paramTest", @message would look like this:
Some text.
Value of param: paramTest
Also, the param is stored fine to [table], so I know that the parameter has a valid value.
Is there a way to capture and see the exact data that is being passed from the external application to the stored procedure? I have been wondering if it is including some sort of special character(s) at the end of the data when it assigns the parameter.