none
Looping through cursors in foxpro

    Question

  • Hi
    I am creating cursor in a function

    SELECT <<column_name1>>, <<coulmn_name2>> FROM <<table_name>> INTO <<cursor_name>>

    Could anyone please tell me how do i loop through the cursor and access the values of coulms.
    thanks
    Thursday, January 17, 2008 8:32 PM

Answers

  • It looks like this:

     

    select * from customer where country = "USA" into cursor crsUSACustomers

     

    scan

      if City = "Seattle" && City is column name - equivalant to: if crsUSACustomers.City = "Seattle"

      * crsUSACustomers.City explicitly tells you meant crsUSACustomers cursor's City column

      * Due to precedence just using City would do and means "the City column in whatever the current cursor is"

    *...

      endif

    endscan

    Friday, January 18, 2008 12:12 AM
  • first you need to have the database open exclusive

    then you need & (macro substitution), but that does not work with &tablename.fieldname &fieldname or &(fieldname), you need to read it into a var, also better do an alltrim on the field value:

     

    Code Block

    lcTrigger = ALLTRIM(qTriggerText.insert_trigger)

     

    CREATE TRIGGER ON (strTabNm) FOR INSERT AS &lcTrigger

     

     

     

    Friday, January 18, 2008 8:16 AM

All replies

  • SELECT <<column_name1>>, <<coulmn_name2>> FROM <<table_name>> INTO <<cursor_name>>

    *** current alias is cursor_name
    SCAN
          ... code here
    ENDSCAN
    Thursday, January 17, 2008 8:45 PM
  • Hey Vladimir Zografski

    thanks for your reply.

    But how do i get the values of the column? Could you please tell me the syntax.

    Please bear with me as i am totally new to fox pro...
    Thursday, January 17, 2008 10:20 PM
  • It looks like this:

     

    select * from customer where country = "USA" into cursor crsUSACustomers

     

    scan

      if City = "Seattle" && City is column name - equivalant to: if crsUSACustomers.City = "Seattle"

      * crsUSACustomers.City explicitly tells you meant crsUSACustomers cursor's City column

      * Due to precedence just using City would do and means "the City column in whatever the current cursor is"

    *...

      endif

    endscan

    Friday, January 18, 2008 12:12 AM
  • Hi CetinBasoz
    Your code was really helpful. But i have one more problem.
    Below is the code where i am looping through the cursor and making use of values in the cursor.

    SELECT insert_trigger FROM trigger_info  WHERE table_name = <<table_name>>INTO CURSOR qTriggerText READWRITE
                       
                        =MESSAGEBOX(qTriggerText.insert_trigger)
                        =MESSAGEBOX(qTriggerText.delete_trigger)

                        SCAN
                            *If insert trigger is present   
                            IF NOT EMPTY(qTriggerText.insert_trigger) THEN
                                CREATE TRIGGER ON (strTabNm) FOR INSERT AS (qTriggerText.insert_trigger)
                            ENDIF

    ENDSCAN

    Basically i have a trigger expression in my cursor with which i want to create trigger.

    If i print it with messagebox the value is coming up properly
    but when i use same logic in CREATE TRIGGER command , the value is not coming.
    my trigger is created with expression like "(qTriggerText.insert_trigger)".
    I also tried with
    qTriggerText.insert_trigger
    &qTriggerText.insert_trigger
    options but couldnt succeed.

    Whatever i give in the Create trigger statement that is coming as trigger expression.
    but i need the value of it.

    Please help me
    Friday, January 18, 2008 12:43 AM
  • first you need to have the database open exclusive

    then you need & (macro substitution), but that does not work with &tablename.fieldname &fieldname or &(fieldname), you need to read it into a var, also better do an alltrim on the field value:

     

    Code Block

    lcTrigger = ALLTRIM(qTriggerText.insert_trigger)

     

    CREATE TRIGGER ON (strTabNm) FOR INSERT AS &lcTrigger

     

     

     

    Friday, January 18, 2008 8:16 AM
  •  hamz2008 wrote:
    Hi
    Could anyone please tell me how do i loop through the cursor and access the values of coulms.

     

    This question leads me to believe that you are new to Visual FoxPro.

     

    Do yourself a fqavor and go to http://www.hentzenwerke.com/catalog/fund6.htm and get a copy of Fundamentals: Building Visual Studio Applications on a Visual FoxPro 6.0 Foundation. Although it is a few versions old, the concepts have not changed and it is the best beginners' reference I have ever seen.

    Friday, January 18, 2008 11:47 AM
  • Hey Olaf Doschke

    thanks a lot . it worked.
    i appreciate and thank you all of you who have given their replies to my post.
    Friday, January 18, 2008 11:37 PM