The db2 tutorial is: use shell to extract and update db2 data. The shell that is 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 ."