今天编写shell脚本往Netezza数据库写入日志,因为shell脚本是采用UTF8编码,因此使用nzsql命令执行sql时结果会乱码
执行过程如下:
nzsql -u ${DQ_NZ_USER} -pw ${DQ_NZ_PWD} -d ${DQ_NZ_DB} -host ${DQ_NZ_HOST} <<EOF
\set ON_ERROR_STOP
DELETE FROM PDATA.ADMIN.DQ_CHK_RESULT WHERE CHK_DATE = '$CURR_DATE'
AND RULE_SUB_ID = $rule_cls_id;
INSERT INTO PDATA.ADMIN.DQ_CHK_RESULT(CHK_DATE,RULE_SUB_ID,ERR_CODE,ERR_MSG,START_DATE,END_DATE)
VALUES ('$CURR_DATE',$rule_cls_id,'$CHECK_CODE','$CHECK_RESULT','$START_TIME','$END_TIME')
;
EOF
其中 CHECK_RESULT 为中文,乱码。
修改后为
export NZ_ENCODING=utf8
nzsql -u ${DQ_NZ_USER} -pw ${DQ_NZ_PWD} -d ${DQ_NZ_DB} -host ${DQ_NZ_HOST} <<EOF
\set ON_ERROR_STOP
DELETE FROM PDATA.ADMIN.DQ_CHK_RESULT WHERE CHK_DATE = '$CURR_DATE'
AND RULE_SUB_ID = $rule_cls_id;
INSERT INTO PDATA.ADMIN.DQ_CHK_RESULT(CHK_DATE,RULE_SUB_ID,ERR_CODE,ERR_MSG,START_DATE,END_DATE)
VALUES ('$CURR_DATE',$rule_cls_id,'$CHECK_CODE','$CHECK_RESULT','$START_TIME','$END_TIME')
;
EOF