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. Row Concatenation

Row Concatenation

Scheduled Pinned Locked Moved Database
announcementlearning
4 Posts 2 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.
  • R Offline
    R Offline
    Ruchi Gupta
    wrote on last edited by
    #1

    I have followign two tables, and want to update TB1 such that IDs col gets updated PARAMETER tables TB1 Dt desk IDs ----------- ---------- ---------- 1 PT 1 AB 2 PT 2 AB TB2 Dt desk book IDs ----------- ---------- ---------- ----------- 1 PT PT1 100 1 PT PT2 200 1 PT PT3 300 1 AB AB1 400 2 PT PT1 500 2 PT PT2 600 2 PT PT3 700 2 AB AB1 800 Final RESULT TB1 Dt desk IDs ----------- ---------- ---------- 1 PT 100, 200, 300 1 AB 400 2 PT 500, 600, 700 2 AB 800 Any pointers Thanks Ruchi

    B 1 Reply Last reply
    0
    • R Ruchi Gupta

      I have followign two tables, and want to update TB1 such that IDs col gets updated PARAMETER tables TB1 Dt desk IDs ----------- ---------- ---------- 1 PT 1 AB 2 PT 2 AB TB2 Dt desk book IDs ----------- ---------- ---------- ----------- 1 PT PT1 100 1 PT PT2 200 1 PT PT3 300 1 AB AB1 400 2 PT PT1 500 2 PT PT2 600 2 PT PT3 700 2 AB AB1 800 Final RESULT TB1 Dt desk IDs ----------- ---------- ---------- 1 PT 100, 200, 300 1 AB 400 2 PT 500, 600, 700 2 AB 800 Any pointers Thanks Ruchi

      B Offline
      B Offline
      Bill Dean
      wrote on last edited by
      #2

      Is this on sql server? If so, your best bet might be to write a cursor to build each of your id strings ('100,200,300') and then update. I've been trying to come up with a clever way to do it in one SQL statement, doing self joins of tb2 to itself...but I am not getting anywhere... Typically, when I need to denormalize data like this, I do the denormalization in the application (typically c#...but that's details). SQL just does not lend itself to this kind of thing nearly as readily as ADO.NET / more procedural programming approaches do. Hope this helps Bill

      R 1 Reply Last reply
      0
      • B Bill Dean

        Is this on sql server? If so, your best bet might be to write a cursor to build each of your id strings ('100,200,300') and then update. I've been trying to come up with a clever way to do it in one SQL statement, doing self joins of tb2 to itself...but I am not getting anywhere... Typically, when I need to denormalize data like this, I do the denormalization in the application (typically c#...but that's details). SQL just does not lend itself to this kind of thing nearly as readily as ADO.NET / more procedural programming approaches do. Hope this helps Bill

        R Offline
        R Offline
        Ruchi Gupta
        wrote on last edited by
        #3

        Thanks very much for your response. I myself am doing the same thing you suggested - Cursor & Updates. But it is kind of expensive operation, when temp table is huge (depending on param passed to SP) I need this result set for C# application. Could you please give me pointers on how you do it in ADO.NET Thanks Ruchi

        B 1 Reply Last reply
        0
        • R Ruchi Gupta

          Thanks very much for your response. I myself am doing the same thing you suggested - Cursor & Updates. But it is kind of expensive operation, when temp table is huge (depending on param passed to SP) I need this result set for C# application. Could you please give me pointers on how you do it in ADO.NET Thanks Ruchi

          B Offline
          B Offline
          Bill Dean
          wrote on last edited by
          #4

          Ruchi Gupta wrote: But it is kind of expensive operation, when temp table is huge Agreed. Which is why I was trying to be clever...the update...from is often handy for complex updates, but I can't come up with a single query that will generate your desired result strings ('100,200,300','400', etc). Besides, that approach required that you know the max number of strings (in your example, 3) before hand. You mention passing parameters for the first time above. Are you updating only a few records in TB1? That might make this easier. I was assuming you were updating the whole table. Anyway in ADO, the idea is to get your data from TB2 in order, loop through the results building the new IDs string for each entry in TB1 and then do the update. C# doesn't suffer from the horrible performance penalties of SQL for looping. Something like: -Load TB1 into a DataTable using a dataadapter (if you set the PK, you can use a SqlCommandBuilder to automatically generate your update command) -Load TB2, sorted by DT,Desk into a second datatable -create a string variable to accumulate the id string; -Cycle through every row of this second table. -if the current row has the same DT and Desk as the previous row, just add the ID to the id string with the comma -other wise, set the IDs column of the Row in the first DataTable with the matching DT and Desk to the accumulated ID string. Use DataTable.Find() to find the row. Clear the ID string. -At the end, use the original dataadapter to update the database. This way, you can do all the itteration in C#, which does it well, rather than in SQL which is terrible at it. Hope this helps Bill

          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