0% found this document useful (0 votes)
62 views56 pages

SQL/400 Tips and Tricks: Bill Fortenberry Books A Million

The document provides SQL code examples and tips for querying and manipulating data in an SQL/400 database. It includes examples of selecting, inserting, updating, and deleting records using both static and dynamic SQL. It also demonstrates different types of joins, subqueries, aggregate functions, and the UNION clause. The examples range from basic queries to more advanced concepts like correlated subqueries and common table expressions.

Uploaded by

Arun G Nair
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views56 pages

SQL/400 Tips and Tricks: Bill Fortenberry Books A Million

The document provides SQL code examples and tips for querying and manipulating data in an SQL/400 database. It includes examples of selecting, inserting, updating, and deleting records using both static and dynamic SQL. It also demonstrates different types of joins, subqueries, aggregate functions, and the UNION clause. The examples range from basic queries to more advanced concepts like correlated subqueries and common table expressions.

Uploaded by

Arun G Nair
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 56

SQL/400

Tips and Tricks

Bill Fortenberry
Books A Million
Why?
•Industry standard / Cross platform

•Learn four statements / Big payback

•Interactive SQL

•Embedded SQL / Dynamic SQL


CUSTOMER LISTING
STATE: AL
BY DESC YTD ORDERS
 
CUST NAME ORDERS SALES REP
------------ ------ --------------
ABC Corp. 75 Freddy Smith
Smith & Smith 63 Joe Jackson
DEF Company 55 < Not Found >
E&F Inc. 40 Freddy Smith
Select cusmst.cuname
cusmst.cuytdc
Ifnull(repmst.rename,
’< Not Found >’)
From cusmst left outer join repmst
on cusmst.curep = repmst.rerep
Where custat = :StateID
Order by 2 desc, 1
Select cusmst.cuname
cusmst.cuytdd
Ifnull(repmst.rename
’< Not Found >’)
From cusmst left outer join repmst
on cusmst.curep = repmst.rerep
Where custat = :StateID
Order by 2 desc, 1
Select cusmst.cuname
cusmst.cuytdd /
cusmst.cuytdd
Ifnull(repmst.rename,
’< Not Found >’)
From cusmst left outer join repmst
on cusmst.curep = repmst.rerep
Where custat = :StateID
Order by 2 desc, 1
Select cusmst.cuname
cusmst.cuytdd /
cusmst.cuytdd
Ifnull(repmst.rename,
’< Not Found >’)
From cusmst left outer join
repmst
on cusmst.curep = repmst.rerep
Where custat = :StateID
Order by 3, 1
Select * from invmst

Select inumbr, idept, idesc


from invmst

Select inumbr, idept, idesc


from invmst
where istat = ‘A’
Select inumbr, idept, idesc
from invmst
where istat = ‘A’
order by inumbr

Select inumbr, idept, upper(idesc),


qtyohd * avgcst as invval
from invmst
where istat = ‘A’ and ibuyer <> 173
order by idept desc, inumbr
Delete from invmst

Delete from invmst


where idept = 12
Insert into cumast
(cunbr, cuname, cucity)
Values
(678987, ‘Atlas Inc.’, ‘Birmingham’)
Insert into cumast
(cunbr, cuname, cucity)
select cwidno, cwcnam, cutown
from newcus
where cutown = ‘Atlanta’
Update cumast
set cuname = ‘Acme Builders’,
cudisc = cudisc + .01
where custid = 123456

Update customer
set row = (‘Atlas’, 13246,
‘Meridian’)
where custid = 2354
Interactive SQL
• Allows prompting for statement syntax.
• Good learning tool.
• Reduces the need for fixit programs.
• Programming tool for Embedded SQL.
• Allows you to quickly destroy data.
Convert Select to Delete
1. Press F9 to retrieve the select command.
Select * from ordhdr where ohornm = 123456

2. Type DELETE over SELECT *.


Delete from ordhdr where ohornm = 123456

3. Press enter.
Convert Select to Insert
1. Press F9 to retrieve the select command.
Select * from orddtl where odornm = 123456

2. Press F15 to push the select statement down a line, then type the
insert command.
insert into mylib/orddtl
Select * from orddtl where odornm = 123456

