简体   繁体   English

如何在 Postgres sql 脚本中执行动态 SQL 语句

[英]How to execute dynamic SQL statement inside Postgres sql script

Below is my script.下面是我的脚本。 I used Execute command to run the dynamic sql statement that i created but it doesn't run and give an output.我使用Execute命令运行我创建的动态 sql 语句,但它没有运行并给出输出。 It doesn't give an error either.它也不会出错。 How can I run this sql statement inside postgres sql script?如何在 postgres sql 脚本中运行此 sql 语句?

DO $$
DECLARE sqlCommand varchar(1000);
 columnList varchar(75);

BEGIN
select  '"BRANCH_NAME", "ADDRESS1"' into columnList;

select concat('SELECT ' , columnList , ' FROM "TESTSCHEMA"."BRANCH" ')  into sqlCommand;

raise info 'COMMAND %', sqlCommand;
execute sqlCommand;

END $$;

Your example is safe, but if you are dealing with user input, always use the format function to construct the SQL string.您的示例是安全的,但如果您正在处理用户输入,请始终使用format函数来构造 SQL 字符串。

Anyway, your problem is that the EXECUTE statement has no INTO clause with a destination for the query results, si they are simply discarded.无论如何,您的问题是EXECUTE语句没有带有查询结果目的地的INTO子句,因为它们只是被丢弃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM