【问题标题】:Postgresql ColdFusion CFQuery BlockPostgresql ColdFusion CFQuery 块
【发布时间】:2013-01-11 18:24:30
【问题描述】:

我是 ColdFusion 的新手,我在 CFQuery 中编写了一段 Postgres 代码:

<cffunction name="insertToReport" access="private" returntype="struct" output="false" >

  <cfset var xyz = structNew() >
  <cfset xyz.code = "">
      <cfquery name="querysampleresult" datasource="abc">
            DO            
            $BODY$
                DECLARE resultValue int;
            BEGIN
               resultValue = 1;
               SELECT resultValue INTO resultValue ;    
            END;
            $BODY$
      </cfquery>

            <cfset xyz.code = querysampleresult.resultValue  >

           <cfreturn xyz >
</cffunction>

我的问题是我无法访问 CFQuery 标记之外的变量 resultValue,即它抛出了异常:

Element RESULTVALUE is undefined in querysampleresult

这发生在函数末尾的 CFSet 语句中:

<cfset xyz.code = querysampleresult.resultValue  >

我不知道如何在结构体中设置变量resultValue,我一定会从这里返回一个结构体到调用环境。 请帮助我,提前致谢。

【问题讨论】:

    标签: postgresql coldfusion coldfusion-9 postgresql-9.1


    【解决方案1】:

    将另一个选择语句添加到选择结果值的查询中。现在您正在选择 INTO 一个不返回查询的表。

    <cffunction name="insertToReport" access="private" returntype="struct" output="false" >
    <cfargument name="rptDataObj" required="yes" type="com.certain.register123.data.reportData" hint="The affected report.">
    <cfargument name="maxColumns" type="numeric" required="false" default="10" hint="The maximum number of active columns that should be saved to the report. " ><!--- As of REG-559 20060215, the default was 10 --->
    <cfargument name="dsn" type="string" required="false" default="#application.portals.data[request.applicationName].dsn#" >
    <cfset var uiCustomColumn = queryNew("") >
    <cfset var strSaveError = structNew() >
    <cfset strSaveError.code = 0 >
    <cfset strSaveError.message = "">
    <cfset strSaveError.udcId = "">
    
    
    <cfquery name="uiCustomColumn" datasource="#arguments.dsn#">
    DO            
    $BODY$
        DECLARE resultValue int;
        DECLARE nextId bigint;
        DECLARE maxColumns bigint;
        DECLARE udcReportId bigint; /*Have to use this value more than once, so declare it to reduce number of parameters*/
        DECLARE udcOrder int;  /*Have to use this value more than once, so declare it to reduce number of parameters*/
    
    BEGIN
        udcReportId = #arguments.rptDataObj.getId()# ;
        maxColumns = #arguments.maxColumns# ;
    
        IF (( select count( udc_id ) from user_defined_column WHERE udc_frn_rpt_id = udcReportId AND udc_is_active = true ) >= maxColumns) THEN
            BEGIN
                resultValue = 1; /*There isn't an available slot for this column */
            END;
            ELSE
            BEGIN
    
                nextId = (SELECT coalesce( MAX(udc_id), 0 ) FROM user_defined_column ) + 1 ;
                udcOrder = (SELECT coalesce( MAX(udc_order), 0 ) FROM user_defined_column WHERE udc_frn_rpt_id = udcReportId AND udc_is_active = true ) + 1 ;
    
                INSERT INTO user_defined_column(
                    udc_id
                    ,udc_frn_rpt_id
                    ,udc_label
                    ,udc_data
                    ,udc_type
                    ,udc_order
                    ,udc_is_active
                    ,udc_date_created
                    ,udc_date_modified
                )
                VALUES(
                    nextId
                    ,udcReportId
                    ,'hi'
                    ,'hi'
                    ,12
                    ,udcOrder
                    ,true
                    ,now()
                    ,now()
                );
    
    
                resultValue = 0;
            END ;
        END IF;
        SELECT resultValue, nextId INTO resultValue  /*Set a success result */
              , nextId;
    
        SELECT resultValue;
    
    END;
    $BODY$
    </cfquery>
    
    <cfset strSaveError.code = uiCustomColumn.resultValue  >
    <cfset strSaveError.udcId = uiCustomColumn.nextId  >
    
    <cfreturn strSaveError >
    

    【讨论】:

    • 如果我使用“SELECT resultValue”,那么异常会被抛出:错误:查询没有结果数据的目的地:(
    • 我们不能直接在 pl/pgsql 中使用 select 语句,我们必须要么返回任何值,要么必须将 resultValue 放入带有 INTO 子句的变量中。所以,你上面的代码似乎不起作用cfquery 块之外的 uiCustomColumn 可以使用 resultValue。但是感谢您的支持。 (
    • @Satish 是的,您可以在 plpgsql 中使用选择。问题是,如果您在匿名代码块中而不是在函数中执行此操作,您要么使用into,如您所说,要么使用perform 'select resultValue'。两者都不起作用,因为void 将始终被返回。
    • @Clodoaldo 是的,你是对的,我已经用一个函数更改了匿名块,以便访问 cfQuery 之外的参数,感谢大家的支持。
    • 哎呀,对不起。自从我使用 PGSQL 以来已经有一段时间了,所以我一直在记忆。哦,好吧!
    【解决方案2】:

    匿名代码块 (DO) 始终返回 void。您需要使用一个函数来返回任何其他内容:

    create function f()
    returns integer as $body$
    declare 
        resultValue integer;
    begin
        resultValue := 1;
        return resultValue;
    end;
    $body$
    language plpgsql
    

    函数创建后,您可以在查询中使用它:

    select f() as resultValue;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多