3. Press enter.
Convert Select to Update
1. Press F9 to retrieve the select command.
Select * from orddtl where odornm = 123456
2. Type UPDATE over SELECT and use the delete key to remove *
FROM.
update orddtl where odornm = 123456
3. Place the cursor on the W in where, and press F15 twice. This will split
the statement at the where clause and push it down two lines.
update orddtl
 
where odornm = 123456
4. Use the blank line for your set clause.
update orddtl
set odstat = ‘P’
where odornm = 123456
Static SQL
• C/Exec SQL
• C+ Update cumast
• C+ set cuname = ‘Acme Builders’
• C+ where cunmbr = 12345
• C/End-Exec

• C/Exec SQL
• C+ Update cumast
• C+ set cuname = :NewName
• C+ where cunmbr = :Customer
• C/End-Exec
RPG code to delete records
C OrderNum chain rordhdr
C if %found
C delete rordhdr
C endif
C
C OrderNum setll rorddtl
C OrderNum reade rorddtl
C dow not %eof
C delete rorddtl
C OrderNum reade rorddtl
C enddo
SQL code to delete records
C/Exec SQL
C+ Delete from ordhdr
C+ where ohornm = :OrderNum
C/End-Exec
 
C/Exec SQL
C+ Delete from orddtl
C+ where odornm = :OrderNum
C/End-Exec
Selecting one row from a table

C/Exec SQL
C+ Select idept, idescr
C+ Into :DeptNum, :SkuDescr
C+ From invmst
C+ Where inumbr = :SkuNumber
C/End-Exec
Selecting multiple rows
* Declare the cursor
C/Exec SQL
C+ Declare c1 Cursor for
C+ Select inumbr, idept, idescr
C+ From invmst
C+ Where idept = :DeptNumbr
C/End-Exec

* Open the cursor


C/Exec SQL
C+ Open c1
C/End-Exec
* Fetch rows into host fields until
* SQLStt indicates an error
C dow SQLStt = '00000'

C/Exec SQL

C+ fetch next
C+ from c1
C+ into :inumbr, :idept, :idescr

C/End-Exec
* If the status field is OK

* then process this record.

C if SQLStt = '00000'
C exsr ProcessRecord
C endif

* fetch next row


C enddo
* Close the cursor
C/Exec SQL
C+ close c1
C/End-Exec
Calculated Column Values
Select skunum,
upper(idesc),
qtyoh,
avgcst,
qtyoh * avgcst
from invmst
Using a literal column value
insert into taxdpt
select 123, txdept, txflag
from taxdpt
where txstrn = 734
Using row functions in RPG

C eval JDEdate = 102003

C/Exec SQL

C+ set :ISOdate = DATE(DIGITS(DECIMAL(


C+ :JDEdate+1900000,7,0)))
C/End-Exec
Counting the records in a file

select count(*) from invmst

COUNT ( * )
530,610
Department Subtotals
select idept, count(*)
from invmst
group by idept
order by idept

Department COUNT ( * )
1 30,610
2 26,416
3 74,297
5 23,272
6 40,789
Limiting groups with Having

select idept, count(*)


from invmst
group by idept
having count(*) < 50000
order by idept
Selecting records with a subquery

select cunmbr, cuname


from cusmst
where cuytds > (select avg(cuytds)
from cusmst)
Using a correlated subquery

select cunmbr, cuname


from cusmst as c1
where cuytds > (select avg(cuytds)
from cusmst as c2
where c2.custat =
c1.custat)
The In Predicate

Select * from orddtl


where odsku in (select skunbr
from invmst
where idept = 4)
The Exists Predicate
update cuswrk
set curnme =
(select rpname from slsrep
where cuswrk.repnum =
slsrep.repnum)

where exists (select * from slsrep


where cuswrk.repnum =
slsrep.repnum)
Old Join Syntax
select ordhdr.ohornm,
ordhdr.ohcusn,
cusmst.cuname
from ordhdr, cusmst
where ordhdr.ohstat = ‘O’ and
ordhdr.ohcusn = cusmst.cusnmr
New Join Syntax
select ordhdr.ohornm,
ordhdr.ohcusn,
cusmst.cuname
from ordhdr join cusmst
on ordhdr.ohcusn = cusmst.cusnmr
where ordhdr.ohstat = ‘O’
Join Types
• JOIN or INNER JOIN
• Each row in table to left will be joined with one or
more rows in table to right using the join
condition. Any rows in left table that do not have
corresponding rows in the right table will not be in
the result table. Produces the same results as
separating the tables with a comma and using the
where clause to provide the join condition.
Join Types
• LEFT JOIN or LEFT OUTER JOIN
• Each row in table to left will be joined with one or
more rows in table to right using the join
condition. Any rows in left table that do not have
corresponding rows in the right table will be in the
result table with null values for fields from the
right table.
Join Types
• EXCEPTION JOIN
• Only rows in the left table that have no
corresponding row in the right table using the join
condition will be in the result table. Nulls are
returned for any fields from the right table.
Join Types
• CROSS JOIN
• Each row in the left table will be joined with each
row in the right table. The ON condition is not
used for a cross join.
Percent of Total Report

