Postgresql ColdFusion CFQuery Block

Sat*_*rma 6 postgresql coldfusion coldfusion-9 postgresql-9.1

我是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>
Run Code Online (Sandbox Code Playgroud)

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

Element RESULTVALUE is undefined in querysampleresult
Run Code Online (Sandbox Code Playgroud)

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

<cfset xyz.code = querysampleresult.resultValue  >
Run Code Online (Sandbox Code Playgroud)

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

Clo*_*eto 1

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

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

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

select f() as resultValue;
Run Code Online (Sandbox Code Playgroud)