Professional Documents
Culture Documents
DB2 SQL - Important Queries
DB2 SQL - Important Queries
DB2 SQL - Important Queries
-- FIRST MAX -- ========== SELECT MAX(REFERRAL_AM) FROM TRCVRY_RFRL_SYMBOL ; -- SECOND MAX -- ========== SELECT MAX(REFERRAL_AM) FROM TRCVRY_RFRL_SYMBOL WHERE REFERRAL_AM < (SELECT MAX(REFERRAL_AM) FROM TRCVRY_RFRL_SYMBOL) ; -- N'TH MAX WITHOUT DUPLICATES -- =========================== SELECT REFERRAL_AM FROM TRCVRY_RFRL_SYMBOL A WHERE 1 = ( SELECT COUNT(*) FROM TRCVRY_RFRL_SYMBOL B WHERE B.REFERRAL_AM >= A.REFERRAL_AM ) ; -- N'TH MAX WITH DUPLICATES -- ======================== SELECT DISTINCT REFERRAL_AM FROM TRCVRY_RFRL_SYMBOL A WHERE 1 = ( SELECT COUNT(DISTINCT B.REFERRAL_AM) FROM TRCVRY_RFRL_SYMBOL B WHERE B.REFERRAL_AM >= A.REFERRAL_AM) ; To calculate day of week: SELECT DAYS(DATE(:WS-SQL-DT1))-(DAYS(DATE(:WS-SQL-DT1))/7) * 7 INTO :WS-DAY-OF-WEEK FROM SYSIBM.SYSDUMMY1 IF WS-DAY-OF-WEEK IS 0 - SUNDAY 1 - MONDAY 2 - TUESDAY
3 4 5 6 Addition of days
SELECT DATE(:WS-SQL-DT1) + 3 DAY INTO :WS-SQL-DT2 FROM SYSIBM.SYSDUMMY1 To get current timestamp SET :WS-CURRENT-TIMESTAMP To get current date SET :WS-CURRENT-DATE Usage of Value Clause SELECT VALUE(MAX(IPL_UPDATE_TS), CURRENT TIMESTAMP) INTO :PROD-IPL-UPDATE-TS FROM VCLMS_PROD WHERE POL_NO = :PROD-POL-NO AND MODULE = :PROD-MODULE AND ANN_STMT_CO = :PROD-ANN-STMT-CO To compare CHAR and INTEGER POL.DPO_POL_NO , CKY.MAJOR_CLASS_CD :WS-POLICY-NUMBER , :WS-MAJOR-CLASS-CODE CRS_DED_POLICY POL , VCOVG_KEY CKY CKY.POL_NO = INTEGER(DECIMAL(POL.DPO_POL_NO)) = CURRENT DATE = CURRENT TIMESTAMP
SELECT STF_FIRST_NAME || STF_LAST_NAME INTO :WS-USER-NM FROM CRS_STAFF WHERE STF_CODE = :WS-I-USER-ID
To Get the Primary Keys for the given table name NAME SYSIBM.SYSCOLUMNS TBCREATOR = <TABLE CREATOR NAME> TBNAME = <TABLE NAME> KEYSEQ > 0 (OR)
SELECT A.COLNAME, A.COLSEQ, A.ORDERING, FROM SYSIBM.SYSKEYS A, SYSIBM.SYSINDEXES B WHERE AND AND AND AND AND A.IXNAME = B.NAME A.IXCREATOR = B.CREATOR A.IXCREATOR = B.TBCREATOR B.TBNAME = <TAB NAME> B.TBCREATOR = <TAB CREATOR> B.UNIQUERULE = P
To get Unique index of a table A.COLNAME ,A.COLSEQ ,A.ORDERING ,A.IXNAME ,B.COLCOUNT ,B.CREATEDTS ,B.ALTEREDTS ,B.UNIQUERULE
SELECT
FROM SYSIBM.SYSKEYS A, SYSIBM.SYSINDEXES B WHERE AND AND AND AND AND A.IXNAME = B.NAME A.IXCREATOR = B.CREATOR A.IXCREATOR = B.TBCREATOR B.TBNAME = <TAB NAME> B.TBCREATOR = <TAB CREATOR> B.UNIQUERULE = 'U' B.ALTEREDTS DESC , A.COLSEQ
ORDER BY
SYSIBM.SYSINDEXES NAME CREATOR TBNAME Index name Index Creator Table name
TBCREATOR UNIQUERULE
= = =
Table Creator P For Primary Keys U For Unique Indexes D For Duplicate Indexes
SYSIBM.SYSKEYS IXNAME IXCREATOR COLNAME COLSEQ ORDERING Index name Index creator Column name Column Sequence in Index Order of Index
SELECT GRANTEE "PROGRAM", TTNAME "TABLE NAME", SELECTAUTH "SELECT", INSERTAUTH "INSERT", UPDATEAUTH "UPDATE", DELETEAUTH "DELETE" FROM SYSIBM.SYSTABAUTH WHERE GRANTEETYPE= 'P' AND TCREATOR= <TABLE CREATOR NAME> AND TTNAME = <TABLE NAME> ORDER BY PROGRAM To Get Table or View or Alias Details NAME "Table or Alias or View" ,TYPE ,CREATOR ,COLCOUNT ,TBCREATOR ,TBNAME ,KEYCOLUMNS ,CREATEDTS ,ALTEREDTS ,CREATEDBY
SELECT
FROM SYSIBM.SYSTABLES WHERE NAME = <TABLE or View or Alias Name> AND CREATOR = < TABLE or View or Alias Creator> TYPE = T (Table) or A (Alias) V (View) G (Temporary Table) COLCOUNT NO OF COLUMNS - only for Tables AND Views (0 for Alias)
RECLENGTH Record length only for Tables (0 for Alias and Views) TBCREATOR only FOR ALIAS ACTUAL TABLE CREATOR TBNAME only FOR ALIAS ACTUAL TABLE NAME KEYCOLUMNS No of Primary Keys To Get View Definition
SELECT
NAME ,CREATOR ,SEQNO ,TEXT FROM SYSIBM.SYSVIEWS WHERE NAME = <VIEW NAME>
SEQNO IF THE VIEW DEFINITION IS MORE THAN ONE LINE TEXT - VIEW DEFINITION To Get Image copied table names A.NAME ,A.CREATOR ,A.TSNAME SYSIBM.SYSTABLES A A.CREATOR = <table creator> A.TYPE = 'T' A.DBNAME = <Database name> EXISTS ( SELECT 'X' FROM SYSIBM.SYSCOPY B WHERE A.TSNAME = B.TSNAME AND A.DBNAME = B.DBNAME )
To Get the Plan names which are Grant to PUBLIC or some User
SELECT DISTINCT A.NAME ,B.SCREATOR ,B.STNAME ,B.DELETEAUTH ,B.INSERTAUTH ,B.SELECTAUTH ,B.UPDATEAUTH ,A.BINDAUTH ,A.EXECUTEAUTH FROM WHERE AND AND SYSIBM.SYSPLANAUTH A ,SYSIBM.SYSTABAUTH B A.GRANTOR = <Grantor name> A.GRANTEE = 'PUBLIC' or <User id> A.NAME = B.GRANTEE
AND OR OR
((B.UPDATEAUTH NOT IN (' ')) (B.INSERTAUTH NOT IN (' ')) (B.DELETEAUTH NOT IN (' ')))
UPDATEAUTH = Y The Grantee can do UPDATE only UPDATEAUTH = G The Grantee can give Grant Permission for UPDATE To Get Column details for all the tables A.NAME ,A.CREATOR ,A.TYPE ,A.TBCREATOR ,A.TBNAME ,B.NAME ,B.COLTYPE ,B.LENGTH ,B.SCALE SYSIBM.SYSTABLES A ,SYSIBM.SYSCOLUMNS B = 'ARLACRSA' = B.TBCREATOR = B.TBNAME LIKE %BR%
SELECT
FROM
To Get List of tables Privileges granted to an Auth id DISTINCT TCREATOR ,TTNAME ,ALTERAUTH ,DELETEAUTH ,INDEXAUTH ,INSERTAUTH ,SELECTAUTH ,UPDATEAUTH
SELECT
FROM SYSIBM.SYSTABAUTH WHERE GRANTEE IN (<user id>,'PUBLIC', 'PUBLIC*') AND GRANTEETYPE = ' ' To Get the affected Plan names if an Index is Dropped BNAME ,BCREATOR ,BTYPE ,DNAME
SELECT
FROM SYSIBM.SYSPLANDEP WHERE BTYPE = I AND BNAME = <Index name to be Dropped> SYSIBM.SYSPLANDEP This table records the dependencies of plans on tables, views, aliases, synonyms, table spaces, indexes, functions, and stored procedures. BNAME BCREATOR BTYPE - The name of an object the plan depends on. - If BNAME is a table space, its database. Otherwise, the authorization ID of the owner of BNAME. - Type of object identified by BNAME: A F I O P R S T V DNAME Alias User-defined function or cast function Index Stored procedure Partitioned table space Table space Synonym Table View
To Get the affected Package names if an Index is Dropped BNAME ,BQUALIFIER ,BTYPE ,DLOCATION ,DCOLLID ,DNAME ,DCONTOKEN ,DOWNER ,DTYPE
SELECT
FROM SYSIBM.SYSPACKDEP WHERE BTYPE = I AND BNAME = <Plan name> SYSIBM.SYSPACKDEP This table records the dependencies of packages on tables, views, aliases, synonyms, table spaces, indexes, functions, and stored procedures.
BNAME
BQUALIFIER - If BNAME identifies a tablespace, the name of its database. Otherwise, the authorization ID of the owner of BNAME. BTYPE - Type of object identified by BNAME and BQUALIFIER: A F I O P R S T V BCREATOR DLOCATION DCOLLID DNAME DCONTOKEN Alias User-defined function or cast function Index Stored procedure Partitioned table space Table space Synonym Table View
- If BNAME is a table space, its database. Otherwise, the authorization ID of the owner of BNAME. - Always contains blanks - Name of the package collection. - Name of the package.
- Consistency token for the package. This is either: * The "level" as specified by the LEVEL option when the package's program was precompiled * The timestamp indicating when the package's program was precompiled, in an internal format. - Owner of the package. T blank - Type of package: Trigger package Not a trigger package
DOWNER DTYPE
Calculate the Difference Between Timestamps PROBLEM: You need to compute the difference, in number of seconds, between the specified timestamps TS1 (start time) and TS2 (end time). SOLUTION: The SQL statement has two parts. * In the first part, you subtract the start time from the end time, deriving the number of days between the timestamps. You then multiply the number of days by 86400, the seconds in a day, to convert the days to the number of seconds. SELECT (DAYS (TS2) - DAYS (TS1)) * 86400 * In the second part, you derive the additional time (hours, minutes, and seconds, list in hhmmss format) between the timestamps and convert
the time to the number of seconds. First you need to extract and convert the hours to seconds: SELECT (HOUR (TIME (TS2)) - HOURS (TIME TS1))) * 3600 Next you need to extract and convert the minutes to seconds: SELECT (MINUTE (TIME (TS2)) - MINUTE (TIME (TS1))) * 60 Then you extract the remaining seconds: SELECT SECOND (TIME (TS2)) - SECOND (TIME (TS1)) Lastly, you add all partial results to produce the difference between the two timestamps in total number of seconds. The following SQL call executes all the parts together in one statement to yield the final result: SELECT (((DAYS (TS2) - DAYS (TS1)) * 86400) + ((HOUR (TIME (TS2)) - HOURS (TIME TS1))) * 3600) + ((MINUTE (TIME (TS2)) - MINUTE (TIME (TS1))) * 60) + (SECOND (TIME (TS2)) - SECOND (TIME (TS1)))) FROM <table name>; This TIP provided by Platinum TIPS March - 1995