简体   繁体   English

如何将变量从Shell脚本传递到sql文件

[英]How to pass a variable from shell script to sql file

I am having a shell file named test.sh which is invoking other sql file 'table.sql'. 我有一个名为test.sh的外壳文件,正在调用其他sql文件“ table.sql”。 'table.sql' file will create some tables, but I want to create the tables in a particular schema 'bird'. 'table.sql'文件将创建一些表,但是我想在特定模式'bird'中创建表。

content of sql file. 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));

content of shell file. 外壳文件的内容。

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]

I will execute my shell file like this 我将像这样执行我的shell文件

sh test.sh db1 9999 bird table.sql 

it is easier doing it in shell, eg: 在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

otherwise use psql variables 否则使用psql变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM