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. Help with cursor

Help with cursor

Scheduled Pinned Locked Moved Database
helpsalesquestion
6 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.
  • A Offline
    A Offline
    allende
    wrote on last edited by
    #1

    Hello Friends, I have never written a cursor before and I have a situation in which I need it. I have two days trying and still couldn't make it work...I need some help I have three tables one for Items, one for Orders and one for OrderDetail. I want to check every X days that all the unpaid orders(which has items blocked on it, defined in OrderDetail) free their items and increase the amount on the Items table. Evey time a customer make an order, the items are reserved, so they need to be freed if the order is canceled or remains unpaid for a given time. In this case I need a cursor that iterates through all the items belonging to the same unpaid order, then increase the amount in the Items table and delete the record in the OrderDetail table. Please, could you help me in this problem?? I will appreciate any help Thanks.

    P 1 Reply Last reply
    0
    • A allende

      Hello Friends, I have never written a cursor before and I have a situation in which I need it. I have two days trying and still couldn't make it work...I need some help I have three tables one for Items, one for Orders and one for OrderDetail. I want to check every X days that all the unpaid orders(which has items blocked on it, defined in OrderDetail) free their items and increase the amount on the Items table. Evey time a customer make an order, the items are reserved, so they need to be freed if the order is canceled or remains unpaid for a given time. In this case I need a cursor that iterates through all the items belonging to the same unpaid order, then increase the amount in the Items table and delete the record in the OrderDetail table. Please, could you help me in this problem?? I will appreciate any help Thanks.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Most of the time, when I see somebody say they need a cursor I can't help thinking that they've missed out on the alternatives. This seems to be the case here. You probably don't need to use a cursor. What you could do is to create a temporary (or in memory) table that contains details of all of the unfilled orders. Then, once you have done this, you would write your update statements for the other tables based on this table. What you will find is that this approach is more efficient because it allows the database engine to operate in a set based manner which is what DB engines are designed to do. As an example (in pseudocode)

      SELECT the missing records
      FROM orderDetails
      INTO a Temporary Table.
      
      UPDATE order WITH THE itemTotals FROM Temporary Table
      WHERE THE Item Id IN Temporary Table
      MATCHES THE Id in my Item TABLE.
      
      Do the other tables here...
      

      Anyway, that's what I would do.

      Deja View - the feeling that you've seen this post before.

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        Most of the time, when I see somebody say they need a cursor I can't help thinking that they've missed out on the alternatives. This seems to be the case here. You probably don't need to use a cursor. What you could do is to create a temporary (or in memory) table that contains details of all of the unfilled orders. Then, once you have done this, you would write your update statements for the other tables based on this table. What you will find is that this approach is more efficient because it allows the database engine to operate in a set based manner which is what DB engines are designed to do. As an example (in pseudocode)

        SELECT the missing records
        FROM orderDetails
        INTO a Temporary Table.
        
        UPDATE order WITH THE itemTotals FROM Temporary Table
        WHERE THE Item Id IN Temporary Table
        MATCHES THE Id in my Item TABLE.
        
        Do the other tables here...
        

        Anyway, that's what I would do.

        Deja View - the feeling that you've seen this post before.

        A Offline
        A Offline
        allende
        wrote on last edited by
        #3

        Thanks Pete, I will be more specific, I tried your suggestion but still doesn't work...I'm not an SQL expert :o) I will rite here the involved tables and their main fields: Orders(OrderId) OrderDetail(OrderId, ProductSubCode, Quantity) Products(ProductSubCode, Quantity) When an order is canceled I want to get all the items on it and re-insert them in the Product table (summarizing with the amount already in that table) If I do a select of all the items in a given order as you told me I will get something like this: "SELECT * INTO #TMP FROM ORDERDETAIL WHERE ORDERID=30" OrderId ProductSubCode Quantity 30 BD0002 10 30 BD0004 4 Now I need to re-insert those quantities into the Products table. I can't figure out how I ca do that without a cursor since I need to iterate through the records in the temporary table. Of course I can still be missing alternatives Thanks a lot.

        P 1 Reply Last reply
        0
        • A allende

          Thanks Pete, I will be more specific, I tried your suggestion but still doesn't work...I'm not an SQL expert :o) I will rite here the involved tables and their main fields: Orders(OrderId) OrderDetail(OrderId, ProductSubCode, Quantity) Products(ProductSubCode, Quantity) When an order is canceled I want to get all the items on it and re-insert them in the Product table (summarizing with the amount already in that table) If I do a select of all the items in a given order as you told me I will get something like this: "SELECT * INTO #TMP FROM ORDERDETAIL WHERE ORDERID=30" OrderId ProductSubCode Quantity 30 BD0002 10 30 BD0004 4 Now I need to re-insert those quantities into the Products table. I can't figure out how I ca do that without a cursor since I need to iterate through the records in the temporary table. Of course I can still be missing alternatives Thanks a lot.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          OK - here's a sample way to update the product table:

          UPDATE Products 
          SET p.Quantity = p.Quantity + t.Quantity
          FROM Products p, #Tmp t
          WHERE p.ProductSubCode = t.ProductSubCode
          

          This doesn't involve any cursors and is completely set based.

          Deja View - the feeling that you've seen this post before.

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            OK - here's a sample way to update the product table:

            UPDATE Products 
            SET p.Quantity = p.Quantity + t.Quantity
            FROM Products p, #Tmp t
            WHERE p.ProductSubCode = t.ProductSubCode
            

            This doesn't involve any cursors and is completely set based.

            Deja View - the feeling that you've seen this post before.

            A Offline
            A Offline
            allende
            wrote on last edited by
            #5

            Thanks a lot Pete!!!! That worked. As you stated there was not necessary a cursor here!!

            P 1 Reply Last reply
            0
            • A allende

              Thanks a lot Pete!!!! That worked. As you stated there was not necessary a cursor here!!

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              No problems. I'm glad that I could help.

              Deja View - the feeling that you've seen this post before.

              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