Beruflich Dokumente
Kultur Dokumente
SQL in PeopleSoft
Some ways in which SQL is applied in PeopleSoft environment.
The three types of SQL commands that are used in SQR are:
Category
Command
SELECT command
Uses of SQR
Used for Reporting.
Background SQL Processing.
Used for Interfacing.
Move data from legacy
System to PeopleSoft System.
Move data from one module
to Other Module.
SECTIONS
A basic structure of a SQR Program
consists:
SETUP
PROGRAM
PROCEDURE
HEADING
FOOTING
The only section required is the Program
Section.
SETUP SECTION
Example:
Begin-setup
declare-layout default
paper-size =(8.5,11)
left-margin =1
right-margin =1
top-margin =1
bottom-margin =1
End-declare
End-setup
Begin-SQL
Insert ;
update;
delete;
End-SQL
PROGRAM SECTION
This is the most important section
that defines the Beginning & End of
the program. This is the only
mandatory section. Generally we
use this section to control the
general flow of processing by calling
different procedures.
PROCEDURE SECTION
This is an optional section and one of the
most important section in SQR. This can be
used to break the program in to
manageable and easy to maintain pieces.
There can be any no. of procedures in an
SQR program and they can be invoked by
using the DO command.
PARAGRAPHS
SELECT
SQL
DOCUMENT
Procedures
Begin-procedure procedure_name
[paragraphs]
[commands]
End-Procedure <procedure_name>
-----------------------------------------------------------------------Procedure name after End-Procedure is
optional (Not recommended)
Procedure is a list of commands and/or
paragraphs that are referenced by a
corresponding do command.
Procedure names must be unique.
HEADING SECTION
BEGINSELECT
BEGINDOCUMENT
BEGIN-SETUP
YES
NO
NO
BEGINPROGRAM
YES
YES
YES
BEGINPROCEDURE
YES
YES
BEGINHEADING
NO
NO
BEGINFOOTING
NO
NO
YES
NO
NO
Printing in SQR
2 types of printing
1. Explicit printing.
2. Implicit printing.
Explicit printing
Print {variable | literal} ([line] [,[column] [,[length]]])
Center
Wrap
Edit
Fill
Box
Shade
Bold
Underline
Edit masks
Edit argument is the most powerful
among all print commands. When this
argument is codedthe field is edited
before it is printed.
3 Types of Edits:
1. Text Edit.
2. Numeric Edit.
3. Date Edit.
Assignment statement
SQR supports move and let for assigning variables.
Move command is used to copy data from one variable
to the other. Edit mask can be used in conjunction with
move statement.
Eg: move $source_data to $target_Data [edit mask]
Operators
The following table lists operators in descending order of
precedence.
Operators of the same precedence are processed in the
sequence they appear in the expression, from left to right.
Use parentheses to override the normal precedence rules.
Substitution variables
Supply values at compile time.
Defined at the beginning of the SQR program using #definestatement.
Use the variable inside {}.
Functions
Type of Functions
Numeric
File-related
Date
String
Miscellaneous Functions
Numeric Functions
Numeric functions.
File-related Functions
These are helpful in performing file maintenance
These functions returns either a zero (0) to indicate success
or the value returned from the operating system to indicate
an error.
Delete
Deletes the file filename.
Syntax: stat_var = delete(filename)
filename = text literal, column, variable, or expression.
stat_var = decimal, float, or integer variable.
Example: let #fstatus = delete($filename)
Exists
Determines if the file, filename, exists.
Syntax: stat_var = exists(filename)
Example: let #fstatus = exists($filename)
Rename
Renames old_filename to new_filename.
Syntax: stat_var = rename(old_filename,
new_filename)
Example: let #fstatus =
rename($old_filename, $new_filename)
Date Functions
dateadd
Returns a date after adding (or subtracting) the specified units to
the date_value.
Example: let $date = dateadd($startdate, day, 7.5)
datedif
Returns the difference between the specified dates expressed in
units_value. The function returns a float value.
Syntax:
dst_var = datediff(date1_value, date2_value, units_value)
Example: let #diff = datediff($date1, $date2, hour)
datenow
Returns the current local date and time from the client machine.
Syntax:
dst_var = datenow()
Example: let $date = datenow()
datetostr
Converts the date date_value to a string in the format format_mask.
Syntax: dst_var = datetostr(date_value [, format_mask])
date_value = date variable or expression.
format_mask = text literal, column, variable, or expression.
Example:
let $formdate = datetostr($date, Day Mon DD, YYYY)
let $localedate = datetostr($date, DATE)
strtodate
Converts the string source_value in the format format_mask to a
date type.
Syntax: dst_var = strtodate(source_value [, format_mask])
source_value = text literal, column, variable, or expression.
format_mask = text literal, column, variable, or expression which
describes the exact format of the source_value.
dst_var = date variable.
Example: let $date = strtodate($str_date, Mon DD, YYYY)
let $date = strtodate($str_date, DATE)
STRING FUNCTIONS
Concatenation :
Let command is one of the most commonly used.
Eg: let $address=$city || , ||$state||,||zip
Extraction
Many methods to extract a portion of a string. Most common
are:
Substr function Allows you to extract starting at a specific
position and for a set of number of characters.
Eg: let $city=substr($address,1,2)
unstring function divides the string into two or more text
using a specified delimiter
Eg: unstring address by , into $city $state $zip
Upper/Lower
Sets the source to uppercase/lowercase and store in
target
Let $name=upper($name)
Let $name=lower($name)
To_char
Converts an integer variable to a string variable
Let $total=to_char(#total)
isblank
Returns a value of one (1) if source_val is an empty string, null string, or
composed entirely of whitespace characters; otherwise, returns a value
of zero(0).
Syntax: dst_var = isblank(source_value)
source_value = date or text literal, column, variable, or expression.
dst_var = decimal, float, or integer variable.
Example: let #blank = isblank(&description)
isnull
Returns a value of one (1) if source_val is null; otherwise, returns a value
of zero (0).
Syntax: dst_var = isnull(source_value)
source_value = date or text literal, column, variable, or expression.
dst_var = decimal, float, or integer variable.
Example: let #null = isnull($date)
String functions
length Returns the number of characters in source_value.
Syntax: dst_var = length(source_value)
source_value = date or text literal, column, variable, or
expression.
dst_var = decimal, float, or integer variable.
Example: let #length = length(&description)
lower Converts the contents of source_value to lowercase
and returns the result.
Syntax: dst_var = lower(source_value)
source_value = date or text literal, column, variable, or
expression.
dst_var = text variable.
Example: let $lower = lower(&description)
String Fucntions
lpad
ltrim
substr
Extracts the specified portion source_value. The extraction begins at
offset_value (origin is 1) for a length of length_value characters.
Syntax:
dst_var = substr(source_value, offset_value, length_value)
source_value = date or text literal, column, variable, or expression.
offset_value = decimal, float, or integer literal, column, variable, or
expression. The value is always converted to integer.
length_value = decimal, float, or integer literal, column, variable, or
expression. The value is always converted to integer.
dst_var = text variable.
Example: let $piece = substr(&record, 10, #len)
unstring function divides the string into two or more text using a
specified delimiter
Eg: unstring address by , into $city $state $zip
to_char
Converts source_value to a string, using maximum
precision.
Syntax:
dst_var = to_char(source_value)
source_value = decimal, float, or integer literal, column, variable,
or expression.
dst_var = text variable.
Example: let $string = to_char(#number)
to_number
Converts source_value to a number. This function returns a
float value.
Syntax: dst_var = to_number(source_value)
source_value = decimal, float, or integer literal, column,
variable, or expression.
dst_var = decimal, float, or integer variable.
Example: let #value = to_number($number)
upper
Converts the contents of source_value to uppercase and returns the result.
Syntax:
dst_var = upper(source_value)
source_value = date or text literal, column, variable, or expression.
dst_var = text variable.
Example: let $upper = upper(&description)
nvl
Returns y_value if the x_value is null otherwise returns x_value.
If x_value is numeric, y_value must also be numeric; otherwise, date and
textual arguments are compatible.
In any case, the x_value determines the type of expression returned. The return
value of the function depends on which value is returned.
Syntax:
dst_var = nvl(x_value, y_value)
x_value = Any literal, column, variable, or expression.
y_value = Any literal, column, variable, or expression.
dst_var = Any variable.
Example: let $city = nvl(&city, -- not city --)
Control Statements
Do
- Used to call Procedures.
If - conditional execution
Evaluate - Case/switch structure
While - Looping construct
Break - Exit from evaluate/while command
Exit-Select - exit from a select paragraph
Goto - Branch to a label
Stop - Halt the program abruptly
DO
Page commands
New-page
This command sends the current page buffer to the output devise and
begins a new one.
A new-page will automatically occur if page overflow occurs.
----------------------------------------------------------------------------------------------------Example:
Begin-select
A.COURSE (+1, 1, 5)
if #current-line > 60
new-page
end-if
End-select
PAGE-NUMBER
This function places the current page number on the
page.
LAST-PAGE
This function places the last page number on the page.
Using LAST-PAGE causes SQR to delay printing until the
last page has been processed so that the number of the
last page is known.
-----------------------------------------------------------------------------Begin-footing 1
PAGE-NUMBER (1,35) Page
LAST-PAGE () of .
End-footing
Output will be like Page 12 of 25.
Example:
begin-select
state (,1) on-break skiplines=1
city (,7)
name (,24)
phone (,55)
position (+1) ! Advance to the next line
from customers
order by state, city, name
end-select
begin-select
state (,1) on-break print=change/top-page
after=state_tot
name (,11)
addr1 (+1,11)
city (+1,11)
phone (,+2) edit (xxx)bxxx-xxxx
next-listing skiplines=1 need=4
let #state_total = #state_total + 1
from customers
order by state, name
end-select
if #cust_total > 0
print ' Total Customers: ' (+3,1)
print #cust_total () edit 999,999
end-if
Lookup Tables
Load lookup is a array which can be used to retrieve information from multiple tables
and improve the program performance. It can be used to specify the table joins.
Load lookup command creates an internal memory array and populates this array
with keys and corresponding column values from the specified tables.
This can be used either in setup section or in any procedure section.
Load lookup retrieves two fields from the database:
1)Key Field
2)Return Value.
----------------------------------------------------------------------------------------------------Syntax: Load-Lookup
name={lookup name}
table={database name}
key={key value}
return_value={return key}
where={where criteria}
Name=Name of the lookup table.
Table=The name of the database table/view on which the lookup is created
Key=The key field of the table that is to be used to lookup on data in that table
ARRAY
Array is a collection of similar groups of fields in the
program memory where in each group can be referred
to by its relative position number in the array.
Arrays are created during the compile stage, before
the program is executed.
CREATE-ARRAY
NAME=array_name
SIZE=nn
{FIELD=name.type[:occurs]
[={init_value_txt_lit | num_lit}]}
NAME Name of the array
SIZE No of elements in array
FIELD Each field or column in the array
Type char,interger,date etc
Example
CREATE-ARRAY
NAME=emps
SIZE=10
FIELD=EMPLID:char:11
FIELD=SALARY:number:12
FILED=PHONE:char:14
Maximum no of arrays in a program is 128
Maximum no of fields per array is 200
Create-Array
Clear-Array
Get
Put
Array-Add
Array-Subtract
Array-Multiply
Array-Divide
SQC:
The frequently used procedures can be grouped by their functionality
and saved as .SQC file, which can be used in any SQR program.
SQC is likely a library of frequently used procedures. There are
approximately 700 PeopleSoft built in SQC.
#include setenv.sqc Sets SQL platform specific environments
#include setup31.sqc Setup section for Portrait mode
#include setup32.sqc Setup section for Landscape mode
#include reset.sqc
Reset printer Prints End of Report in last
page
#include curdttm.sqc Current date and time
#include datetime.sqc Routines for Date and time formatting
#include number.sqc Routine to format numbers
#include prcsapi.sqc Update process request API
#include prcsdef.sqc Update process request variable declaration
#include setup32.sqc - To print landscape and portrait form.
SQRW:
Pshome\bin\sqr\MSS\BINW\sqrw.exe
Load Look Up
Array
Multiple Reports
Depending on the reports we generate we use no. of
layouts.
We can direct each report to different printer
Multiple reports can be generated using a single SQR
program with the help of DECLARE-REPORT and USEREPORT commands.
By using USE-REPORT command we have to bipass
the data in to particular reports.
We have to use the FOR-REPORT command foe
different headings and Footings.
DECLARE-REPORT report_name
LAYOUT=layout_name
PRINTER-TYPE=printer_type
END-DECLARE
Examples
declare-layout customer_layout
paper-size=(a4)
left-margin=12.7 mm
right-margin=25.4 mm
end-declare
declare-layout summary_layout
paper-size=(14, 11)
left-margin=1
right-margin=1
end-declare
declare-report customer_detail
layout=customer_layout
printer-type=postscript
end-declare
declare-report customer_summary
layout=summary_layout
printer-type=postscript
end-declare
USE-REPORT report_name
For programs with multiple reports, allows the user to switch
between reports.
All subsequent PRINT commands will write to the same report
until the next USE-REPORT command encounters.
The USE-REPORT command can be used to specify to which
report file(s) the subsequent report output is to be written. An
application can contain several USEREPORT statements to control
several reports.
Examples
use-report customer_detail
use-printer-type PostScript
print (1, 1) Customer Name:
print () $customer_name
INTERFACING:
You can use sequential files to
Import data from other systems into your database
tables.
Unload data from your database tables into sequential
files, and use the files as interfaces to external systems.
Sort your data using the operating system commands
or utilities.
Input/Output Commands:
Opening a file :
Before accessing a file, it must be opened.
OPEN {filename} AS {num} {FOR-READING | FOR-WRITING
| FOR-APPEND} {RECORD=length_num[:FIXED | :VARY]}
{STATUS=num_var}
LET $filename='C:\USER\SQR\deductions.txt'
OPEN $filename AS 1 FOR-READING RECORD=80
STATUS=#file1_status
#file1_status=0 Successfully opened
Reading a File:
READ {file_number} INTO {any_var:length}
[STATUS=read_status_num_var]
READ 1 INTO $Input_Record:80
READ 1 INTO $last_name:20 $first_name:10 $address:50
#end-file End of File
SQR internal variable #end-file is set to 1 when there are no more
records to read
Writing a File:
WRITE {file_number} FROM {text_var | num_var |
date_var} [STATUS=write_staus_num_var]
WRITE 1 FROM $Record:80 STATUS=#write_stat
WRITE 2 FROM $last_name:20 , $first_name:10 ,
$address:50
Closing a File: CLOSE 1
OUT-BOUND
BEGIN-PROGRAM
Let $writeFile = C:\temp\TEST_FILE.txt
Open $writeFile AS 1 for-Writing Record =100 STATUS =#fileStat
IF #fileStat ! =0
PRINT ERROR IN OPENING FILE (2,1)
ELSE
DO WRITE-DATA
CLOSE 1
END-IF
END-PROGRAM
BEGIN-PROGRAM WRITE-DATA
BEGIN-SELECT
EMPLID
DEPTID
NAME
WRITE 1 FROM &DEPITD : 6 &EMPLID :11 &NAME :50
FROM PS_EMPLOYMENT
END-SELECT
END-PROCEDURE
INBOUND:
BEGIN-PROGRAM
Let $writeFile = C:\temp\TEST_FILE.txt
Open $writeFile AS 1 for-Writing Record =100 STATUS =#fileStat
IF #fileStat ! =0
PRINT ERROR IN OPENING FILE (2,1)
ELSE
WHILE 1
READ 1 into $line:40
unstring $line by , into $deptid $emplid $name
do insertion
If #end-file
BREAK
END-IF
END-WHILE
END-IF
CLOSE 1
END-PROGRAM
BEGIN-PROCEDURE INSERTION
BEGIN-SQL
INSERT INTO PS_TEST
(DEPTID,EMPLID,NAME)
VALUES ($DEPTID, $EMPLID , $NAME)
END-SQL
END-PROCEDURE
Debugging SQR:
Display statement
Show statement
#Debug command and Command line
flags
ERROR HANDLING
Compile Stage:
Begin-SQL ON-ERROR
Skip/warn/Stop
Execution Stage:
Begin-Select ON-ERROR Procedure
Name
Begin-SQL ON-ERROR Procedure
Name
-O <path> : Log file.log Error or
Success.
-E <path> : Error File.err Error &
DYNAMIC SQR:
Where the substitution variable is resolved during
runtime, you can replace the entire where clause as
substitution variable and still it is called static SQL.
Eg:
Let $Where_C=Where A.COURSE_DATE=||
$Course_Dt
Begin-Select
A.COURSE
A.DESCR
FROM PS_TRAINING A
[$where_C]
End-Select
Compile Stage
All #Include external source file (SQC) are inserted into
the source.
All #commands are evaluated. The #DEFINE text
substitution variables are placed into the program
source.
The SETUP section is processed, including allocation
and population of memory arrays created by the LoadLookup and Array commands.
Work buffers are allocated.
Optimization of the SQL data access path is
determined.
Syntax of SQR source program is checked.
Execution Stage
Processes data for the Body section
Processes HEADING section
Processes FOOTING section
Writes entire page buffer to file.
Printer
Process Scheduler
SQR Program
Flat File
Process Monitor
Program Run Status
Information
Types of Reports:
Tabular Report -: Getting Data from single Table.
Master Detail Report : Used to get data from
more than one table.
Form Report: Special kind of report like payslip
for each & every row of data in the table we
have a new page.
Mailing Lables: Effective use paper space.
Letters & Documents: Used for the same.
Cross Tabular Reports : Used for calculations in
row wise as well as in column wise.
Graphical Reports: Like bar charts, PIE diagrams
etc..
Exercise - 1
1)Create a table (Organisation, Department id, Emp id,
name, Emp.type, Base Salary, HRA, Allowances, Over
time)*
2)Create a fixed length record file with about 20 rows of data
for the above table.
3)Write a sqr program that would read through your file and
insert into the table you created in step 1
Exercise 2
4)Read data from the above table
5)Write it out a salary report (calculating the total pay), by
emp.type. i.e., first all permanent employees then
contractors.
6)For each organisation/department change, the report
should be printed on a fresh page.
7)Also, create a comma separated file of all contractors
Thanks for
Being Patience