The DB2 tutorial is: use shell to extract and update DB2 data. The shell written for work needs to process the DB2 database Program Use shell to extract and process DB2 data.
# SQL Definition
SQL = "select AAA, BBB, CCC from mytbl1"
# Execute SQL
Sdata = 'db2 "$ SQL "'
# Return Value Judgment
If [$? -Ne 0]
Then
# Display error messages returned by DB2
Echo "$ sdata"
Exit 1
Fi
# Process the obtained data.
Echo "$ sdata" | sed-e'4,/^ $ /! D;/^ $/d' |
While read aaa bbb ccc
Do
Echo "AAA is $ AAA, BBB is $ BBB, CCC is $ CCC"
Done
# Retrieve data pieces
Echo "$ sdata" | sed-n-e '/^ $/{1, 3D; N; s/[^ 0-9] * \ ([0-9] * \) [^ 0-9] */\ 1/; P;} '| read CNT
Echo "The Count of selected data is $ CNT ."
Exit 0★Update DB2 data and obtain the update result.
SQL = "Update mytbl1 set AAA = '000000', BBB = '05 ', CCC = '12 '"
# Execute SQL
Sdata = 'db2-a "$ SQL "'
# Obtain sqlcode
Echo "$ sdata" | sed-n-e's/^. * sqlcode: \ ([-, 0-9] [0-9] * \). */\ 1/P' | read sqlcode
Echo "sqlcode is $ sqlcode ."
# Obtain sqlstate
Echo "$ sdata" | sed-n-e's/^. * sqlstate: \ ([-, 0-9] [0-9] * \). */\ 1/P' | read sqlstate
Echo "sqlstate is $ sqlstate ."
# Obtain the number of updates (the third value of sqlerrd)
Echo "$ sdata" | sed-n-e '/sqlerrd/S/^. * (3) \ ([-, 0-9] [0-9] * \). */\ 1/P' | read updcnt
Echo "updated data's count is $ updcnt ."
# Obtain the fifth value of sqlerrd
Echo "$ sdata" | sed-n-e '/sqlerrd/{n; S/^. * (5) \ ([-, 0-9] [0-9] * \). */\ 1/; P;} '| read sqlerrd5
echo "sqlerrd (5) is $ sqlerrd5."