Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Nesting error in trigger
Message
From
10/06/2005 02:14:49
 
 
To
10/06/2005 01:22:21
Eugene Kolmakov
Millennium Technologies
Vladivostok, Russia
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01022038
Message ID:
01022043
Views:
14
>Hi!
>
>I have a problem. When I change values of a record in the table, I need to update some other child records in the same table. And I want to make it work using trigger. But nesting error is raised on trigger execution. I use the following code:
>
>FUNCTION Update_Child_Fields
>
>IF myTable.Type = 1 && Type = 1 - Parent, Type = 2 - Child
>
> lcParentID = ALLTRIM(myTable.Key)
> lcOwnerID = ALLTRIM(myTable.OwnerID)
> lcAliasName = SYS(2015)
>
> USE myTable AGAIN IN 0 ALIAS (lcAliasName)
>
> SELECT (lcAliasName)
> Replace Parent_OwnerID WITH lcOwnerID;
> FOR ALLTRIM(Parent) == lcParentID
> IN (lcAliasName)
>
> USE IN (lcAliasName)
>
>ENDIF
>
>ENDFUNC
>
>I call this function in myTable's Update trigger. I have no idea why it doesn't work and how to make it work correctly. Need your help.

Eugene,

If this is the trigger code for table myTable, then myTable may be opened with a different alias (see use again alias ...)
Hence, using myTable.Type is mostly wrong. Either myTable alias is not there, and if it is you may be referencing the wrong alias.

There is no need to reference myTable as an alias since that alias is selected when the trigger fires
There are a couple of other issues

(1) if you select another alias, you have to re-select the original work area prior to exiting the function

(2) avoid using alltrim(), it will not optimize
FUNCTION Update_Child_Fields
	
IF Type = 1    && Type = 1 - Parent, Type = 2 - Child

   local lcParentID, lcOwnerID, lcAliasName

   lcParentID = ALLTRIM(Key)
   lcOwnerID = ALLTRIM(OwnerID)
   lcAliasName = SYS(2015)
		
   USE myTable AGAIN IN 0 ALIAS (lcAliasName)

   Replace Parent_OwnerID WITH m.lcOwnerID;
      all FOR Parent == m.lcParentID ;
      IN (m.lcAliasName)

   USE IN (m.lcAliasName)

ENDIF
	
ENDFUNC
(3) You do not test to see whether the OwnerId has changed, you update the child table even if it has not

(4) For the next step you may want to consider alias open and close routines: see standard trigger code for an example

And lastly - nothing to do with the code - Why do you have Parent_OwnerID in the child table ? Does it belong there ? Your code tells me it does not since you keep it in sync with the parent. I'd say that Parent_OwnerID does not belong in the child table. Hence, you do not need the trigger code ;-)
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform