May 6, 2004 at 12:47 am
May 6, 2004 at 7:49 am
For single result, you could try something like this,
declare @res varchar(50)
set @res=''
select @res=@res+product_name + ', ' from order_item
where orderid = '001234'
select @res
If you need for eatch orderid, develop a function based on top by passing
in orderid.
Syntax are not checked.
May 7, 2004 at 12:47 pm
Here's one solution using a user_defined function. You may need to check it for syntax, but I think it'll be right. I also have one that doesn't use a user-defined function, so let me know if you want that one instead, but this one is simpler.
CREATE FUNCTION
dbo.StringConcat(@userID int, @fieldID int)
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @String VARCHAR(255)
SELECT @String = @String + b.product_name + ', '
from orders a, order_items b
where a.orderid = b.orderid
WHERE a.orderid = @orderID
RETURN @String
END
And here’s the call of it:
SELECT a.orderidid, dbo.StringConcat(a.orderid) as newtext
from orders a, order_items b
where a.orderid = b.orderid
GROUP BY a.orderid
May 7, 2004 at 1:18 pm
Also consider if the presenation will be handled in an application it may be simpler to put the logic in the application for the rollup than in sql.
May 10, 2004 at 3:51 am
Thanks, Optimist 🙂 your solution was something that didn't occur to me, and is very helpful! I was looking for that right now, because I wanted to avoid the use of cursors in a very similar situation.
It works fine, and there is just one little thing that needs to be corrected in your SQL, and that is initial setting of @String to empty (instead of NULL): SELECT @String = ''. I'm not sure what the @fieldID parameter is for - I didn't use it and tested the function without it.
Thanks again!
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply