将命令行参数传递给sql(Postgres) [英] Pass command line args to sql (Postgres)
本文介绍了将命令行参数传递给sql(Postgres)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将命令行参数传递给使用 psql
(Postgres)运行的sql文件?
How can I pass command line args to sql files ran with psql
(Postgres)?
ie
psql mydatabase< mysqlfile.sql arg1 arg2 arg3 ...
这可能吗?
推荐答案
使用 psql中的变量插值功能。
如果指定 -v variable1 = value1
或-在命令行上设置variable1 = value1
参数,然后sql文件中的:variable1
将替换为相应的文本值。
If you specify -v variable1=value1
or --set variable1=value1
parameter on command line, then :variable1
in the sql file will be replaced with corresponding text value.
注意:如果需要引号,空格等,请使用标准SQL带引号的字符串。
Note: use standard-SQL quoted strings if you need quotes, spaces and so on.
示例:
echo "SELECT :arg1 FROM :arg2 LIMIT 10;" > script.sql
psql mydatabase -v arg1=relname -v arg2=pg_class < script.sql
psql mydatabase -v arg1="'some string' as label" -v arg2=pg_namespace < script.sql
这篇关于将命令行参数传递给sql(Postgres)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文