1. 程式人生 > >通過shell指令碼呼叫儲存過程

通過shell指令碼呼叫儲存過程

shell指令碼

#/bin/sh
# 獲取資料庫密碼,根據實際情況,需要修改
export_pgpasswd()
{
    if [  -f  /usr/local/svconfig/server/conf/imos.cfg  ]
    then
        dbpasswd=`cat /usr/local/svconfig/server/conf/imos.cfg | grep DBPassword |  awk -F "=" '{ print $2}'`
        dbpasswd=`/usr/local/bin/unipd 1 ${dbpasswd}`
        if [ ""x = "$dbpasswd"x ]
        then
            echo " `date +"%Y-%m-%d %H:%M:%S"` --> error: do passwd error, DBPassword not in imos.cfg" >> ${LOGFILE} 2>&1
            exit 1
        fi
        export PGPASSWORD=$dbpasswd
    else
        export PGPASSWORD=passwd
    fi

}

export_pgpasswd
WORK_PATH=`pwd`
/home/postgres/pgsql/bin/psql -U postgres -f ${WORK_PATH}/test.sql

被呼叫的SQL指令碼

test.sql

create or replace function fun_test() returns integer as 
$BODY$
begin 
   xxx(具體實現程式碼)
   return 0;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

select * from fun_test();