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

SQL: how to determine valid return value

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

    SQL: how to determine valid return value

    How do you determine if a SELECT statement with a WHERE clause returned any records? For instance:

    "select name from company where id = 1000"

    Will either return one record, or no records.

    Need to know before attempting to use values in the record set. If the recordset is empty it throws an error rather than returning a nice "null" or 0.

    ------------EXAMPLE--------------
    'example if the table does not contain a record with ID = 1000
    mySelect = "select name from company where id = 1000"
    conn.execute(mySelect)
    rs=conn.ResultSet
    ?rs.data(1)
    ERROR: Result set row requested not found while getting result set data column.'

    ------------------

    Also, I've lobbied Martin to create an SQL forum on our board, what do you think?
    Steve Wood
    See my profile on IADN


    #2
    Re: SQL: how to determine valid return value

    mySelect = "select name from company where id = 1000"
    sql_execute = conn.execute(mySelect)
    rs=conn.ResultSet

    if the query returns anything sql_execute variable will return = t
    Cheers
    Mauricio

    Comment


      #3
      Re: SQL: how to determine valid return value

      That's what you would think glancing at the Alpha Help examples, but it is wrong. It returns 1 as long as the statement is valid, nothing to do with the number of records returned. This is why I test.
      Steve Wood
      See my profile on IADN

      Comment


        #4
        Re: SQL: how to determine valid return value

        This works in Q&D testing with my database.

        'example if the table does not contain a record with ID = 1000
        mySelect = "select name from company where id = 1000"
        conn.execute(mySelect)
        rs=conn.ResultSet
        if rs.tostring() = "" then
        'error handler
        end if

        ?rs.data(1)

        Comment


          #5
          Re: SQL: how to determine valid return value

          A variation of that will work. You didn't mention rs.tostring, you said that conn.execute would return true if a record was found, which is incorrect.

          I found a solution that looks similar using the count function:

          mySelect = "select count(*),id FROM company where id = xxx"
          ?rs.data(1)
          = 0 'if not found
          Steve Wood
          See my profile on IADN

          Comment


            #6
            Re: SQL: how to determine valid return value

            Sorry, BTW Actually Jeff and I are different persons ;)

            you can also use:

            mySelect = "select name from company where id = 1000"
            data = conn.Tostring(mySelect)
            w_count(data,crlf())

            it will return the number of resulting rows, plus you have the actual data available in the "data" variable
            Last edited by mmaisterrena; 06-28-2010, 01:22 PM.
            Cheers
            Mauricio

            Comment


              #7
              Re: SQL: how to determine valid return value

              Sorry, I've been awake since for 28 hours working on the same project conversion from DBF to MySQL. Its really mind boggling converting SET-based xbasic routines to SQL.
              Steve Wood
              See my profile on IADN

              Comment


                #8
                Re: SQL: how to determine valid return value

                On additional testing, putting count(*) in the list of fields to return always renders ONE record. The record does include the proper count though. So to use this method you have to run the query twice, once with only count(*) in the field list. Here is part of my select statement to illustrate:

                This will yield a list of field values
                Code:
                select mbi.id,mbi.name,mbio.id,mbio.name
                from menu_bundleitems as mbi
                
                inner join menu_bundleitemoptions as mbio
                on mbio.BUNDLEITEM_ID=mbi.ID
                where mbi.id = 100002 
                order by mbi.sequence, mbio.sequence
                This just lists one record, but with the proper count:
                Code:
                select count(*)
                from menu_bundleitems as mbi
                
                inner join menu_bundleitemoptions as mbio
                on mbio.BUNDLEITEM_ID=mbi.ID
                where mbi.id = 100002 
                order by mbi.sequence, mbio.sequence
                Your examples are easier than running the query twice, but I wonder which is faster for the computer, converting the results in to a string in order to count, or re-running the whole query twice.
                Steve Wood
                See my profile on IADN

                Comment


                  #9
                  Re: SQL: how to determine valid return value

                  Steve,

                  Hard to know which is faster, but SELECT statements run very fast, so I'm guessing that the real difference is not very much. The connection.toString() is an Alpha function that must be running the select to get the result set, and may even be doing the count as part of the function in order to prevent an error.

                  When I run the count, I usually just count one field, typically the record id, rather than using count(*).

                  There is also a technique using EXISTS with a subquery, but I've never tried it. It yields a true/false, so I think you still need to run two selects.


                  Pat
                  Pat Bremkamp
                  MindKicks Consulting

                  Comment


                    #10
                    Re: SQL: how to determine valid return value

                    If you want to get a result set into a character string, use ResultSet.toString. This is a real common method to build lists for dropdowns, checkbox, and radio controls. You can also use ResultSet.toPropertyArray to build an array. This will retain the data type for each element in the result set and will size the array to the number of records returned.

                    If you want to step through the resultSet or just see if the SELECT returned any records, just use ResultSet.NextRow(). Here is a simple example using the Access AlphaSports to see if a single record is returned.
                    Code:
                    dim Fname as c
                    dim cn as sql::connection
                    if cn.open("::name::connection1")
                    	if cn.Execute("SELECT Firstname FROM Customer WHERE customer_id = 26") 'should have one record
                    		if cn.ResultSet.nextRow() 'places cursor on first row
                    			fname = cn.ResultSet.data("Firstname")
                    		end if
                    	end if
                    	cn.close()
                    end if 
                    ui_msg_box("",fname)
                    When the result set is returned, the cursor is not initially on any row, so the first nextNow() just places the cursor on the first row, if one exists. If there are no records, the nextRow() will return false. If you wanted to return a list of first names, you could use ResultSet.toString() or something like this
                    Code:
                    dim Fname as c
                    dim cn as sql::connection
                    if cn.open("::name::connection1")
                    	if cn.Execute("SELECT Firstname FROM Customer WHERE customer_id > 26") 'multiple
                    		while cn.ResultSet.nextRow() 
                    			fname = fname + cn.ResultSet.data("Firstname")+crlf()
                    		end while
                    	end if
                    	cn.close()
                    end if 
                    ui_msg_box("",fname)

                    Comment


                      #11
                      Re: SQL: how to determine valid return value

                      On additional testing, putting count(*) in the list of fields to return always renders ONE record. The record does include the proper count though.
                      Here is a helpful reference on the SQL count function:

                      http://www.techonthenet.com/sql/count.php

                      However, if you are just trying to determine the number of rows in the result set, you can check the ResultSet.RowCount property.
                      Last edited by Bob Moore; 06-28-2010, 09:59 PM.
                      Bob Moore


                      Comment


                        #12
                        Re: SQL: how to determine valid return value

                        Good advice, needed. I've been dropping results (if more than one record) to a property array because that's the only way I know how to iterate through the results
                        Steve Wood
                        See my profile on IADN

                        Comment


                          #13
                          Re: SQL: how to determine valid return value

                          I use something like this

                          mySelect = "select name from company where id = 1000"
                          data = conn.Tostring(mySelect)
                          n = w_count(data,crlf())

                          for i=1 to n
                          Value = word(data,i,crlf())
                          some action here
                          next
                          Cheers
                          Mauricio

                          Comment


                            #14
                            Re: SQL: how to determine valid return value

                            Steve,
                            Here is how I iterate through the results returned by mysql

                            dim cn as sql::connection
                            cn.open("::name::connection1")

                            cn.Execute("SELECT Firstname FROM Customer WHERE customer_id > 26") 'multiple

                            rs=cn.resultset
                            if eval_valid("rs.currentrow") 'if valid, there is data to process; if not, sql statement did not return any rows
                            rows_to_process=rs.rowcount
                            loopflag=.t.
                            while loopflag
                            'do work here on the data
                            loopflag=rs.nextrow()
                            end while
                            end if
                            cn.close()

                            Comment


                              #15
                              Re: SQL: how to determine valid return value

                              Originally posted by Bob Moore View Post
                              Here is a helpful reference on the SQL count function:

                              http://www.techonthenet.com/sql/count.php

                              However, if you are just trying to determine the number of rows in the result set, you can check the ResultSet.RowCount property.
                              Some SQL sources such as Access and SQL Server do not return the row count so ResultSet.RowCount will always return -1
                              Code:
                              dim cn as sql::connection
                              ?cn.open("::name::connection1")
                              = .T.
                              
                              ?cn.execute("Select Firstname from customer")
                              = .T.
                              
                              ?cn.ResultSet.RowCount
                              = -1
                              Therefore, it is an unreliable way to get the count of records in a select.

                              Comment

                              Working...
                              X