![](/img/trans.png)
[英]How to pass SQL credentials to an PowerShell script as command-line arguments
[英]Pass arguments to a sql file via command line arguments in shell script
我正在使用 SnowSQL 运行 SQL 脚本。 我有命令行参数,在我的 SnowSQL 调用期间也需要将这些参数传递给 SQL 脚本。 这是我所拥有的:
SQL 脚本(file.sql):
select * from table where cli_id = '$1' and store_id = '$2';
外壳脚本(run.sh):
snowsql -c credentials -f file.sql
我使用以下命令在我的终端中运行它:
sh run.sh 123 555
这不起作用 - 如何修复它以将参数正确传递给 SQL 文件?
#!/usr/bin/env bash
snowsql -c credentials -D COLOR="$1" -D SIZE="$2" -f file.sql
在 SQL 文件中:
SELECT id, name, description IN articles WHERE colour=&COLOR AND size=&SIZE;
请参阅: 使用 SnowSQL — Snowflake 文档 / 使用变量 / 连接时定义
连接时定义(-D 或 --variable 连接参数)
要在连接到 Snowflake 时定义变量,请在终端命令行上指定 -D 或 --variable 连接参数,后跟变量名称和值,格式为 <variable_name>=<variable_value>。
例如:
Linux/macOS
$ snowsql ... -D tablename=CENUSTRACKONE --variable db_key=$DB_KEY
视窗
$ snowsql ... -D tablename=CENUSTRACKONE --variable db_key=%DB_KEY%
尝试将脚本file.sql
更改为:
select * from table where cli_id = '&cli_id' and store_id = '&store_id';
.. 和run.sh
应该是:
snowsql -c credentials --variable cli_id=$1 --variable store_id=$2 -f file.sql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.