Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Concatenate Output Param

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2014-11-26 : 11:30:45
I have tried several variations but just can't seem to get it working right.
It will return an empty string:


CREATE PROCEDURE [dbo].[AddInfo]

@UserID VARCHAR(10),
@Chemicals dbo.chemical_list_tbltype READONLY,
@msg VARCHAR(MAX) OUTPUT,
@errMessage VARCHAR(512) OUTPUT

AS
BEGIN
DECLARE @Name VARCHAR(256)
DECLARE @RowsToProcess INT
DECLARE @CurrentRow INT

SET @msg = '' - set to empty string first
SET @RowsToProcess = 10
SET @CurrentRow = 0

WHILE @CurrentRow < @RowsToProcess
BEGIN
SET @CurrentRow = @CurrentRow + 1
SELECT
@Name = 'Ethanol' --Hard coded for testing
IF NOT EXISTS(SELECT 1 FROM Chemicals WHERE Name = @Name)
BEGIN
INSERT INTO Chemicals(Name) VALUES(@Name)
END
ELSE
BEGIN
--DECLARE @tmp VARCHAR(MAX)
--SET @tmp = @msg
--SET @msg = @tmp + @Name --Returns empty string

--SET @msg = @msg + @Name + '<br />' --Returns empty string
SET @msg += @Name --Returns empty string
--SET @msg += 'test' --Returns empty string
--SET @msg = 'test' --This works
--SET @msg += '<br />' --Returns empty string
END

END
END

GO

I am positive the Names I am inputting are repeats and it does hit the else statement.

Why will this not work?

Thanks!

PROBLEM SOLVED

Decided to check something so I set @msg = 'test' before the while loop.
Then, what I needed worked.
So, I set it to SET @msg = ''
I suppose you need to set the parameter from NULL to empty string first.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-11-26 : 12:19:23
Please post your actual code. It is incomplete. To test whether or not it is in the IF or ELSE, add a PRINT statement.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2014-11-26 : 13:20:26
I adjusted my question to show all the needed code.
I tested the edited code and it still brings back an empty string.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-11-26 : 13:33:30
I think you need to set @msg to an empty string as it's NULL, and the default setting of CONCAT_NULL_YIELDS_NULL will mean that it is NULL even if you append other things to it. You can either set @msg to '' or change CONCAT_NULL_YIELDS_NULL.

http://msdn.microsoft.com/en-us/library/ms176056.aspx

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2014-11-26 : 13:35:19
Yes, thanks! I just discovered that and was posting an edit.
Thanks for your input!!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-11-26 : 13:55:18


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -