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. How to combine results in cursor?

How to combine results in cursor?

Scheduled Pinned Locked Moved Database
databasetutorialquestion
3 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.
  • S Offline
    S Offline
    SledgeHammer01
    wrote on last edited by
    #1

    I have a query that returns a table something like this: 102 NULL 101 NULL the 101 & 102 are FKs into another table, and I need to "expand" those... so what I want to end up with is: 102 b 102 a 101 b 101 a so I figured I'd create a cursor on the first table and loop through the 102, 101, etc. and then do an inner select to "expand" the 101 and 102... this works fine... I end up with 4 result sets though. One for the 102, one for the first null, one of the 101 and one for the second null. The empty result sets are significant to the query... I figured I could insert a dummy record in there with a special ID... unless there is a better way, but not sure how to combine the 4 result sets into a single one with out losing the empty ones?

    M G 2 Replies Last reply
    0
    • S SledgeHammer01

      I have a query that returns a table something like this: 102 NULL 101 NULL the 101 & 102 are FKs into another table, and I need to "expand" those... so what I want to end up with is: 102 b 102 a 101 b 101 a so I figured I'd create a cursor on the first table and loop through the 102, 101, etc. and then do an inner select to "expand" the 101 and 102... this works fine... I end up with 4 result sets though. One for the 102, one for the first null, one of the 101 and one for the second null. The empty result sets are significant to the query... I figured I could insert a dummy record in there with a special ID... unless there is a better way, but not sure how to combine the 4 result sets into a single one with out losing the empty ones?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I would as a default try and avoid cursors, they really are a last resort tool. Have you tried UNION or UNION ALL on the 2 queries.

      Select This, null That
      from SomeTable

      Union
      Select This, That
      from SomeTable
      left join ThatTable on ...

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • S SledgeHammer01

        I have a query that returns a table something like this: 102 NULL 101 NULL the 101 & 102 are FKs into another table, and I need to "expand" those... so what I want to end up with is: 102 b 102 a 101 b 101 a so I figured I'd create a cursor on the first table and loop through the 102, 101, etc. and then do an inner select to "expand" the 101 and 102... this works fine... I end up with 4 result sets though. One for the 102, one for the first null, one of the 101 and one for the second null. The empty result sets are significant to the query... I figured I could insert a dummy record in there with a special ID... unless there is a better way, but not sure how to combine the 4 result sets into a single one with out losing the empty ones?

        G Offline
        G Offline
        Ganu Sharma
        wrote on last edited by
        #3

        Dear you try....... you can reduce your null selection by is null clause. you can use union or union all are two method combine multiple results set. select 101 union select null union select 102 union select null union all result will be as... null, null 101 102 101 102 create table temp table insert into @temptable(ID)

        select 101
        union
        select null
        union
        select 102
        union
        select null

        select * from @temptable where id is not null ......................... try this......

        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