0% found this document useful (0 votes)
18 views9 pages

DB2 Command Examples and Queries Guide

The document contains a comprehensive list of DB2 commands for various operations, including displaying current locks, calculating maximum values, determining the day of the week, and managing timestamps. It also includes SQL queries for retrieving table details, primary keys, unique indexes, and privileges granted to users. Additionally, it provides methods for calculating differences between timestamps and handling various data types in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

DB2 Command Examples and Queries Guide

The document contains a comprehensive list of DB2 commands for various operations, including displaying current locks, calculating maximum values, determining the day of the week, and managing timestamps. It also includes SQL queries for retrieving table details, primary keys, unique indexes, and privileges granted to users. Additionally, it provides methods for calculating differences between timestamps and handling various data types in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

DB2 commands:

 DB2 Run Command to diplay Current Locks on a Table:

-DIS DB(ARLACRSP) SPACENAM(SCRSCASE) USE LOCKS

 Query to Calculate the Max Value:

-- 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 - WEDNESDAY
4 - THURSDAY
5 - FRIDAY
6 - SATURDAY

 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 = CURRENT TIMESTAMP

 To get current date

SET :WS-CURRENT-DATE = 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

SELECT
POL.DPO_POL_NO
, CKY.MAJOR_CLASS_CD
INTO :WS-POLICY-NUMBER
, :WS-MAJOR-CLASS-CODE
FROM CRS_DED_POLICY POL
, VCOVG_KEY CKY
WHERE
CKY.POL_NO = INTEGER(DECIMAL(POL.DPO_POL_NO))

Here DPO_POL_NO is CHAR(15) and POL_NO is INTEGER.

 To concatenate two Columns

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

SELECT NAME
FROM [Link]
WHERE TBCREATOR = <TABLE CREATOR NAME>
AND TBNAME = <TABLE NAME>
AND KEYSEQ > 0

(OR)

SELECT [Link],
[Link],
[Link],

FROM [Link] A,
[Link] B

WHERE [Link] = [Link]


AND [Link] = [Link]
AND [Link] = [Link]
AND [Link] = <TAB NAME>
AND [Link] = <TAB CREATOR>
AND [Link] = ‘P’

 To get Unique index of a table

SELECT [Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]

FROM [Link] A,
[Link] B

WHERE [Link] = [Link]


AND [Link] = [Link]
AND [Link] = [Link]
AND [Link] = <TAB NAME>
AND [Link] = <TAB CREATOR>
AND [Link] = 'U'

ORDER BY [Link] DESC


, [Link]

[Link]

NAME – Index name


CREATOR – Index Creator
TBNAME – Table name
TBCREATOR – Table Creator
UNIQUERULE = P – For Primary Keys
= U – For Unique Indexes
= D – For Duplicate Indexes
COLCOUNT – No of Columns for that Index

INDEXTYPE - Type of Index (1 or 2)


CREATEDTS - Created TS
ALTEREDTS - Altered TS

[Link]

IXNAME - Index name


IXCREATOR - Index creator
COLNAME - Column name
COLSEQ - Column Sequence in Index
ORDERING - Order of Index

 To Get the affected program names because of a table

SELECT GRANTEE "PROGRAM",


TTNAME "TABLE NAME",
SELECTAUTH "SELECT",
INSERTAUTH "INSERT",
UPDATEAUTH "UPDATE",
DELETEAUTH "DELETE"
FROM [Link]
WHERE GRANTEETYPE= 'P'
AND TCREATOR= <TABLE CREATOR NAME>
AND TTNAME = <TABLE NAME>

ORDER BY PROGRAM

 To Get Table or View or Alias Details

SELECT NAME "Table or Alias or View"


,TYPE
,CREATOR
,COLCOUNT
,TBCREATOR
,TBNAME
,KEYCOLUMNS
,CREATEDTS
,ALTEREDTS
,CREATEDBY

FROM [Link]
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 [Link]
WHERE NAME = <VIEW NAME>

SEQNO – IF THE VIEW DEFINITION IS MORE THAN ONE LINE


TEXT - VIEW DEFINITION

 To Get Image copied table names

SELECT [Link]
,[Link]
,[Link]
FROM [Link] A
WHERE [Link] = <table creator>
AND [Link] = 'T'
AND [Link] = <Database name>
AND EXISTS ( SELECT 'X' FROM [Link] B
WHERE [Link] = [Link]
AND [Link] = [Link] )

 To Get the Plan names which are Grant to PUBLIC or some User

SELECT DISTINCT
[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]

FROM [Link] A
,[Link] B

WHERE [Link] = <Grantor name>


AND [Link] = 'PUBLIC' or <User id>
AND [Link] = [Link]
AND (([Link] NOT IN (' '))
OR ([Link] NOT IN (' '))
OR ([Link] 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

SELECT [Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]
,[Link]

FROM [Link] A
,[Link] B

WHERE [Link] = 'ARLACRSA'


AND [Link] = [Link]
AND [Link] = [Link]
AND [Link] LIKE ‘%BR%’
WITH UR

 To Get List of tables Privileges granted to an Auth id

SELECT DISTINCT
TCREATOR
,TTNAME
,ALTERAUTH
,DELETEAUTH
,INDEXAUTH
,INSERTAUTH
,SELECTAUTH
,UPDATEAUTH

FROM [Link]

WHERE GRANTEE IN (<user id>,'PUBLIC', 'PUBLIC*')


AND GRANTEETYPE = ' '

 To Get the affected Plan names if an Index is Dropped

SELECT BNAME
,BCREATOR
,BTYPE
,DNAME

FROM [Link]

WHERE BTYPE = ‘I’


AND BNAME = <Index name to be Dropped>

[Link]
This table records the dependencies of plans on tables, views, aliases,
synonyms, table spaces, indexes, functions, and stored procedures.

BNAME - The name of an object the plan depends on.

BCREATOR - If BNAME is a table space, its database.


Otherwise, the authorization ID of the owner of BNAME.

BTYPE - Type of object identified by BNAME:

A Alias
F User-defined function or cast function
I Index
O Stored procedure
P Partitioned table space
R Table space
S Synonym
T Table
V View

DNAME - Name of the plan.

 To Get the affected Package names if an Index is Dropped

SELECT BNAME
,BQUALIFIER
,BTYPE
,DLOCATION
,DCOLLID
,DNAME
,DCONTOKEN
,DOWNER
,DTYPE

FROM [Link]

WHERE BTYPE = ‘I’


AND BNAME = <Plan name>

[Link]

This table records the dependencies of packages on tables, views, aliases,


synonyms, table spaces, indexes, functions, and stored procedures.

BNAME - The name of an object that a package depends on.

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 Alias
F User-defined function or cast function
I Index
O Stored procedure
P Partitioned table space
R Table space
S Synonym
T Table
V View

BCREATOR - If BNAME is a table space, its database.


Otherwise, the authorization ID of the owner of BNAME.

DLOCATION - Always contains blanks

DCOLLID - Name of the package collection.

DNAME - Name of the package.

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

DOWNER - Owner of the package.

DTYPE - Type of package:


T Trigger package
blank Not a trigger package

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

You might also like