动态 SQL (EXECUTE) 作为 IF 语句的条件

发布于 2024-12-20 13:54:25 字数 244 浏览 3 评论 0原文

我想要执行一条动态 SQL 语句,其返回值是 IF 语句的条件:

IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN

这会生成错误 ERROR: type "execute" does not exit

是否可以这样做,或者是否需要在IF语句之前执行SQL到变量中,然后检查该变量作为条件?

I want to execute a dynamic SQL statement, with its returned value being the conditional for an IF statement:

IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN

This generates the error ERROR: type "execute" does not exist.

Is it possible to do this, or is it necessary to execute the SQL before the IF statement into a variable, and then check the variable as the conditional?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

债姬 2024-12-27 13:54:25

这种构造是不可能的:

IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN ...

您可以简化为:

IF EXISTS (SELECT FROM mytable) THEN ...

但您的示例可能已简化。对于通过 EXECUTE 执行的动态 SQL在此处阅读手册。您可以在执行任何 DML 命令后立即检查特殊变量 FOUND 以查看是否有任何行受到影响:

IF FOUND THEN ...

但是:

请特别注意,EXECUTE 会更改GET DIAGNOSTICS 的输出,但不会更改FOUND。< /p>

大胆强调我的。对于普通的EXECUTE,请执行以下操作:

...
DECLARE
   i int;
BEGIN
   EXECUTE 'SELECT FROM mytable';  -- something dynamic here

   GET DIAGNOSTICS i = ROW_COUNT;

   IF i > 0 THEN ...

,如果合适的话 - 特别是只有单行结果 - 使用 INTO 子句 EXECUTE 直接从动态查询中获取结果。我在此处引用手册

如果提供了行或变量列表,它必须与
查询结果的结构(当使用记录变量时,它
将配置自身以自动匹配结果结构)。如果
返回多行,只有第一行会被分配给
INTO 变量。如果没有返回行,则将 NULL 分配给 INTO
变量。

...
DECLARE
   _var1 int;  -- init value is NULL unless instructed otherwise
BEGIN

EXECUTE format('SELECT var1 FROM %I WHERE x=y LIMIT 1', 'my_Table')
INTO    _var1;

IF _var1 IS NOT NULL THEN ...

This construct is not possible:

IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN ...

You can simplify to:

IF EXISTS (SELECT FROM mytable) THEN ...

But your example is probably simplified. For dynamic SQL executed with EXECUTE, read the manual here. You can check the special variable FOUND immediately after executing any DML command to see whether any rows were affected:

IF FOUND THEN ...

However:

Note in particular that EXECUTE changes the output of GET DIAGNOSTICS, but does not change FOUND.

Bold emphasis mine. For a plain EXECUTE do this instead:

...
DECLARE
   i int;
BEGIN
   EXECUTE 'SELECT FROM mytable';  -- something dynamic here

   GET DIAGNOSTICS i = ROW_COUNT;

   IF i > 0 THEN ...

Or, if opportune - in particular with only single-row results - use the INTO clause with EXECUTE to get a result from the dynamic query directly. I quote the manual here:

If a row or variable list is provided, it must exactly match the
structure of the query's results (when a record variable is used, it
will configure itself to match the result structure automatically). If
multiple rows are returned, only the first will be assigned to the
INTO variable. If no rows are returned, NULL is assigned to the INTO
variable(s).

...
DECLARE
   _var1 int;  -- init value is NULL unless instructed otherwise
BEGIN

EXECUTE format('SELECT var1 FROM %I WHERE x=y LIMIT 1', 'my_Table')
INTO    _var1;

IF _var1 IS NOT NULL THEN ...
贪了杯 2024-12-27 13:54:25

Matt,

从上面的语法来看,您正在编写 PL/pgSQL,而不是 SQL。根据这个假设,有两种方法可以完成您想要的操作,但是两种方法都需要两行代码:

EXECUTE 'SELECT EXISTS (SELECT 1 FROM ' || table_variable || ' );' INTO boolean_var;

IF boolean_var THEN ...

或者:

EXECUTE 'SELECT 1 FROM ' || table_variable || ' );';

IF FOUND THEN ...

“FOUND”是一个特殊变量,用于检查最后一次查询运行是否返回任何行。

Matt,

From the syntax above, you're writing PL/pgSQL, not SQL. On tht assumption, there are two ways to do what you want, but both will require two lines of code:

EXECUTE 'SELECT EXISTS (SELECT 1 FROM ' || table_variable || ' );' INTO boolean_var;

IF boolean_var THEN ...

Or:

EXECUTE 'SELECT 1 FROM ' || table_variable || ' );';

IF FOUND THEN ...

"FOUND" is a special variable which checks if the last query run returned any rows.

琴流音 2024-12-27 13:54:25
SET @SQLQUERY='SELECT 1 FROM mytable'

EXEC (@SQLQUERY)
If @@RowCount >0  THEN
SET @SQLQUERY='SELECT 1 FROM mytable'

EXEC (@SQLQUERY)
If @@RowCount >0  THEN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文