Function SG_ExecQuery() Returns boolean;
   Local string &sOutFile;
   Local Record &rcdQryRunParms;
   Local Record &rcdQryPrompts;
   Local SQL &sqlSelectQryParms;
   Local number &cnt, &i, &Result;
   Local ApiObject &aQry, &QryRunTimePrompts;
   Local boolean &bPublic;
   Local number &nOrigPSMessagesMode;
   Local ApiObject &PSMessages;
   Local number &MsgSetNbr, &MsgNbr;
   Local boolean &bResult;
  
   &nOrigPSMessagesMode = %Session.PSMessagesMode;
   %Session.PSMessagesMode = 1;
   &sQryName = "SG_VNDR_DEBTS2";
   &sOutFile = %FilePath | &sQryName | "_" | SG_VNDR_DEB_AET.PROCESS_INSTANCE;
   &aQry = %Session.GetQuery();
  
   If &aQry.Open(&sQryName, True, False) = 0 Then
      &rcdQryPrompts = &aQry.PromptRecord;
      If &rcdQryPrompts <> Null Then
         &QryRunTimePrompts = &aQry.RunTimePrompts;
         &rcdQryRunParms = CreateRecord(Record.SG_RUN_VNDRDBTS);
         &sqlSelectQryParms = CreateSQL("%Selectall(:1) WHERE OPRID = :2 AND RUN_CNTL_ID = :3");
         &sqlSelectQryParms.Execute(&rcdQryRunParms, %OperatorId, SG_VNDR_DEB_AET.RUN_CNTL_ID);
         While &sqlSelectQryParms.Fetch(&rcdQryRunParms)
            /* OPRID and RUN_CNTL_ID are here the only prompts for this query … */
            &rcdQryPrompts.GetField(Field.OPRID).Value = &rcdQryRunParms.GetField(Field.OPRID).Value;
            &rcdQryPrompts.GetField(Field.RUN_CNTL_ID).Value = &rcdQryRunParms.GetField(Field.RUN_CNTL_ID).Value;
            Break;
         End-While;
         &sqlSelectQryParms.Close();
      End-If;
      &Result = &aQry.RunToFile(&rcdQryPrompts, &sOutFile, %Query_XLS, 0);
   End-If;
   &aQry.Close();
  
   %Session.PSMessagesMode = &nOrigPSMessagesMode;
   If %Session.PSmessages.Count = 0 Then
      Return True;
   Else
      &bResult = True;
      &PSMessages = %Session.PSmessages;
      For &i = 1 To &PSMessages.Count
         If (&PSMessages.Item(&i).MessageType <= 1) Then
            &bResult = False;
            Break;
         End-If;
      End-For;
      Return &bResult;
   End-If;
End-Function; /* SG_ExecQuery */

If SG_ExecQuery() = True Then
   Exit (0);
Else
   Exit (1);
End-If;