Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

How to Fetch data into an Array using SQL Object

How to Fetch data into an Array using SQL Object
When you use some of the SQL class built-in functions such as CreateSQL, you use Fetch method to retrieve the next row of data from the SELECT that is open on the SQL object.
Sometimes we use list of output variables to catch the data from the SQL fetch. These are the values of the fields from the selected row and are assigned in order to the given output variables. But, sometimes it is wise to use an array to store the fetch results.
In general this can be used when you don't know how many values are return from the SQL fetch or if you have significant number of fetch results are returned. In this case you may need lot of output variables which is cumbersome to maintain within the program.

You can do this as;
Local array of any &arrayFetch = CreateArrayAny();

&SQL = CreateSQL(SQL.YOUR_SQL_OBJECT);

While &SQL.Fetch(&arrayFetch)
 /* Process the row in &arrayFetch . */ 
 MessageBox(0,"",0,0,"First Value: " | &arrayFetch[1] | " Second Value: " | &arrayFetch[2] | " ... " | " Nth Value: " | &arrayFetch[3],0); 
End-While;

How to Display a detailed SQL error (Online)

How to Display a detailed SQL error (Online)
Sometimes when you navigate to a PeopleSoft page or while doing some work online you might come up with this error.

"A sql error occurred. Please consult log for Administrator."

As developers when we build and test it is handy if we can see detailed error so that we can rectify it quickly. Sometimes it is not that obvious where the error is coming from even though you build the records, views etc.. Otherwise, you have to get the Appserver logs to find out where the error is coming from.

There is a way you can enable SQL error details so that it will be displayed to the users (in this case developers)

If you want SQL error details to be visible to users, set this property as follows:
Suppress SQL Error=0

This property exists in the PSTOOLS section of the PSAPPSRV.CFG file.

"For security purposes, this option has a default value of 1 to prevent SQL error details from being displayed to users.
Any SQL errors that occur don't display details, but refer users to consult the system log.
The details that were in the SQL message are written to the log file. This helps to prevent SQL injection vulnerabilities."

So, if you want to see the detailed SQL error online, ask your PeopleSoft Sysadmin to change this option in App Server PSAPPSRV.CFG file so that next time you can see the detailed SQL error message.

How to dynamically populate data in a grid using SQL views

How to dynamically populate data in a grid using SQL views
PeopleSoft Grids are powerful page controls in PeopleSoft. It can display information in a spreadsheet format and you can embed other page controls such as buttons, drop-down lists, html areas, etc.. to the grid. You use grids depending your table structure and how your page will display to the users based on user requirements. Grids are used in occur Level 1, 2 or 3 and it depends on whether you have more than one underlying record definition on a page.

Sometimes you want to pull information from multiple records and display in a grid on a page.
Since, a grid can associate with only one primary record, you have to create a view to get data from one or more records. Then you can associate the view record as the primary record in the grid.
However, imagine that you want to populate the grid dynamically passing parameters to the view.
Since, views cannot accept parameters dynamically (unlike SQL definitions or dynamic views) how do you handle this kind of situation. You can achieve that with the help of Select method in Rowset class. Select, reads data from the database tables or views into either a row or rowset object.

A good example from PeopleBooks
The following example selects into the child scroll of the level one rowset.  Each row fetched is placed under the appropriate row in &LEVEL1.
Note that instead of hard-coding the WHERE clause, the SQL repository is used to access a SQL definition named SELECT_WHERE.

&LEVEL1 = &LEVEL0()(1).GetRowset(SCROLL.EMPL_CHECKLIST);
&LEVEL1.Select(SCROLL.EMPL_CHKLST_ITM, RECORD.EMPL_CHKLST_ITM, SQL.SELECT_WHERE);
Another way you can achieve this, by creating a function to accept dynamic parameters (to WHERE clause) and then calling that function in appropriate event to populate the grid.
Note that here we use rowset Flush method to remove all rows from the rowset and free its associated buffer before selecting to the grid.
Flush method is often used to clear a work scroll before using the Select method.

rem Function to Refresh the Grid;
Function RefreshMyGrid(&Param1, &Param2,..,&ParamN)
   &rs = GetLevel0()(1).GetRowset(Scroll.MY_GRID_VW);
   &rs.Flush();
   &rs.Select(Record.MY_GRID_VW, "WHERE MYFIELD1 = :1 AND MYFIELD2=:2, MYFIELDN=:N", &Param1,&Param2,&ParamN);
End-Function;

Important PeopleSoft Queries Part I

Important PeopleSoft Queries Part I

Some of the PeopleSoft PeopleTools Queries that someone might find useful;

