=== modified file 'src/com/goldencode/p2j/schema/SchemaDictionary.java'
--- src/com/goldencode/p2j/schema/SchemaDictionary.java	2017-04-01 23:33:34 +0000
+++ src/com/goldencode/p2j/schema/SchemaDictionary.java	2017-07-18 20:03:56 +0000
@@ -2141,7 +2141,7 @@
     *          Name of the table whose node is to be found.
     * @param   toNode
     *          Copy target node.
-    * @param   skipGlobal
+    * @param   preferTemp
     *          if (@code true}, do not look in the global schema scope to find {@code fromTable}.
     *          
     * @return  Name node as described above.
@@ -2149,7 +2149,7 @@
     * @throws  AmbiguousSchemaNameException
     *          if the lookup results in ambiguity within a scope.
     */
-   public NameNode findTableFromNode(String fromTable, NameNode toNode, boolean skipGlobal)
+   public NameNode findTableFromNode(String fromTable, NameNode toNode, boolean preferTemp)
    throws AmbiguousSchemaNameException
    {
       int type = EntityName.TABLE;
@@ -2157,33 +2157,48 @@
       // Try to find the "from" node in any scope.
       String fromName = fromTable;
       
-      // look first for a persistent table in the schema global scope; a persistent table takes
-      // precedence over a temp-table with the same name, unless this behavior is explicitly
-      // overridden (e.g. DEFINE BUFFER x FOR TEMP-TABLE y)
-      NameNode fromNode = null;
-      if (!skipGlobal)
-      {
-         try
-         {
-            fromNode = findEntry(fromTable,
-                                 type,
-                                 SCHEMA_GLOBAL_SCOPE,
-                                 SCHEMA_GLOBAL_SCOPE,
-                                 false);
-         }
-         catch (AmbiguousSchemaNameException exc)
-         {
-            // if an unambiguous match for a persistent table could not be found, allow fromNode
-            // to remain null
-         }
-      }
-      
-      // if not found there, look for a temp-table in a higher scope
-      if (fromNode == null)
-      {
-         fromNode = findEntry(fromTable, type, scopes.size() - 1, USER_GLOBAL_SCOPE, false);
-      }
-      
+      NameNode fromNode;
+      NameNode nodeGlobal = null;
+      NameNode nodeUser = null;
+
+      // look for a persistent table in the schema global scope
+      try
+      {
+         nodeGlobal = findEntry(fromTable,
+                                type,
+                                SCHEMA_GLOBAL_SCOPE,
+                                SCHEMA_GLOBAL_SCOPE,
+                                false);
+      }
+      catch (AmbiguousSchemaNameException exc)
+      {
+         // if an unambiguous match for a persistent table could not be found, allow fromNode
+         // to remain null
+      }
+
+      // look for a temp-table in a higher scope, note that the lookup must always yield
+      // an exact match
+      try
+      {
+         nodeUser = findEntry(fromTable,
+                              type,
+                              scopes.size() - 1,
+                              USER_GLOBAL_SCOPE,
+                              true);
+      }
+      catch (AmbiguousSchemaNameException exc)
+      {
+         // if an unambiguous match for a temp table could not be found, allow fromNode
+         // to remain null
+      }
+
+      // a persistent table takes precedence over a temp-table with the same name,
+      // unless this behavior is explicitly overridden (e.g. DEFINE BUFFER x FOR TEMP-TABLE y);
+      // when a persistent table is not found always use the temp table regardless of
+      // the TEMP-TABLE keyword and vice versa, when a temp table is not found always use the
+      // persistent table regardless of the TEMP-TABLE keyword
+      fromNode = (preferTemp && nodeUser != null) || nodeGlobal == null ? nodeUser : nodeGlobal;
+
       // Make sure the node we found is not our "to" node. This can happen if the "to" node is
       // named the same as the "from" node, and was not qualified to differentiate it when it was
       // added. Also ensure that the "from" node is not a buffer;  we need the backing table.
@@ -2224,9 +2239,6 @@
     * @param   toNode
     *          Name node which represents the table to which the field entries
     *          will be copied.
-    * @param   forceTemp
-    *          if (@code true}, do not look in the global schema scope to find a "real" table to
-    *          replace the temp/work-table {@code fromNode} previously resolved.
     *
     * @throws  AmbiguousSchemaNameException
     *          if <code>table</code> matches more than one entry in the
@@ -2236,11 +2248,10 @@
     */
    public void addFieldEntries(String fromTable,
                                NameNode fromNode,
-                               NameNode toNode,
-                               boolean forceTemp)
+                               NameNode toNode)
    throws SchemaException
    {
-      addEntries(fromTable, fromNode, toNode, EntityName.TABLE, forceTemp);
+      addEntries(fromTable, fromNode, toNode, EntityName.TABLE);
       
       // force all children to have the same line/col numbers as the table
       // node, rather than the line/col nums from the copied (from) table
@@ -3772,9 +3783,6 @@
     * @param   type
     *          Type of the parent entity to which child entries are to be
     *          added (using {@link EntityName} constants).
-    * @param   forceTemp
-    *          if (@code true}, do not look in the global schema scope to find a "real" table to
-    *          replace the temp/work-table {@code fromNode} previously resolved.
     *
     * @throws  AmbiguousSchemaNameException
     *          if a database lookup produces more than one match.
@@ -3784,8 +3792,7 @@
    private void addEntries(String from,
                            NameNode fromNode,
                            NameNode toNode,
-                           int type,
-                           boolean forceTemp)
+                           int type)
    throws AmbiguousSchemaNameException,
           SchemaException
    {
@@ -3795,33 +3802,7 @@
          case BUFFER:
          {
             int fromType = fromNode.getType();
-            if (fromType == TEMP_TABLE || fromType == WORK_TABLE)
-            {
-               // forceTemp indicates we know the from table should be a temp/work table; only
-               // search in the schema global scope if set to false
-               if (!forceTemp)
-               {
-                  // If fromNode is a temp table and toNode is a buffer, make
-                  // sure fromNode is not masking a real table of the same name.
-                  try
-                  {
-                     NameNode realTable = findEntry(from,
-                                                    type,
-                                                    SCHEMA_GLOBAL_SCOPE,
-                                                    SCHEMA_GLOBAL_SCOPE,
-                                                    true);
-                     if (realTable != null)
-                     {
-                        fromNode = realTable;
-                     }
-                  }
-                  catch (AmbiguousSchemaNameException exc)
-                  {
-                     // This answers our question: there is no real table by this name.
-                  }
-               }
-            }
-            else if (fromType == TABLE)
+            if (fromType == TABLE)
             {
                // If fromNode is a "real" table, create a fake copy (a
                // shadow) of its parent database node and store it in the

=== modified file 'src/com/goldencode/p2j/uast/SymbolResolver.java'
--- src/com/goldencode/p2j/uast/SymbolResolver.java	2017-07-17 09:41:37 +0000
+++ src/com/goldencode/p2j/uast/SymbolResolver.java	2017-07-18 20:03:56 +0000
@@ -4738,13 +4738,13 @@
     *           The table reference in which to add fields.
     * @param    tablename
     *           The name of the table whose fields should be added.
-    * @param    skipGlobal
+    * @param    preferTemp
     *           if (@code true}, do not look in the global schema scope to find
     *           {@code tablename}.
     *
     * @return   The list of ASTs representing the copied fields.
     */
-   public Aast[] addFieldsFrom(Object table, String tablename, boolean skipGlobal)
+   public Aast[] addFieldsFrom(Object table, String tablename, boolean preferTemp)
    {
       List<Aast> fields = null;
       try
@@ -4760,7 +4760,7 @@
             // current schema
             if (dict.isTable(tablename))
             {
-               nn = dict.findTableFromNode(tablename, toTable, skipGlobal);
+               nn = dict.findTableFromNode(tablename, toTable, preferTemp);
             }
             
             return nn;
@@ -4776,7 +4776,7 @@
          }
          
          // make edits in our local schema dictionary
-         schemaDict.addFieldEntries(tablename, fromTable, toTable, skipGlobal);
+         schemaDict.addFieldEntries(tablename, fromTable, toTable);
          
          fields = new ArrayList<Aast>();
          Aast child = (Aast) toTable.getAst().getFirstChild();

=== modified file 'src/com/goldencode/p2j/uast/progress.g'
--- src/com/goldencode/p2j/uast/progress.g	2017-07-13 18:53:19 +0000
+++ src/com/goldencode/p2j/uast/progress.g	2017-07-18 20:15:15 +0000
@@ -5928,16 +5928,15 @@
     * @param    st
     *           Static specifier if not null. Only will be non-null if this is a resource defined
     *           as a member of a class definition.
-    * @param    forceTemp
-    *           if {@code true}, assume {@code recordName} represents a temp-table, and do not
-    *           search for it in the global schema scope when adding fields from it to the buffer
-    *           being define.
+    * @param    preferTemp
+    *           if {@code true}, temp-table is given precedence when the symbol name yields
+    *           both a temp and persistent table.
     */
    private void defineBufferFromSymbol(Aast symbol,
                                        String recordName,
                                        Aast am,
                                        Aast st,
-                                       boolean forceTemp)
+                                       boolean preferTemp)
    {
       String name = symbol.getText();
    
@@ -5947,7 +5946,7 @@
       // buffer is created in a global scope
       Object table = sym.addTable(name, BUFFER, bufferScope, symbol, am, st);
       
-      sym.addFieldsFrom(table, recordName, forceTemp);
+      sym.addFieldsFrom(table, recordName, preferTemp);
       
       String bufname = sym.lookupBufferName(name, false);
       symbol.putAnnotation("bufname", bufname);
@@ -10734,17 +10733,17 @@
       {
          if (#b != null && rtext != null)
          {
-            boolean forceTemp = false;
+            boolean preferTemp = false;
             AST ns = #b.getNextSibling();
             if (ns != null && ns.getType() == KW_FOR)
             {
                AST a = ns.getFirstChild();
                if (a != null && a.getType() == KW_TEMP_TAB)
                {
-                  forceTemp = true;
+                  preferTemp = true;
                }
             }
-            defineBufferFromSymbol(#b, rtext, am, st, forceTemp);
+            defineBufferFromSymbol(#b, rtext, am, st, preferTemp);
          }
       }
    ;
@@ -22209,7 +22208,7 @@
          if (b4table != null)
          {
             Object before = sym.addTable(b4table, TEMP_TABLE, true, #bt, null, null);
-            Aast[] fields = sym.addFieldsFrom(before, tablename);
+            Aast[] fields = sym.addFieldsFrom(before, tablename, true);
             
             for (int i = 0; i < fields.length; i++)
             {

