Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. INSERT from SELECT into a variable

INSERT from SELECT into a variable

Scheduled Pinned Locked Moved Database
questiondatabasemysqlcomannouncement
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    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

    Kornfeld Eliyahu PeterK S 2 Replies Last reply
    0
    • J Jassim Rahma

      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

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      DECLARE @var AS NVARCHAR -- or whatever you have
      SELECT @var = field1 FROM table1 WHERE ... -- it should be a condition to return a single row

      Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      J 1 Reply Last reply
      0
      • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

        DECLARE @var AS NVARCHAR -- or whatever you have
        SELECT @var = field1 FROM table1 WHERE ... -- it should be a condition to return a single row

        Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

        J Offline
        J Offline
        Jassim Rahma
        wrote on last edited by
        #3

        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 Cookies

        in separate rows

        Technology News @ www.JassimRahma.com

        Kornfeld Eliyahu PeterK 1 Reply Last reply
        0
        • J Jassim Rahma

          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 Cookies

          in separate rows

          Technology News @ www.JassimRahma.com

          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu Peter
          wrote on last edited by
          #4

          DECLARE @var NVARCHAR(MAX)
          SET @var = ''
          SELECT @var = @var + '[LINE-BREAK]' + field1 + field2 + field3 FROM table1
          SELECT @var

          Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

          "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

          J 1 Reply Last reply
          0
          • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

            DECLARE @var NVARCHAR(MAX)
            SET @var = ''
            SELECT @var = @var + '[LINE-BREAK]' + field1 + field2 + field3 FROM table1
            SELECT @var

            Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

            J Offline
            J Offline
            Jassim Rahma
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • J Jassim Rahma

              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

              S Offline
              S Offline
              Sascha Lefevre
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups