Results 1 to 7 of 7
  1. #1
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44

    T-SQL Cursor within a cursor

    Hello All -


    I have a T-SQL cursor that works great in SQL Server. Basically it will open a main cursor (cursor1) from tableA and then it opens a second cursor (cursor2) based on the same table (tableA) from the first cursor.

    Basic psedo code:


    Code:
    open cusror1
     select name, id from tableA
    where id >500
     order by id desc
        fetch into @name, @id
    
           open cursor2
              select name from table A
                 where id < @id
                 order by id desc
    
    perform some stuff
     
            close cursor 2
    
    fetch next cursor1
    etc.

    I need to convert to Access. Is this possible? Do I need to use recordsets? I am using Access 2007.

    Thank you in advance!!

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Perhaps you could tell us what it is you are trying to do. And then someone could advise on what options may be appropriate.

  3. #3
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    Basically I have one table with lets say 1000 rows. Each row has 4 important pieces of data......customerType, Customer_rating, Customer_distance, FLAG.

    The first 200 rows (ordered by customer_rating) have the flag set to 'X'.

    The first cursor, selects all the customers where the flag is null ordered by customer_rating desc. (So first time though row 201 would be selected for example).

    Then with that customer, I perform another select on the same table where the customer_rating is greater than the rating for customer 201. (So this would return the first 200 rows).

    Within the second cursor I perform some validation and might update the flag for 201 and then fetch the next record which would be 202 and perform the same inner select.

    __________________________________
    Right now I am thinking I could create two subs and have the first sub create my main cursor/recordset and then send the customer info (201) to a second sub where I can create another recordset??

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Great, but what I asking is along these lines, in order to let people know what you are trying to do.

    I have different types of Customers. Some of them do x, and they get assigned a rating.
    I want to process theCustomers with ratiings > abc ...

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I think the answer to your question is yes, the Access equivalent would be 2 recordset loops.

    I think where orange is heading is that there is often a more efficient way of accomplishing the same goal. Most SQL Server people I know basically frown on cursors, believing that there is usually a set-based way of accomplishing the task more efficiently. If you describe the process as orange is suggesting, someone may see a better way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    Ok, thanks. Let me see if I can explain the process.

    I have 1000 potential customers with a field where they have already been ranked from best potential to worst potential.....200 of them I already know are good. (Flag is set).

    I want to read through table, grab the first one where the flag is not set, order by ranking (best first) (SAY THIS IS CUST201). Then take that customer(CUST201) and compare it to the 200 ones where they are already set as good. (This would be inner cursor).

    Then i run a lot of field comparisons (not important for this) on:
    CUST201 vs CUST1
    CUST201 vs CUST2
    CUST201 vs CUST3
    CUST201 vs CUST4
    etc....

    If for example CUST201 vs CUST2 meets my criteria, then I will set CUST201 flag and exit inner cursor and then select next customer (CUST202). If I go through and compare all of them and none of them fit my criteria the FLAG does not get set.

    Then compare CUST202 with 1-201 where FLAG is set.


    Make any sense?

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I agree with Paul --- 2 recordset loops.
    The purpose of my previous post was to get you to describe your "business need" in plain English. Just to tell people, who are there to advise and help, what your problem/opportunity is; and to remove/reduce "progammer talk".


    Here's a link that may be useful. http://allenbrowne.com/ser-29.html
    Good luck with your project.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Changing the cursor
    By BernardKane in forum Forms
    Replies: 4
    Last Post: 10-24-2022, 09:59 AM
  2. Bold Field by Cursor Position
    By Rawb in forum Forms
    Replies: 4
    Last Post: 02-08-2011, 08:03 AM
  3. Replies: 0
    Last Post: 01-04-2011, 12:30 AM
  4. Cursor issue when no records
    By shenry16 in forum Forms
    Replies: 4
    Last Post: 01-20-2010, 01:22 PM
  5. Replies: 0
    Last Post: 03-26-2007, 12:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums