PostgreSQL:查询在带有变量的查询中没有结果数据的目的地

Posted

技术标签:

【中文标题】PostgreSQL:查询在带有变量的查询中没有结果数据的目的地【英文标题】:PostgreSQL: Query has no destination for result data in a query with variables 【发布时间】:2020-04-28 19:05:27 【问题描述】:

错误:查询没有结果数据的目的地

提示:如果要丢弃 SELECT 的结果,请改用 PERFORM。

上下文:PL/pgSQL 函数 inline_code_block 第 11 行的 SQL 语句


DO $$
DECLARE
id_renovacion integer := (select Id_renovacionContrato 
from BEA.DIM_RENOVACION_CONTRATO where upper(Desc_renovacionContrato) = 'UNDEFINED'); 

id_renovacionpending integer := (select Id_renovacionContrato 
from BEA.DIM_RENOVACION_CONTRATO where upper(Desc_renovacionContrato) = 'PENDING');


BEGIN

SELECT count(id_contrato) as id_cantidad,
                CASE    when count(id_contrato) = 0 THEN 'ImagesHappyMaritransparente1.png'
                             when count(id_contrato) is null THEN 'ImagesHappyMaritransparente1.png'
                             when count(id_contrato) = 1 THEN 'ImagesTITIhappymarycontratos-off-1_145.png'
                             when count(id_contrato) = 2 THEN 'ImagesTITIhappymarycontratos-off-2_145.png'
                             when count(id_contrato) = 3 THEN 'ImagesTITIhappymarycontratos-off-3_145.png'
                             when count(id_contrato) = 4 THEN 'ImagesTITIhappymarycontratos-off-4_145.png'
                             when count(id_contrato) = 5 THEN 'ImagesTITIhappymarycontratos-off-5_145.png'
                             when count(id_contrato) = 6 THEN 'ImagesTITIhappymarycontratos-off-6_145.png'
                             when count(id_contrato) = 7 THEN 'ImagesTITIhappymarycontratos-off-7_145.png'
                             when count(id_contrato) = 8 THEN 'ImagesTITIhappymarycontratos-off-8_145.png'
                             when count(id_contrato) = 9 THEN 'ImagesTITIhappymarycontratos-off-9_145.png'
                             when count(id_contrato) > 9 THEN 'ImagesTITIhappymarycontratos-off-9+_145.png'
                END
 FROM BEA.FACT_CONTRATOS
WHERE cantDiasDif > 0 AND Id_rangoContrato IN (id_renovacion, id_renovacionpending);

END 
$$

LANGUAGE plpgsql; ```

【问题讨论】:

错误信息非常具有描述性。你明白它在说什么吗?您打算如何处理选定的数据? 您的问题是什么? “你如何避免:ERROR: query has no destination for result data”? 【参考方案1】:

我可以在发布的代码中看到两个问题。

    create 在代码中。我不知道为什么你的匿名块中有create

    您的 DO 块中有以下 SELECT:

SELECT count(id_contrato) as id_cantidad,
                CASE    when count(id_contrato) = 0 THEN 'ImagesHappyMaritransparente1.png'
                             when count(id_contrato) is null THEN 'ImagesHappyMaritransparente1.png'
                             when count(id_contrato) = 1 THEN 'ImagesTITIhappymarycontratos-off-1_145.png'
                             when count(id_contrato) = 2 THEN 'ImagesTITIhappymarycontratos-off-2_145.png'
                             when count(id_contrato) = 3 THEN 'ImagesTITIhappymarycontratos-off-3_145.png'
                             when count(id_contrato) = 4 THEN 'ImagesTITIhappymarycontratos-off-4_145.png'
                             when count(id_contrato) = 5 THEN 'ImagesTITIhappymarycontratos-off-5_145.png'
                             when count(id_contrato) = 6 THEN 'ImagesTITIhappymarycontratos-off-6_145.png'
                             when count(id_contrato) = 7 THEN 'ImagesTITIhappymarycontratos-off-7_145.png'
                             when count(id_contrato) = 8 THEN 'ImagesTITIhappymarycontratos-off-8_145.png'
                             when count(id_contrato) = 9 THEN 'ImagesTITIhappymarycontratos-off-9_145.png'
                             when count(id_contrato) > 9 THEN 'ImagesTITIhappymarycontratos-off-9+_145.png'
                END
 FROM BEA.FACT_CONTRATOS
WHERE cantDiasDif > 0 AND Id_rangoContrato IN (id_renovacion, id_renovacionpending);

而且您没有将 SELECT 的返回结果存储到任何变量中。如果您不关心 SELECT 结果,请使用 PERFORM,如下所示:

PERFORM count(id_contrato) as id_cantidad,
                CASE    when count(id_contrato) = 0 THEN 'ImagesHappyMaritransparente1.png'
                             when count(id_contrato) is null THEN 'ImagesHappyMaritransparente1.png'
                             when count(id_contrato) = 1 THEN 'ImagesTITIhappymarycontratos-off-1_145.png'
                             when count(id_contrato) = 2 THEN 'ImagesTITIhappymarycontratos-off-2_145.png'
                             when count(id_contrato) = 3 THEN 'ImagesTITIhappymarycontratos-off-3_145.png'
                             when count(id_contrato) = 4 THEN 'ImagesTITIhappymarycontratos-off-4_145.png'
                             when count(id_contrato) = 5 THEN 'ImagesTITIhappymarycontratos-off-5_145.png'
                             when count(id_contrato) = 6 THEN 'ImagesTITIhappymarycontratos-off-6_145.png'
                             when count(id_contrato) = 7 THEN 'ImagesTITIhappymarycontratos-off-7_145.png'
                             when count(id_contrato) = 8 THEN 'ImagesTITIhappymarycontratos-off-8_145.png'
                             when count(id_contrato) = 9 THEN 'ImagesTITIhappymarycontratos-off-9_145.png'
                             when count(id_contrato) > 9 THEN 'ImagesTITIhappymarycontratos-off-9+_145.png'
                END
 FROM BEA.FACT_CONTRATOS
WHERE cantDiasDif > 0 AND Id_rangoContrato IN (id_renovacion, id_renovacionpending);

但是,请问您为什么要这样做?让我们知道不存储 SELECT 的结果或您的意图的原因。

【讨论】:

嗨 Vibhor,是的,你是对的,“创建”是代码中的一个错误,我正在尝试解决它,创建一个表并解决它。另外,我应用了您的建议,在代码中添加了 PERFORM 而不是 SELECT,但我需要显示结果,使用 CREATE 已解决。回答您的问题我正在使用它在 BI 应用程序中显示它:MicroStrategy。感谢您的回答【参考方案2】:

此代码解决问题,使用 CREATE 表显示结果。

DO $$
DECLARE
id_renovacion integer := (select Id_renovacionContrato 
from BEA.DIM_RENOVACION_CONTRATO where upper(Desc_renovacionContrato) = 'UNDEFINED'); 

id_renovacionpending integer := (select Id_renovacionContrato 
from BEA.DIM_RENOVACION_CONTRATO where upper(Desc_renovacionContrato) = 'PENDING');


BEGIN

DROP TABLE if EXISTS  tbcontratos;
CREATE TABLE tbcontratos (id_cantidad integer, images varchar(200));

insert into tbcontratos
SELECT count(id_contrato) as id_cantidad,
                CASE    when count(id_contrato) = 0 THEN 'Images\HappyMari\transparente1.png'
                             when count(id_contrato) is null THEN 'Images\HappyMari\transparente1.png'
                             when count(id_contrato) = 1 THEN 'Images\TITI\happymary\contratos-off-1_145.png'
                             when count(id_contrato) = 2 THEN 'Images\TITI\happymary\contratos-off-2_145.png'
                             when count(id_contrato) = 3 THEN 'Images\TITI\happymary\contratos-off-3_145.png'
                             when count(id_contrato) = 4 THEN 'Images\TITI\happymary\contratos-off-4_145.png'
                             when count(id_contrato) = 5 THEN 'Images\TITI\happymary\contratos-off-5_145.png'
                             when count(id_contrato) = 6 THEN 'Images\TITI\happymary\contratos-off-6_145.png'
                             when count(id_contrato) = 7 THEN 'Images\TITI\happymary\contratos-off-7_145.png'
                             when count(id_contrato) = 8 THEN 'Images\TITI\happymary\contratos-off-8_145.png'
                             when count(id_contrato) = 9 THEN 'Images\TITI\happymary\contratos-off-9_145.png'
                             when count(id_contrato) > 9 THEN 'Images\TITI\happymary\contratos-off-9+_145.png'
                END
 FROM BEA.FACT_CONTRATOS
WHERE cantDiasDif > 0 AND Id_rangoContrato IN (id_renovacion, id_renovacionpending);



END 
$$

LANGUAGE plpgsql;

select id_cantidad, images from tbcontratos; ```

【讨论】:

以上是关于PostgreSQL:查询在带有变量的查询中没有结果数据的目的地的主要内容,如果未能解决你的问题,请参考以下文章

有没有一种简单的方法可以在 PostgreSQL 查询中使用变量? [复制]

限制 PostgreSQL 最小时间间隔的结果行

如何在 PostgreSQL 查询中声明变量 [重复]

Postgresql - 使用带有改变序列表达式的子查询

插入带有字符串的数组时,postgresql 查询会出错

如何在全范围内平均减少 SQL 查询的结果行?