gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:09 28 4
gpt4 key购买 nike

我有一个名为 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 

最佳答案

在 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变量

关于sql - 如何将变量从shell脚本传递到sql文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46342693/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com