使用 Shell 从 DB2 数据库获取前 5 行数据并打印
以下是使用 Shell 从 DB2 获取 table 前五条数据,并逐行读取每一行数据打印出来的示例代码:
#!/bin/bash
# DB2 connection parameters
DBNAME='your_db_name'
USER='your_username'
PASS='your_password'
HOST='your_host_name'
PORT='your_port_number'
# SQL query to fetch top 5 rows from a table
QUERY='SELECT * FROM your_table_name FETCH FIRST 5 ROWS ONLY'
# Connect to DB2 and execute the query
db2 connect to $DBNAME user $USER using $PASS
db2 +p -x 'CONNECT TO $DBNAME USER $USER USING $PASS; $QUERY' > output.txt
# Read each line from the output file and print it
while read line; do
echo $line
done < output.txt
# Disconnect from DB2
db2 connect reset
db2 terminate
请注意,您需要替换示例代码中的数据库连接参数,表名和查询以适应您的环境。此外,您还需要安装 DB2 客户端以使此脚本正常工作。
原文地址: https://www.cveoy.top/t/topic/nESg 著作权归作者所有。请勿转载和采集!