Example of how shell extracts data from a db2 database

Source: Internet
Author: User

This document describes how to use shell to extract data from a db2 database. The following is an example for your reference.

An example of how to extract and process data from a db2 database using shell is as follows:

# 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 '"

# Run SQLSDATA = '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 ."

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.