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

标签 sql linux postgresql shell

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

相关文章:

sql - 数据库作业调度

java - 表数据覆盖

mysql - 对另一个查询的最高值进行排序

sql - 使用 count 函数合并多个查询

mysql - 删除一个表中给定另一表中的值的行-SQLite

linux - OpenCV错误: Image step is wrong

Linux 中的 Python : Put user input asynchronously into queue

C程序打印目录中的目录名并排除当前目录和父目录

php - 简单的数据库查询功能

postgresql - 使用 terraform 创建只读 postgres 用户