2011-03-28 119 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