PostgreSQL: How to pass parameters from command line?

前端 未结 7 707
孤独总比滥情好
孤独总比滥情好 2020-12-23 15:42

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

7条回答
  •  悲&欢浪女
    2020-12-23 16:17

    Found out in PostgreSQL, you can PREPARE statements just like you can in a scripting language. Unfortunately, you still can't use ?, but you can use $n notation.

    Using the above example:

    PREPARE foo(text,text,text) AS
        SELECT  * 
        FROM    foobar
        WHERE   foo = $1
           AND  bar = $2
            OR  baz = $3  ;
    EXECUTE foo('foo','bar','baz');
    DEALLOCATE foo;
    

提交回复
热议问题