Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Updating multiple rows in SQL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Updating multiple rows in SQL

    I have a list control that lists order items. I want to update these rows directly in the SQL database. They all have a common field in them called 'LinkID'. The field entry will be identical for all of the ones that I want to update. I can do an INSERT with no problem to an SQL database. The only thing I'm stuck on is how to loop through the records to update the 'isSold' field to true or 1. I know the value to LinkID so don't need to hunt for that.

    Can someone point me in the right direction?

    #2
    Re: Updating multiple rows in SQL

    I think I said this wrong. Let's try again.

    I have a table with products. once they're sold, I want to update isSold to true. The products table is PRODUCT_INFORMATION.
    The OrderItems table is the orders where the records are essentially these products with a field ORDER_ID which contains the order ID number.
    So, the Order_ID is going to be the same for the records in the OrderItems table.
    So, I need to call all the records in the Order_ID table with the same Order_ID field (say 21) and then change the isSold to true (or 1).

    Comment


      #3
      Re: Updating multiple rows in SQL

      Hi David,

      I did the best I could based on the information provided.
      I think the code is generic enough that it should work withMySQL or msSQL.
      Somewhere along the line it sounds like this is a great time to use a trigger to update the product_information table.

      Gregg

      Code:
      update product_information p
      left join OrderItems  on p.order_id = o.order_id
      set p.isSold = 1
      where (p.isSold <> 1 or p.isSold is null) and p.order_id = o.order_id
      Gregg
      https://paiza.io is a great site to test and share sql code

      Comment


        #4
        Re: Updating multiple rows in SQL

        That's almost there, but not quite.

        The only problem is, the order_id doesn't exist in the Product_Information table.

        Think of Product_Information as the inventory table.
        OrderItems is the table where the things they're buying are stored.
        OnlineOrders is the order record.

        After the order, the OrderItems table records are update with the OrderID from OrderItems. There's already another linking field called CartLinkID which was used all along to keep track of what is in the customer's cart. It's also a session id so it can easily be writted to the OnlineOrders table.

        Now I need to update the inventory to sold. The Product_Information ID numbers are stored in the OrderItems table under PARTNO field. So, somehow I need to call the OrderItems table, find the corresponding items that belong to OnlineOrders record, then cross reference to Product_Information and mark them sold.


        I sometimes talk too much when I explain, so let me know if this makes sense.

        Comment


          #5
          Re: Updating multiple rows in SQL

          The part about order_id not being in product_information makes sense.
          If I have this right, the orderItems table should have an orderID and productID column, and then OnlineOrders should have an onlineOrderID and orderID column.
          Assuming a value in the OnlineOrders table has to change to trigger this (like a purchaseTime column), the code below "should" work.

          I have a much better time if I have sample tables/columns to work with. I mentioned the purchaseTime column because this screams to be done via trigger.
          I'm attaching an image of what I'm guessing is the basic table setup. Feel free to let me know how I did.
          2020-03-19_1-38-13.png
          Code:
          update product_information p
          left join OrderItems i on i.fkProductID = p.ID
          left join OnlineOrder o on o.fkOrderItemsID = i.ID
          set p.isSold = 1
          where (p.isSold <> 1 or p.isSold is null) and p.id = i.fkProductID and o.fkOrderItemsID = i.ID
          Gregg
          https://paiza.io is a great site to test and share sql code

          Comment


            #6
            Re: Updating multiple rows in SQL

            Attached is how it works.

            The OrderItems holds the Product_Information_ID AND the OnlineOrders ID. That's so there can be more than 1 OrderItem. Imagine a shopping cart.

            I'm gonna play with what you have and see if I can modify to work. Thanks!!

            GRAPHICS-0140.png

            Comment


              #7
              Re: Updating multiple rows in SQL

              That pic helped a lot.
              This should do it.
              Code:
              update product_information p
              left join OrderItems i on i.fkProductID = p.ID
              left join OnlineOrder o on o.ID = i.onlineordersID
              set p.isSold = 1
              where (p.isSold <> 1 or p.isSold is null) and p.id = i.fkProductID and o.ID = i.onlineordersID
              Gregg
              https://paiza.io is a great site to test and share sql code

              Comment


                #8
                Re: Updating multiple rows in SQL

                What am I missing here. it's not working.

                Code:
                dim orderid as c
                orderid = 34
                args.add("ORDERINFOID",orderid)
                Code:
                UPDATE Product_Information p
                LEFT JOIN OrderItems i on i.PARTNO = p.PRODUCT_INFORMATION_ID
                LEFT JOIN OnlineOrder o on o.Order_ID = i.ORDERINFO_ID
                SET p.isSold = 1
                where (p.isSold <> 1 or p.isSold is null) and p.PRODUCT_INFORMATION_ID = i.PARTNO and o.Order_id = :ORDERINFOID

                Comment


                  #9
                  Re: Updating multiple rows in SQL

                  Please show the code, and hopefully the error text.
                  If you want to talk this through, my phone is 6086283558
                  Gregg
                  https://paiza.io is a great site to test and share sql code

                  Comment


                    #10
                    Re: Updating multiple rows in SQL

                    Is this what you're looking for? Should work, huh?

                    Code:
                    dim cn as sql::connection
                    dim flag as l 
                    flag = cn.open("::Name::SQL")
                    if flag = .f. then 
                    	'there was an error
                    	dim errorText as c 
                    	errorText = cn.callresult.text
                    	end
                    end if 
                    'turn on portable SQL
                    cn.PortableSQLEnabled = .t.
                    dim SQL as c 
                    sql = <<%sql%
                    UPDATE Product_Information p
                    LEFT JOIN OrderItems i on i.PARTNO = p.PRODUCT_INFORMATION_ID
                    LEFT JOIN OnlineOrder o on o.Order_ID = i.ORDERINFO_ID
                    SET p.isSold = 1
                    where (p.isSold <> 1 or p.isSold is null) and p.PRODUCT_INFORMATION_ID = i.PARTNO and o.Order_id = :ORDERINFOID
                    %sql%
                    dim args as sql::arguments
                    
                    dim orderid as c
                    orderid=34
                    args.add("ORDERINFOID",orderid)
                    
                    'execute the SQL
                    flag = cn.execute(sql,args)
                    if flag = .f. then 
                    	'there was an error
                    	dim errorText as c 
                    	errorText = cn.callresult.text
                    	cn.close()
                    	end
                    end if 
                    dim rowsUpdated as n 
                    'get the number of rows that were updated
                    rowsUpdated = cn.CallResult.rowsaffected
                    cn.close()

                    Comment


                      #11
                      Re: Updating multiple rows in SQL

                      Hurray for stupid things(Alpha's portable SQL determines valid sql code is invalid) :)
                      It's not your fault, but change cn.PortableSQLEnabled = .t. to cn.PortableSQLEnabled = .f.
                      Last edited by madtowng; 03-20-2020, 06:11 AM.
                      Gregg
                      https://paiza.io is a great site to test and share sql code

                      Comment


                        #12
                        Re: Updating multiple rows in SQL

                        I've tried this format every which way I can think of and can't get it to work. What I assume it's doing is a backwards path through the databases to get the product ID and update it to sold.

                        This doesn't work either. And pick an SQL format and I've tried it. Argh...

                        Code:
                        dim cn as sql::connection
                        dim flag as l 
                        flag = cn.open("::Name::SQL")
                        if flag = .f. then 
                        	'there was an error
                        	dim errorText as c 
                        	errorText = cn.callresult.text
                        	end
                        end if 
                        
                        'turn on portable SQL
                        cn.PortableSQLEnabled = .f.
                        dim SQL as c 
                        sql = <<%sql%
                        UPDATE
                          Product_Information
                          LEFT JOIN OrderItems ON OrderItems.PARTNO = Product_Information.PRODUCT_INFORMATION_ID
                          LEFT JOIN OnlineOrders ON OnlineOrders.Order_ID = OrderItems.ORDERINFO_ID
                        SET
                          Product_Information.isSold = 1
                        WHERE
                          Product_Information.PRODUCT_INFORMATION_ID = OrderItems.PARTNO
                          AND OnlineOrders.Order_ID = 37
                        %sql%
                        dim args as sql::arguments
                        
                        dim orderid as n
                        orderid = 37
                        args.add("ORDERINFOID",orderid)
                        'execute the SQL
                        flag = cn.execute(sql,args)
                        if flag = .f. then 
                        	'there was an error
                        	dim errorText as c 
                        	errorText = cn.callresult.text
                        	cn.close()
                        	end
                        end if 
                        dim rowsUpdated as n 
                        'get the number of rows that were updated
                        rowsUpdated = cn.CallResult.rowsaffected
                        cn.close()

                        Comment


                          #13
                          Re: Updating multiple rows in SQL

                          What is the error?
                          Gregg
                          https://paiza.io is a great site to test and share sql code

                          Comment


                            #14
                            Re: Updating multiple rows in SQL

                            I've not studied your query much, but wanted to suggest a different approach.

                            Instead of those joins for the table being updated, you can get a list of keys that need updating like this:

                            Code:
                            UPDATE MyTable
                            SET SomeValue = 1
                            WHERE ID in(
                            	(SELECT something here that returns the list of MyTable.IDs that need updating)
                            	)
                            And, yes, do not turn on portableSQL for this stuff
                            -Steve
                            sigpic

                            Comment


                              #15
                              Re: Updating multiple rows in SQL

                              Gregg, there's no error. Just doesn't update. If I take away all the JOINs and update the isSold by itself, it works. But obviously we can't do that because we don't know the product numbers.

                              Comment

                              Working...
                              X