【问题标题】:How to pass the parameter with the file while executing the postgresql command in the comand line?在命令行中执行postgresql命令时如何将参数与文件一起传递?
【发布时间】:2019-05-21 06:37:25
【问题描述】:

我有一个名为insert_all.sql 的文件,其中包含如下内容。这里v1是要传递的参数。

 do $$
    begin
    delete from tabledetails where table_name = '$(v1)';
    end;
    $$;

我正在尝试使用下面给出的命令在该文件 (insert_all.sql) 中执行查询。但它不起作用。我的命令有什么问题?如果错了,请给我建议。

psql.exe -U postgres -p 5454 -h 127.0.0.1 -d desk -f D:\insert_all.sql -v v1='statusTable'

【问题讨论】:

标签: postgresql plpgsql psql


【解决方案1】:

变量选项适用于文件中的简单查询,但不适用于DO 块内。一种选择是创建一个存储变量的 TEMP 表,然后像这样使用它。

create temp table var_temp as select :'v1'::TEXT as var;
 do $$
    begin
    delete from tabledetails where table_name =(select var from var_temp) ;
    end;
 $$; 

psql.exe -U postgres -p 5454 -h 127.0.0.1 -d desk -f D:\insert_all.sql -v v1="statusTable"

如果您没有任何其他需要 DO 块的操作,请考虑将删除作为普通 SQL 语句运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多