How to find database tables (i.e SQL records) attached to a page?

SELECT A.LBLTEXT, A.RECNAME, A.FIELDNAME, A.PNLNAME
FROM PSPNLFIELD A
WHERE A,PNLNAME = :1 --Page Name
AND A.RECNAME NOT LIKE '%XLAT%'
AND A.RECNAME NOT LIKE '%WRK%'
AND A.RECNAME NOT LIKE '%DERIVED%'
AND A.FIELDNAME <> ' '
ORDER BY 2;

How to find objectownerid of a record definition?

SELECT A.RECNAME, A.RECDESCR, A.OBJECTOWNERID, A.DESCRLONG
FROM PSRECDEFN A
WHERE A.RECNAME = :1 --Record Name

How to find list of record definitions (tables) the belong to a particular object owner ?

SELECT A.RECNAME, A.RECDESCR, A.OBJECTOWNERID, A.DESCRLONG
FROM PSRECDEFN A
WHERE A.OBJECTOWNERID= :1 --Object owner id

How to find PS Queries that select data from specific table

SELECT A.OPRID, A.QRYNAME, A.RECNAME, A.FIELDNAME
FROM PSQRYFIELD A
WHERE A.RECNAME LIKE :1 -- Record Name

How to find PS Queries that use specific field

SELECT A.OPRID, A.QRYNAME, A.RECNAME, A.FIELDNAME
FROM PSQRYFIELD A
WHERE A.FIELDNAME LIKE :1 -- Field Name

How to find Queries that were not delivered by PeopleSoft

SELECT A.OPRID, A.QRYNAME, A.DESCR
FROM PSQRYDEFN A
WHERE A.LASTUPDOPRID <> 'PPLSOFT'

How to Find the Record Type of a Record in PeopleSoft

How to Find the Record Type of a Record in PeopleSoft
This simple SQL will show you how to find basic information about a record type.
SELECT RECNAME,
DECODE (RECTYPE,
     0, 'SQL Table',
     1, 'SQL View',
     2, 'Derived',
     3, 'Sub Record',
     5, 'Dynamic View',
     6, 'Query View',
     7, 'Temporary Table',
     'Unknown') As RecordType,
RECDESCR,
PARENTRECNAME,
AUDITRECNAME
FROM PSRECDEFN WHERE RECNAME = 'ACAD_PROG'; --Record Name

How to use USEEDIT field to find out Record Field Properties in PeopleSoft

How to use USEEDIT field to find out Record Field Properties in PeopleSoft
This handy SQL script will provide how to find Record Field properties such as Key Fields, Edit Fields, Default values etc.. of a Record. Presented here are two versions of SQLs. First one give is quite simple and give you enough information about record field properties of a record and it uses PSRECFIELD tools table. Second one is bit complex and will give you more in-depth information such as last update datetime and operator id, etc..

Side Note: The SQLs are using Oracle Function BITAND. Also if you want to know more info about USEEDIT field please click here.
USEEDIT field assigns a decimal value to each individual bit value.

SQL 1 (simple)

SELECT
  FIELDNAME,
  DECODE(BITAND(USEEDIT, 1), 1 ,'Y', DECODE(BITAND(USEEDIT, 16), 16, 'Alt')) as Key,
  DECODE(BITAND(USEEDIT, 2048),2048 , 'Y' , '') as Search,
  DECODE(BITAND(USEEDIT, 32), 32 ,'Y','') AS List,
  DECODE(BITAND(USEEDIT, 256), 256 ,'Y','')AS Req,
  DECODE(BITAND(USEEDIT, 512), 512 ,'Xlat', DECODE(BITAND(USEEDIT, 8192), 8192 ,'Y/N', DECODE(BITAND(USEEDIT, 16384),16384 ,'Prompt',''))) AS Edit,
  EDITTABLE, DEFFIELDNAME
FROM PSRECFIELDDB
WHERE
  RECNAME = 'ACAD_PROG' --Record Name
ORDER BY FIELDNUM;

SQL 2 (complex)

