创建Postgres函数时出现语法错误

时间:2015-08-02 15:54:48

标签: sql postgresql

我正在尝试创建一个函数,该函数对所有一个查询值的结果进行求和,并将其与另一个简单查询进行比较。

这就是我所拥有的,但是我在开始(第二行)附近遇到语法错误:

CREATE FUNCTION trigf1(sbno integer, scid numeric(4,0)) RETURNS integer
BEGIN
    declare sum int default 0;
    declare max as SELECT totvoters FROM ballotbox WHERE cid=scid AND bno=sbno;

    for r as
        SELECT nofvotes FROM votes WHERE cid=scid AND bno=sbno;
    do
        set sum = sum + r.nofvotes;
    end for

    if sum > max
        then return(0);
    else
        return(1);
END

这导致:

  

'BEGIN'附近的语法错误

我正在使用postgreSQL和pgadminIII(以防它是相关的)。

我不知道为什么我会收到这个错误,所有内容似乎都与教科书定义完全一样。 (这是我正在使用的教科书:http://digilib.usu.ac.id/buku/107859/Database-systems-concepts,-6th-ed.html

3 个答案:

答案 0 :(得分:10)

我不知道哪本"教科书"你正在使用,但如果你写的所有内容与那本书完全一样,那本书就完全错了:

CREATE FUNCTION trigf1(sbno integer, scid numeric(4,0)) 
    RETURNS integer
AS         -- error #1: no AS keyword
$body$     -- error #2: use dollar quoting to specify the function body as a string
DECLARE    -- error #3: the declare block comes before the actual code
   sum_ integer := 0; -- error #5: you can't use a reserved keyword as a variable
   max_ integer;      -- error #6:  you can't initialize a variable with a select,
   r   record;   -- you need to declare the record for the cursor loop
BEGIN
   select totvoters
     into max_
   from ballotbox 
   WHERE cid=scid AND bno=sbno;

    -- error #7: the syntax for a loop uses IN not AS
    -- error #8: you need to declare R before you can use it
    -- error #9: the SELECT for a cursor loop must NOT be terminated with a ;
    FOR r IN SELECT nofvotes FROM votes WHERE cid=scid AND bno=sbno
    loop  -- error #10: you need to use LOOP, not DO

        sum_ := sum_ + r.nofvotes;  -- error #11: you need to use := for an assignment, not SET
    end loop; -- error #12: it's END LOOP
              -- error #13: you need to terminate the statement with a ;

    if sum_ > max_ then 
       return 0;
    else
       return 1;
    end if; -- error #14: an END if is required
END;
$body$
language plpgsql; -- error #14: you need to specify the language

手册记录了所有这些:

不需要整个FOR循环且效率极低。它可以替换为:

SELECT sum(nofvotes)
  into sum_
FROM votes 
WHERE cid=scid AND bno=sbno;

Postgres有一个原生的布尔类型,使用它而不是整数更好。如果将函数声明为returns boolean,则最后一行可以简化为

return max_ > sum_;

这部分:

 select totvoters
   into max_
 from ballotbox 
 WHERE cid=scid AND bno=sbno;
如果cid,bno在表格选票箱中是唯一的,

才能正常工作。否则,如果select返回多行,则可能在运行时出现错误。

假设ballotbox上的select确实使用了主键(或唯一键),整个函数可以简化为一个小的SQL表达式:

create function trigf1(sbno integer, scid numeric(4,0))
  returns boolean
as
$body$
  return (select totvoters from ballotbox WHERE cid=scid AND bno=sbno) > 
         (SELECT sum(nofvotes) FROM votes WHERE cid=scid AND bno=sbno);
$body$
language sql;

答案 1 :(得分:0)

我不是一个postgresSQL人,但我会想到

declare max as SELECT totvoters FROM ballotbox WHERE cid=scid AND bno=sbno;

应该是

declare max := SELECT totvoters FROM ballotbox WHERE cid=scid AND bno=sbno;

答案 2 :(得分:0)

函数的主体应该是as 'code...'关键字后面的字符串,即CREATE FUNCTION trigf1(sbno integer, scid numeric(4,0)) RETURNS integer AS $$ BEGIN declare sum int default 0; declare max as SELECT totvoters FROM ballotbox WHERE cid=scid AND bno=sbno; for r as SELECT nofvotes FROM votes WHERE cid=scid AND bno=sbno; do set sum = sum + r.nofvotes; end for if sum > max then return(0); else return(1); END $$ 。通常使用A Closer Look at the Paint Mechanism

 $(document).ready(function()
 {

   $("#next").click(function()
    {
       var qid=$("#q").val();
        alert(qid);
        var d1=$("#r1").val();
        var d2=$("#r2").val();
        var d3=$("#r3").val();
        var d4=$("#r4").val();
        $.ajax({url:"<?php echo base_url().'index.php/ajax/question1';?>",data:{id:qid,a1:d1,a2:d2,a3:d3,a4:d4},success:function(data)
                 { 
                   $("#dd").html(data);
                  }
              });
     });
   });