Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple rows into one variable in SQL Query

SELECT ft.ThreadTitle AS Title, 
fr.ReplyText + ' ' + ua2.Username + ' ' + ft.ThreadText + ' '  +

-- THIS NEXT LINE IS WHAT I WANT TO ACHIEVE:

(Select ReplyText from ForumReply Where ThreadID=ft.ThreadID) 

-- THE ABOVE LINE HAVE MULTIPLE ROWS/VALUES THAT I WANT TO JOIN INTO ONE VARIABLE. HOW?
AS [Content], 

ss.Domain, 
ss.SiteID, 
ft.ThreadID AS ObjectId
FROM         dbo.ForumReply AS fr INNER JOIN
                      dbo.ForumThreads AS ft ON fr.ThreadID = ft.ThreadID INNER JOIN
                      dbo.User_Account AS ua1 ON ft.CreateByUserID = ua1.UserID INNER JOIN
                      dbo.User_Account AS ua2 ON fr.ReplyUserID = ua2.UserID INNER JOIN
                      dbo.SysT_Site AS ss ON ua1.SiteID = ss.SiteID

This query gives error: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

How do I rewrite this query to work so that I get all the values into one variable? The end result need to be a "View" that looks like this:

.ObjectID int

.Content (string with all text that exists in the Forumthread.threadText and forumReply.ReplyText)

.Domain string

.SiteID int

like image 474
Joakim Avatar asked Mar 10 '11 16:03

Joakim


People also ask

How do I select multiple rows in a single column in SQL?

In this case, we use GROUP_CONCAT function to concatenate multiple rows into one column. GROUP_CONCAT concatenates all non-null values in a group and returns them as a single string. If you want to avoid duplicates, you can also add DISTINCT in your query.

How do I convert multiple rows to one row in SQL?

You can concatenate rows into single string using COALESCE method. This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.

Can I insert multiple rows in one query in SQL?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

Can you assign multiple values to a variable in SQL?

Assigning multiple values to multiple variablesIf you have to populate multiple variables, instead of using separate SET statements each time consider using SELECT for populating all variables in a single statement. This can be used for populating variables directly or by selecting values from database.


3 Answers

Building upon Martin's comment:

DECLARE @t TABLE  (id int, ReplyText varchar(100))
INSERT INTO @t (id, ReplyText) VALUES (1, 'So Long,')
INSERT INTO @t (id, ReplyText) VALUES  (2, 'And Thanks for')
INSERT INTO @t (id, ReplyText) VALUES  (3, 'All the Fish!')

SELECT (SELECT replytext + ' '  FROM @t FOR XML PATH('')) AS CONTENT
like image 135
jfollas Avatar answered Oct 19 '22 05:10

jfollas


You can assign to local variable this way.

SELECT @TempVariable = (SELECT replytext + ' ' FROM @t FOR XML PATH(''))
like image 2
Khuzaima Shajapurwala Avatar answered Oct 19 '22 06:10

Khuzaima Shajapurwala


What you need is a string concat aggregate function, which sql server unfortunuatley does not include.

You can create your own though using .Net CLr function see here and here

like image 1
Richard Friend Avatar answered Oct 19 '22 05:10

Richard Friend