IVDEPT IVSKU IVONHD IVACST


3 928564 126 .95
1 214843 26 3.45
1 245279 78 2.33
3 983521 39 3.78
6 394856 132 .28
Department Totals Select
Statement

select ivdept , sum(ivonhd*ivacst)


from invmst
group by ivdept
with depttotals (deptnum, depttot)
as (select ivdept,sum(ivonhd*ivacst)
from invmst
group by ivdept)

select i.ivdept, i.ivsku, i.ivonhd,


i.ivacst, d.depttot
from invmst as i
join depttotals as d
on i.ivdept=d.deptnum
order by 1,2
The final result table

IVDEPT IVSKU IVONH IVACST DEPTTOT


1 214843 26 3.45 271.44
1 245279 78 2.33 271.44
3 928564 126 .95 267.12
3 983521 39 3.78 267.12
6 394856 132 .28 36.96
The Union Clause
Select ‘Test’, inumbr, ivohnd
from testlib/invmst

Union

Select ‘Live’, inumbr, ivohnd


from livelib/invmst
Inventory File
IVDEPT IVSKU IVONHD IVACST
3 928564 126 .95
1 214843 26 3.45
1 245279 78 2.33
3 983521 39 3.78
6 394856 132 .28
Detail Lines
select 'd', ivdept,
ivsku,(ivonhd*ivacst)
from invmst
 
TYPE IVDEPT IVSKU AMOUNT
d 3 928564 119.70
d 1 214843 89.70
d 1 245279 181.74
d 3 983521 147.42
d 6 394856 36.96
Department Totals
select 's',ivdept,0,
sum(ivonhd*ivacst)
from invmst
group by 's',ivdept
 
TYPE IVDEPT IVSKU SUM
s 1 0 271.44
s 3 0 267.12
s 6 0 36.96
Grand Total

select 't',999,0,
sum(ivonhd*ivacst)
from invmst
 
Type IVDEPT IVSKU SUM
t 999 0 575.52
The complete select statement
select 'd', ivdept, ivsku,(ivonhd*ivacst)
from invmst
union all

select 's',ivdept,0,sum(ivonhd*ivacst)
from invmst
group by 's',ivdept

union all

select 't',999,0,sum(ivonhd*ivacst)
from invmst
order by 2,1,3
The final result table
TYPE IVDEPT IVSKU ONHAND
d 1 214843 89.70
d 1 245279 181.74
s 1 0 271.44
d 3 928564 119.70
d 3 983521 147.42
s 3 0 267.12
d 6 394856 36.96
s 6 0 36.96
t 999 0 575.52
Dynamic SQL

Price Download History Inquiry Screen

Sku Number ..........


Store ..........
Price Event ..........
From Date ..........
To Date ..........

The Sku number must be entered.


All other fields are optional.
* basic statement and inumbr search clause

C eval sqlstm = 'select * from sdipluh where +


C plusku = ' +
C %trim(%editc(inumbr:'3'))
* store

C if store <> 0
C eval sqlstm=sqlstm+' and plustr=' +
C %trim(%editc(store:'3'))
C endif
* event number

C if event <> 0
C eval sqlstm=sqlstm+' and pluevt=' +
C %trim(%editc(event:'3'))
C endif
* dates

C if datef <> 0
C eval sqlstm=sqlstm+
C ' and plucur between ' +
C %trim(%editc(datef:'3')) +
C ' and ' +
C %trim(%editc(datet:'3'))
C endif
* add order by clause

C eval sqlstm=sqlstm+' order by 1,2,3'


The Prepare Statement
* prepare the cursor

C/Exec SQL

C+ Prepare dynsqlstm

C+ from :sqlstm

C/End-Exec

You might also like