【问题标题】:How to pass a variable from shell script to sql file如何将变量从shell脚本传递到sql文件
【发布时间】:2018-03-02 17:35:42
【问题描述】:

我有一个名为 test.sh 的 shell 文件,它正在调用其他 sql 文件“table.sql”。 'table.sql' 文件将创建一些表,但我想在特定模式 'bird' 中创建表。

sql 文件的内容。

create schema bird; --bird should not be hard coded it should be in variable
set search_path to 'bird';
create table bird.sparrow(id int, name varchar2(20));

shell 文件的内容。

dbname=$1
cnport=$2
schemaname=$3
filename=$4

gsql -d ${dbname} -p ${cnport} -f ${filenam} #[how to give schema name here so that it can be used in table.sql without hardcoding]

我会像这样执行我的 shell 文件

sh test.sh db1 9999 bird table.sql 

【问题讨论】:

    标签: sql linux postgresql shell


    【解决方案1】:

    在shell中做起来更容易,例如:

    dbname=$1
    cnport=$2
    schemaname=$3
    filename=$4
    
    gsql -d ${dbname} -p ${cnport} <<EOF
    create schema $3; --bird should not be hard coded it should be in variable
    set search_path to '$3';
    create table bird.sparrow(id int, name varchar2(20));
    EOF
    

    否则使用 psql 变量

    【讨论】:

      猜你喜欢
      • 2010-12-27
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      相关资源
      最近更新 更多