SELECT A.recname,        A.fieldname,
       CASE
         WHEN B.fieldtype = 0 THEN 'CHAR'
         WHEN B.fieldtype = 1 THEN 'LONG CHAR'
         WHEN B.fieldtype = 2 THEN 'NUMBER'
         WHEN B.fieldtype = 3 THEN 'SIGNED NBR'
         WHEN B.fieldtype = 4 THEN 'DATE'
         WHEN B.fieldtype = 5 THEN 'TIME'
         WHEN B.fieldtype = 6 THEN 'DATETIME'
         WHEN B.fieldtype = 7
               OR B.fieldtype = 8 THEN 'IMAGE'
         ELSE NULL
       END           AS FIELDTYPE,
       CASE
         WHEN B.fieldtype = 2                OR B.fieldtype = 3 THEN Trim(To_char(B.length))                                       || '.' || To_char(B.decimalpos)
         ELSE To_char(B.length)
       END           AS FLDLEN,
       CASE
         WHEN Bitand(A.useedit, 256) > 0 THEN 'YES'
         ELSE 'NO'
       END           AS REQ,
       CASE
         WHEN Bitand(A.useedit, 1) > 0 THEN 'KEY'
         WHEN Bitand(A.useedit, 2) > 0 THEN 'DUP'
         WHEN Bitand(A.useedit, 16) > 0 THEN 'ALT'
         ELSE NULL 
       END           AS KEY_TYPE, 
       CASE 
         WHEN Bitand(A.useedit, 64) > 0 THEN 'DESC' 
         WHEN ( Bitand(A.useedit, 1) > 0 
                 OR Bitand(A.useedit, 2) > 0 
                 OR Bitand(A.useedit, 16) > 0 ) 
              AND Bitand(A.useedit, 64) = 0 THEN 'ASC' 
         ELSE NULL 
       END           AS DIR, 
       CASE 
         WHEN Bitand(A.useedit, 2048) > 0 THEN 'YES' 
         ELSE 'NO' 
       END           AS SRCH, 
       CASE 
         WHEN Bitand(A.useedit, 32) > 0 THEN 'YES' 
         ELSE 'NO' 
       END           AS LIST, 
       CASE 
         WHEN Bitand(A.useedit, 4) > 0 THEN 'YES' 
         ELSE 'NO' 
       END           AS SYS, 
       CASE 
         WHEN Trim(A.defrecname) = '' THEN A.deffieldname 
         ELSE Trim(A.defrecname) 
              || '.' 
              || A.deffieldname 
       END           AS DEFAULT_VALUE, 
       CASE 
         WHEN Bitand(A.useedit, 8) > 0 
              AND Bitand(A.useedit, 128) = 0 
              AND Bitand(A.useedit, 1024) = 0 THEN 'A' 
         WHEN Bitand(A.useedit, 8) > 0 
              AND Bitand(A.useedit, 128) > 0 
              AND Bitand(A.useedit, 1024) = 0 THEN 'AC' 
         WHEN Bitand(A.useedit, 8) > 0 
              AND Bitand(A.useedit, 128) > 0 
              AND Bitand(A.useedit, 1024) > 0 THEN 'ACD' 
         WHEN Bitand(A.useedit, 8) = 0 
              AND Bitand(A.useedit, 128) > 0 
              AND Bitand(A.useedit, 1024) = 0 THEN 'C' 
         WHEN Bitand(A.useedit, 8) = 0 
              AND Bitand(A.useedit, 128) > 0 
              AND Bitand(A.useedit, 1024) > 0 THEN 'CD' 
         WHEN Bitand(A.useedit, 8) = 0 
              AND Bitand(A.useedit, 128) = 0 
              AND Bitand(A.useedit, 1024) > 0 THEN 'D' 
         ELSE NULL 
       END           AS AUDT, 
       CASE 
         WHEN Bitand(A.useedit, 16384) > 0 THEN 'PROMPT' 
         WHEN Bitand(A.useedit, 512) > 0 THEN 'XLAT' 
         WHEN Bitand(A.useedit, 8192) > 0 THEN 'Y/N' 
         ELSE NULL 
       END           AS EDIT, 
       A.edittable   AS PROMPT_TABLE, 
       A.setcntrlfld AS SET_CONTROL_FLD, 
       CASE 
         WHEN Bitand(A.useedit, 4096) > 0 THEN 'YES' 
         ELSE 'NO' 
       END           AS REASONABLE_DT, 
       CASE 
         WHEN Bitand(A.useedit, 32768) > 0 THEN 'YES' 
         ELSE 'NO' 
       END           AS AUTO_UPDT, 
       CASE 
         WHEN Bitand(A.useedit, 262144) > 0 THEN 'FROM' 
         WHEN Bitand(A.useedit, 524288) > 0 THEN 'THROUGH' 
         ELSE NULL 
       END           AS SEARCH_FIELD, 
       CASE 
         WHEN A.subrecord = 'Y' THEN 'YES' 
         ELSE 'NO' 
       END           AS SUBRECORD, 
       A.lastupddttm, 
       A.lastupdoprid 
FROM   psrecfield A, 
       psdbfield B 
WHERE  A.recname = :1 --paste recname here 
       AND A.fieldname = B.fieldname 
ORDER  BY fieldnum;