2011-03-28 55 views
2

功能,但并不功能...声明变量不是在用我要声明Postgres里变量的Postgres

Declare 
    c varchar; 
    a integer; 
    b integer; 

    select b = count (*) from table 

    set a = 1 
     while a <= b 
begin 

    select c = [c] from table where id = a 

    if (c = '1') 
     insert into table2 select(*) from table 

end 


set a = a+1 

但错误错误:在语法错误或接近“VARCHAR” LINE 2:C VARCHAR; ^ 我希望有人能帮助我

回答

8

如果你是9.0,你可以使用使用DO语句匿名块:

http://www.postgresql.org/docs/current/static/sql-do.html

9.0之前,你不能使用匿名PL/pgSQL的块,你会需要为此创建一个函数。

此外,你有pl/pgSQL的语法完全错误。

在同一行中不能有赋值语句和WHILE语句。您还遗漏了WHILE循环的其他requird关键字。

请参考手册,以了解正确的语法:

http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#AEN50163

语法读取从选择一个值的变量也是错误的。

正确的语法来检索SELECT的结果是:

SELECT count(*) 
    INTO b 
FROM some_table; 

退房手动为此,以及:

http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW