meet200301

Embed Size (px)

Citation preview

  • 8/8/2019 meet200301

    1/56

    SQL/400

    Tips and Tricks

    Bill FortenberryBooks A Million

  • 8/8/2019 meet200301

    2/56

    Why?

    Industry standard / Cross platform

    Learn four statements / Big payback

    Interactive SQL

    Embedded SQL / Dynamic SQL

  • 8/8/2019 meet200301

    3/56

    CUSTOMER LISTING

    STATE: AL

    BY DESC YTD ORDERS

    CUST NAME ORDERS SALES REP------------ ------ --------------

    ABC Corp. 75 Freddy Smith

    Smith & Smith 63 Joe JacksonDEF Company 55 < Not Found >

    E&F Inc. 40 Freddy Smith

  • 8/8/2019 meet200301

    4/56

    Select cusmst.cunamecusmst.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

  • 8/8/2019 meet200301

    5/56

  • 8/8/2019 meet200301

    6/56

    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 = :StateIDOrder by 2 desc, 1

  • 8/8/2019 meet200301

    7/56

    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

  • 8/8/2019 meet200301

    8/56

    Select * from invmst

    Select inumbr, idept, idesc

    from invmst

    Select inumbr, idept, idesc

    from invmst

    where istat = A

  • 8/8/2019 meet200301

    9/56

    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

  • 8/8/2019 meet200301

    10/56

    Delete from invmst

    Delete from invmstwhere idept = 12

  • 8/8/2019 meet200301

    11/56

    Insert into cumast

    (cunbr, cuname, cucity)

    Values

    (678987, Atlas Inc., Birmingham)

  • 8/8/2019 meet200301

    12/56

    Insert into cumast

    (cunbr, cuname, cucity)

    select cwidno, cwcnam, cutown

    from newcuswhere cutown = Atlanta

  • 8/8/2019 meet200301

    13/56

    Update cumast

    set cuname = Acme Builders,cudisc = cudisc + .01

    where custid = 123456

    Update customer

    set row = (Atlas, 13246,

    Meridian)

    where custid = 2354

  • 8/8/2019 meet200301

    14/56

    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.

  • 8/8/2019 meet200301

    15/56

    Convert Select to Delete1. Press F9 to retrieve the select command.

    Select * from ordhdr where ohornm = 123456

    2. Type DELETE over SELEC

    T *.Delete from ordhdr where ohornm = 123456

    3. Press enter.

  • 8/8/2019 meet200301

    16/56

    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 theinsert command.

    insert into mylib/orddtl

    Select * from orddtl where odornm = 123456

    3. Press enter.

  • 8/8/2019 meet200301

    17/56

    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 splitthe 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

  • 8/8/2019 meet200301

    18/56

    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

  • 8/8/2019 meet200301

    19/56

    RPG code to delete recordsC OrderNum chain rordhdr

    C if %found

    C delete rordhdr

    C endif

    CC OrderNum setll rorddtl

    C OrderNum reade rorddtl

    C dow not %eof

    C delete rorddtlC OrderNum reade rorddtl

    C enddo

  • 8/8/2019 meet200301

    20/56

    SQL code to delete records

    C/Exec SQLC+ Delete from ordhdr

    C+ where ohornm = :OrderNum

    C/End-Exec

    C/Exec SQL

    C+ Delete from orddtl

    C+ where odornm = :OrderNum

    C/End-Exec

  • 8/8/2019 meet200301

    21/56

    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

  • 8/8/2019 meet200301

    22/56

    Selecting multiple rows* Declare the cursor

    C/Exec SQL

    C+ Declare c1 Cursor for

    C+ Select inumbr, idept, idescr

    C+ From invmstC+ Where idept = :DeptNumbr

    C/End-Exec

    * Open the cursorC/Exec SQL

    C+ Open c1

    C/End-Exec

  • 8/8/2019 meet200301

    23/56

    * 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, :idescrC/End-Exec

  • 8/8/2019 meet200301

    24/56

    * If the status field is OK

    * then process this record.

    C if SQLStt = '00000'

    C exsr ProcessRecord

    C endif

    * fetch next row

    C enddo

  • 8/8/2019 meet200301

    25/56

    * Close the cursor

    C/Exec SQL

    C+ close c1C/End-Exec

  • 8/8/2019 meet200301

    26/56

    Calculated

    Column Values

    Select skunum,

    upper(idesc),

    qtyoh,

    avgcst,

    qtyoh * avgcst

    from invmst

  • 8/8/2019 meet200301

    27/56

    Using a literal column valueinsert into taxdpt

    select 123, txdept, txflag

    from taxdpt

    where txstrn = 734

  • 8/8/2019 meet200301

    28/56

    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

  • 8/8/2019 meet200301

    29/56

    Counting the records in a file

    select count(*) from invmst

    COUNT ( * )

    530,610

  • 8/8/2019 meet200301

    30/56

    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

  • 8/8/2019 meet200301

    31/56

    Limiting groups with Having

    select idept, count(*)

    from invmstgroup by idept

    having count(*) < 50000

    order by idept

  • 8/8/2019 meet200301

    32/56

    Selecting records with a subquery

    select cunmbr, cuname

    from cusmstwhere cuytds > (select avg(cuytds)

    from cusmst)

  • 8/8/2019 meet200301

    33/56

    Using a correlated subqueryselect cunmbr, cuname

    from cusmst as c1

    where cuytds > (select avg(cuytds)

    from cusmst as c2

    where c2.custat =

    c1.custat)

  • 8/8/2019 meet200301

    34/56

    TheIn Predicate

    Select * from orddtl

    where odsku in (select skunbrfrom invmst

    where idept = 4)

  • 8/8/2019 meet200301

    35/56

    The Exists Predicateupdate cuswrk

    set curnme =

    (select rpname from slsrep

    where cuswrk.repnum =slsrep.repnum)

    where exists (select * from slsrepwhere cuswrk.repnum =

    slsrep.repnum)

  • 8/8/2019 meet200301

    36/56

    Old Join Syntaxselect ordhdr.ohornm,

    ordhdr.ohcusn,

    cusmst.cunamefrom ordhdr, cusmst

    where ordhdr.ohstat = O and

    ordhdr.ohcusn = cusmst.cusnmr

  • 8/8/2019 meet200301

    37/56

    N

    ew Join Syntaxselect ordhdr.ohornm,

    ordhdr.ohcusn,

    cusmst.cunamefrom ordhdr join cusmst

    on ordhdr.ohcusn = cusmst.cusnmr

    where ordhdr.ohstat = O

  • 8/8/2019 meet200301

    38/56

    Join Types JOIN orINNER JOIN

    Each row in table to left will be joined with one or

    more rows in table to right using the joincondition. 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 thewhere clause to provide the join condition.

  • 8/8/2019 meet200301

    39/56

    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 joincondition. 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.

  • 8/8/2019 meet200301

    40/56

    Join Types EXCEPTION JOIN

    Only rows in the left table that have no

    corresponding row in the right table using the joincondition will be in the result table. Nulls are

    returned for any fields from the right table.

  • 8/8/2019 meet200301

    41/56

    Join Types CROSS JOIN

    Each row in the left table will be joined with each

    row in the right table. The ON condition is notused for a cross join.

  • 8/8/2019 meet200301

    42/56

    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

  • 8/8/2019 meet200301

    43/56

    Department Totals Select

    Statement

    select ivdept , sum(ivonhd*ivacst)

    from invmst

    group by ivdept

  • 8/8/2019 meet200301

    44/56

    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.deptnumorder by 1,2

  • 8/8/2019 meet200301

    45/56

    The final result table

    IVDEPT IVSKU IVONH IVACST DEPTTOT

    1 214843 26 3.45 271.44

    1 245279 78 2.33 271.443 928564 126 .95 267.12

    3 983521 39 3.78 267.12

    6 394856 132 .28 36.96

  • 8/8/2019 meet200301

    46/56

    The Union Clause

    Select Test, inumbr, ivohnd

    from testlib/invmst

    Union

    Select Live, inumbr, ivohnd

    from livelib/invmst

  • 8/8/2019 meet200301

    47/56

    I

    nventory FileIVDEPT 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

  • 8/8/2019 meet200301

    48/56

    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.74d 3 983521 147.42

    d 6 394856 36.96

  • 8/8/2019 meet200301

    49/56

    Department Totals

    select 's',ivdept,0,

    sum(ivonhd*ivacst)

    from invmst

    group by 's',ivdept

    TYPE IVDEPT IVSKU SUM

    s 1 0 271.44s 3 0 267.12

    s 6 0 36.96

  • 8/8/2019 meet200301

    50/56

    Grand Total

    select 't',999,0,

    sum(ivonhd*ivacst)from invmst

    Type IVDEPT IVSKU SUM

    t 999 0 575.52

  • 8/8/2019 meet200301

    51/56

    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

  • 8/8/2019 meet200301

    52/56

    The final result tableTYPE 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.96s 6 0 36.96

    t 999 0 575.52

  • 8/8/2019 meet200301

    53/56

    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.

  • 8/8/2019 meet200301

    54/56

    * basic statement and inumbr search clause

    C eval sqlstm = 'select * from sdipluh where +

    C plusku = ' +

    C %trim(%editc(inumbr:'3'))

    * storeC if store 0

    C eval sqlstm=sqlstm+' and plustr=' +

    C %trim(%editc(store:'3'))

    C endif

  • 8/8/2019 meet200301

    55/56

    * 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'

  • 8/8/2019 meet200301

    56/56

    The Prepare Statement

    * prepare the cursor

    C/Exec SQL

    C+ Prepare dynsqlstmC+ from :sqlstm

    C/End-Exec