PostgreSQL:如何从命令行传递参数? [英] PostgreSQL: How to pass parameters from command line?

查看:692
本文介绍了PostgreSQL:如何从命令行传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用?占位符的脚本中有一个比较详细的查询.我想直接从psql命令行(脚本之外)测试相同的查询.我想避免进入并将所有?替换为实际值,而是希望在查询后传递参数.

I have a somewhat detailed query in a script that uses ? placeholders. I wanted to test this same query directly from the psql command line (outside the script). I want to avoid going in and replacing all the ? with actual values, instead I'd like to pass the arguments after the query.

示例:

SELECT  * 
FROM    foobar
WHERE   foo = ?
   AND  bar = ?
    OR  baz = ?  ;

正在寻找类似的东西:

%> {select * from foobar where foo=? and bar=? or baz=? , 'foo','bar','baz' };

推荐答案

例如,您可以使用-v构造

You can use the -v construct e.g

psql -v v1=12  -v v2="'Hello World'" -v v3="'2010-11-12'"

,然后将sql中的变量称为:v1,:v2等

and then refer to the variables in sql as :v1, :v2 etc

select * from table_1 where id = :v1;

请注意我们如何使用两个引号" '...' "

Please pay attention on how we pass string/date value using two quotes " '...' "

这篇关于PostgreSQL:如何从命令行传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