Sei sulla pagina 1di 2

Meta SQL that Matters

Save hours when writing complex SQL statements

B Y

JOE

WEESSIES

Have you ever had to type in a SQL statement that just seemed to take
Have you ever had to type in a SQL
statement that just seemed to take forever?
Well I have, but PeopleSoft gives you some neat
tricks to help with putting SQL together easier
and faster. These are Meta SQL commands that
perform various functions in building SQL
statements. Meta SQL commands are used in
SQLExec commands, ScrollSelect functions,
Rowset Class methods, SQL objects,
Application Engine SQL, COBOL and even in
the Application Designer for building dynamic
views.
PeopleSoft developers are starting to use these
commands in more and more of their delivered
programs. These do take some getting used to
but once you figure them out, it is really quite
simple. Everyone is used to using Meta SQL in
Application Engine programs. The most
commonly used command is “%Bind”.
%List – creates a list of field names based
on a record.
%ListEqual – creates a list of fields from
two records with an equal between them.
%Bind
The &Bind command, when followed by the
AET record and field name, converts to the
value in the SQL statement. This allows you to
write dynamic SQL statements. (See Listing 1)
• %EffDtCheck – creates a subselect
statement for an effective date controlled
record.
• %InsertSelect – creates a insert SQL
statement.
In the where clause the %bind Meta SQL
command is followed by the field name. The
value in this field is then converted into the SQL
statement when it is run. There are more
parameters to this %bind command but those
are outside the scope of this article.
• %Join – creates a where clause joining two
records.
%List
You should have a basic skill in using the Meta
SQL commands. We will now build on this skill
in using some of the more advanced Meta SQL
commands. There are five commands we will
concentrate on here that you can use to build
large SQL statements in writing a few
commands.
This command gives you a list of field names
from one Record based on the parameters you
define. This command is made up of two
parameters with varying options. The first
parameter determines the fields to use within
the defined Record (second parameter) that are
to be made into a comma-separated list. For
this first parameter, there are four different
options, but you can only select one.
(See Table 1)

VP1 ONLINE.COM

30

31

Listing 1 %Select(Run_date, Business_unit) select ship_date, business_unit from PS_SHIP_INF_INV where order_no =
Listing 1
%Select(Run_date, Business_unit)
select ship_date, business_unit from PS_SHIP_INF_INV
where order_no = %bind(order_no)
The second parameter is the Record name. This
can just be the name or a bind variable, but not
a string of characters. This parameter can also
have an alias included as well, if the field list
being built needs one. Consider this example.
Note: The field list generated must match
field types within a SQL statement so that if
you are using an insert / select type
statement, the select field types must line up
with the insert field types; otherwise you will
receive a run time SQL error.
Insert into PS_TMP2
(%List(FIELD_LIST, TMP2))
Select %List(KEY_FIELDS, TMPMST A)
from PS_TMP1 A
The first line of the example shows that you can
use the Record itself to build a full field list. The
field list is built following the order of the Fields
as defined on the Record definition. The first
command uses all the Fields but does not use
the alias condition as shown in line 2. Here, a
select statement field list is created and is
aliased with the value of ‘A’.
Insert into PS_TMP2 (%List(TMPA),
%List(TMPB), %List(TMPC))
Select %List(FIELD_LIST_NOLONGS,
TMPMST A) from PS_TMP1 A
You can use this command more than once in a
SQL statement. For an insert SQL statement,
you can have one build the insert field list, a
second build the select field list and a third the
Order By clause. You can even have two or
more of these commands build one select field
list. There is no limit on the number of times
within one SQL statement that you can use this
command.
There are some issues to be careful of when
using this %List command. The Field order is
based on the Record Definition - so if anyone
changes this, you have just changed all of your
underlying SQL statements. You need to be
aware of this condition when you are modifying
Record definitions. PeopleSoft recommends
that if you create a statement, like in the very
first example, you should create it as follows:
Insert into PS_TMP2
(%List(KEY_FIELDS, TMPMST))
Select %List(KEY_FIELDS, TMPMST A)
from PS_TMP1 A
The command uses the same Record to build
not only the insert field list but the select field
list as well. This way when you change the
Table 1
Value
Action
KEY_FIELDS
Build using only Fields set as Key.
Alternate Key does
not build.
FIELD_LIST
Build using all Fields
FIELD_LIST_NOLONGS
Build using all Fields except fields with LONG
character type.
ORDER_BY
Build using all Fields and add ‘DESC’ behind all fields
where the order has been set to descending in the Record
Definition. This option is used when this command is to
build a field list within the Order By clause of a SQL
statement. Do not use this parameter within a standard
select statement or you will get an error.
VP1
MAY
/
JUN
2003