如何将变量从postgresql脚本传递到外部脚本

时间:2017-06-21 23:01:38

标签: postgresql psql

如何以正确的方式将变量从postgresql脚本传递到外部脚本?看来,如果我使用\echo '...',但是使用\echo `...`\!,变量插值将会很好用。这里是示例代码行:

-- set the variables
\set DB_SCHEMA     GT1
\set GEO_VAR_EPSG  '32632'
\set GEO_VAR_TMAX  '250'
-- :DBNAME is a local psql variable
-- print via echo '...'
\echo 'CALL mk-exp-position.pl' :DBNAME :GEO_VAR_TMAX :GEO_VAR_EPSG :DB_SCHEMA.exp_position :DB_SCHEMA.exp_transect
-- try to call some external stuff via \echo `...`
\echo `./mk-exp-position.pl :DBNAME :GEO_VAR_TMAX :GEO_VAR_EPSG :DB_SCHEMA.exp_position :DB_SCHEMA.exp_transect`
-- try to call external script via \! ...
\! ./mk-exp-position.pl :DBNAME :GEO_VAR_TMAX :GEO_VAR_EPSG :DB_SCHEMA.exp_position :DB_SCHEMA.exp_transect

perl scriptlet从命令行读取params到某些变量并打印出来:

#!/usr/bin/perl -w
use warnings;
use strict;
use DBI;

# check parameter count, give help and die
if ( $#ARGV < 4) {
 die " USAGE: $0 DB_NAME MAX_DIST EPSG SRC_TAB DST_TAB\n"
}

# read assign the command line parameter array to some variables
my ($DB_NAME, $MAX_DIST, $EPSG, $TAB_IN, $TAB_OUT) = @ARGV;

# print the variables to the stdout
print "#FILL.TABLE $TAB_OUT FROM $TAB_IN IN $DB_NAME PARAM MAX_DIST=$MAX_DIST EPSG=$EPSG\n";

结果\ echo&#39; CALL ......&#39;:

CALL mk-exp-position.pl daisi_2017_06_06 250 32632 GT1.exp_position GT1.exp_transect

结果\ echo` ...`或\! ......:

#FILL.TABLE :DB_SCHEMA.exp_transect FROM :DB_SCHEMA.exp_position IN :DBNAME PARAM MAX_DIST=:GEO_VAR_CMAX EPSG=:GEO_VAR_EPSG

此致

0 个答案:

没有答案