0

My select query returns a list of 10 rows.

I tried use a COALESCE, but the exception throws "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

My query:

DECLARE @sqlDetails NVARCHAR(MAX);
SET @sqlDetails = COALESCE( @sqlDetails + ' ', '') + (  SELECT
                                                        rp.RuleDetails 
                                                    FROM 
                                                        RuleBook rb, 
                                                        RuleException re, 
                                                        RuleParameters rp 
                                                    WHERE 
                                                        rb.RuleBookID = 57 AND
                                                        rb.RuleBookID = re.RuleBookID AND 
                                                        rp.RuleBookID = re.RuleBookID AND
                                                        rp.RuleDetails NOT LIKE '%TRIGGER%' AND
                                                        rp.RuleDetails NOT LIKE '%EmailTo%');

I need concatenate the return into ONE single row, How I can do this?

Thanks.

1
  • what your select block gives you in result? (select rp.RuleDetails), it give single record OR Multiple? Commented Apr 28, 2017 at 12:25

2 Answers 2

1

Try using STUFF AND FOR XML PATH see How Stuff and 'For Xml Path' work in Sql Server

SET @sqlDetails = STUFF((SELECT ','+RuleDetails FROM RuleBook rb, RuleException re, RuleParameters rp 
where rb.RuleBookID = 57 AND rb.RuleBookID = re.RuleBookID AND rp.RuleBookID = re.RuleBookID AND rp.RuleDetails NOT LIKE '%TRIGGER%' AND rp.RuleDetails NOT LIKE '%EmailTo%'FOR XML PATH('')) , 1 , 1 , '' )
Sign up to request clarification or add additional context in comments.

Comments

0

Hey Jose as you are trying the get the data into a variable, my assumption is that you need only a single record. As a Variable cannot store multiple values.

Please see if the below query helps your requirement.

DECLARE @sqlDetails NVARCHAR(MAX);
SET @sqlDetails = COALESCE( @sqlDetails + ' ', '') + (  SELECT
                                                        TOP 1 rp.RuleDetails 
                                                    FROM 
                                                        RuleBook rb, 
                                                        RuleException re, 
                                                        RuleParameters rp 
                                                    WHERE 
                                                        rb.RuleBookID = 57 AND
                                                        rb.RuleBookID = re.RuleBookID AND 
                                                        rp.RuleBookID = re.RuleBookID AND
                                                        rp.RuleDetails NOT LIKE '%TRIGGER%' AND
                                                        rp.RuleDetails NOT LIKE '%EmailTo%');

SELECT @sqlDetails

If your requirement is to pull all the records from the SubQuery and form a string separated by comma you can use the below query. This is similar to what Nelsonrr had mentioned previously

DECLARE @sqlDetails NVARCHAR(MAX);
SET @sqlDetails =(SELECT @sqlDetails ) + ',' + (SELECT STUFF((SELECT ','+
                                                        rp.RuleDetails 
                                                    FROM 
                                                        RuleBook rb, 
                                                        RuleException re, 
                                                        RuleParameters rp 
                                                    WHERE 
                                                        rb.RuleBookID = 57 AND
                                                        rb.RuleBookID = re.RuleBookID AND 
                                                        rp.RuleBookID = re.RuleBookID AND
                                                        rp.RuleDetails NOT LIKE '%TRIGGER%' AND
                                                        rp.RuleDetails NOT LIKE '%EmailTo%'FOR XML PATH('')) , 1 , 1 , ''));

SELECT @sqlDetails

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.