Вы находитесь на странице: 1из 2

Set Processing

Explained
Start Thinking About Data in Sets, Not Rows.
B Y

J O E

W E E S S I E S

S et processing is not a new concept.


It has been around since SQL was
invented. In fact, SQL allowed set
processing to be used easily giving
SQL its great power. With
PeopleSoft built on top of a SQL

Set Processing is not difficult


to use, but it takes a new
way of looking at data and
program design.

processing (non-set processing is


also called row-by-row processing).
The most important reason is that
normally we do not tend to think in
set terms. We tend to think in rowby-row when designing our
programs. Even some flow charting
programs use row-by-row
processing. These programs can be
used in set processing but you have
to use them knowing set processing

Listing 1

database, you can and should use Set


processing to build your programs.
Set processing is not relegated to
batch programs such as SQR and
Application Engine, but can be used
within PeopleCode as well.
Why isnt set processing used
everywhere? Set Processing is not
difficult to use, but it takes a new
way of looking at data and program
design. Set Processing also cannot
be used everywhere since something
as simple as printing a report
requires some use of non-set

Employee Record
Employee_id - Key
Department_cd
Dept Update Record
Old_Dept_cd - Key
New_Dept_cd

Listing 2
Do Select
%select(employee_id,
department_cd)
select employee_id,
department_cd from
ps_employee

V P 1 O N L I N E . C O M

26

Listing 3

Listing 4

SQL
%select(new_dept_cd)
select new_dept_cd from
ps_dept_update where
old_dept_cd =
%bind(department_cd)

SQL
Update ps_employee set
department_cd =
%bind(new_dept_cd) where
employee_id =
%bind(employee_id)

logic, which I hope you are going to


learn here.
To learn about set processing you
need to understand its differences
from row-by-row processing.
Starting with a classic example, say
you have to update all your
employee records setting new
department codes based on a new
update table. (See Listing 1)
To start the program (for this
example Application Engine code is
shown) we would write a select
statement gathering the employee
information. This select statement
would be in a Do Select type. (See
Listing 2)
The next statement would take the
bind variables for each employee
row and get the new dept record to
use. (See Listing 3)

Listing 5

27

SQL
update ps_employee
set department_cd = (select
A.new_dept_cd from
ps_update_dept A where
ps_employee.department_cd =
A.old_dept_cd)

V P 1

J A N

F E B

2 0 0 3

Now that we have the new


department record we can update the
employee record with the new value.
(See Listing 4)
With our one employee record
updated we need the program to go
back to the original select and get
the next employee. This program
would then run through all of the
employees looping row by row
through the employee record
updating them one by one.
With this simple example lets
review the process as if we had used
set processing. We still need to
update the employees department
code but instead of looping through
each employee we want to see if we
can update the employee record in a
set statement. (See Listing 5)
Even though this example is overly
simplistic, it does show that without
stopping and trying to think in set
processing terms, you can write
inefficient SQL. Even with writing
reports you can work in set
processing for the logic of gathering
the data, although you still have to
do row-by-row for the actual
output. Heres a quick example