INSERT from SELECT into a variable
-
Hi, In MySQL, I have basket field call item_name in my order_basket table. I want an SQL statement to take all item_name and INSERT it into a variable call param_details whch a TEXT type with a "\n" between them. something like this
DECLARE param_details text;
INSERT CONTACT(item_name, "\n" INTO param_details FROM order_basket
How can I do this please? Thanks, Jassim[^]
Technology News @ www.JassimRahma.com
-
Hi, In MySQL, I have basket field call item_name in my order_basket table. I want an SQL statement to take all item_name and INSERT it into a variable call param_details whch a TEXT type with a "\n" between them. something like this
DECLARE param_details text;
INSERT CONTACT(item_name, "\n" INTO param_details FROM order_basket
How can I do this please? Thanks, Jassim[^]
Technology News @ www.JassimRahma.com
DECLARE @var AS NVARCHAR -- or whatever you have
SELECT @var = field1 FROM table1 WHERE ... -- it should be a condition to return a single rowSkipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
-
DECLARE @var AS NVARCHAR -- or whatever you have
SELECT @var = field1 FROM table1 WHERE ... -- it should be a condition to return a single rowSkipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
but this is the issue, the item_name is in separate rows and I want to make them in one variable with a line feed, they are something like this:
Vienna Cheesecake
Caramel Crunch
Wedding Package
Graduation Sprinkles Cake
Red Velvet Cupcake
Original Cookiesin separate rows
Technology News @ www.JassimRahma.com
-
but this is the issue, the item_name is in separate rows and I want to make them in one variable with a line feed, they are something like this:
Vienna Cheesecake
Caramel Crunch
Wedding Package
Graduation Sprinkles Cake
Red Velvet Cupcake
Original Cookiesin separate rows
Technology News @ www.JassimRahma.com
DECLARE @var NVARCHAR(MAX)
SET @var = ''
SELECT @var = @var + '[LINE-BREAK]' + field1 + field2 + field3 FROM table1
SELECT @varSkipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
-
DECLARE @var NVARCHAR(MAX)
SET @var = ''
SELECT @var = @var + '[LINE-BREAK]' + field1 + field2 + field3 FROM table1
SELECT @varSkipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
not giving the expected result. Based on the code you provided[^] Shows the actual data fro the item_name and item_weight as an example[^]
Technology News @ www.JassimRahma.com
-
Hi, In MySQL, I have basket field call item_name in my order_basket table. I want an SQL statement to take all item_name and INSERT it into a variable call param_details whch a TEXT type with a "\n" between them. something like this
DECLARE param_details text;
INSERT CONTACT(item_name, "\n" INTO param_details FROM order_basket
How can I do this please? Thanks, Jassim[^]
Technology News @ www.JassimRahma.com
I think you're looking for GROUP_CONCAT()[^]. As far as I understand it, you don't need the grouping aspect of it but there doesn't seem to be an equivalent function without it. So you need to provide the GROUP BY clause with a column whose value is equal for all rows whose
item_name
you want to concatenate. Pay attention to the remark about the maximum string length returned by the function in the linked documentation.If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson