SQL/400 Tips and Tricks: Bill Fortenberry Books A Million
SQL/400 Tips and Tricks: Bill Fortenberry Books A Million
Bill Fortenberry
Books A Million
Why?
•Industry standard / Cross platform
•Interactive SQL
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
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
C/Exec SQL
C+ fetch next
C+ from c1
C+ into :inumbr, :idept, :idescr
C/End-Exec
* If the status field is OK
C if SQLStt = '00000'
C exsr ProcessRecord
C endif
C/Exec SQL
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
Union
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
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/Exec SQL
C+ Prepare dynsqlstm
C+ from :sqlstm
C/End-Exec