2013-04-11 248 views
15

的名單這裏是我的簡單的匿名代碼塊:PSQL - 超過行循環的循環變量必須是一個記錄或者行變量或標量變量

do $$ 
    declare foo varchar(50) := ''; 
    begin 
    for a in 
     select a from (values('foo'), ('bar'), ('fooBar')) s(a) 
    loop 
     foo := a; 
     print foo; 
    end loop; 
    end; 
$$; 

當我運行它:

psql -f test.sql 

我得到這個錯誤:

psql:test.sql:11: ERROR: loop variable of loop over rows must be a record or row variable or list of scalar variables 
LINE 4:  for a in 
      ^

回答

39

解決它自己,MEH:

do $$ 
    declare 
    arow record; 
    foo varchar(50); 
    begin 
    for arow in 
     select a from (values('foo'), ('bar'), ('fooBar')) s(a) 
    loop 
     foo := arow.a; 
     RAISE NOTICE 'Calling cs_create_job(%)', foo; 
    end loop; 
    end; 
$$; 
+0

相關:https://dba.stackexchange.com/q/191694/104401 – Wildcard 2017-11-25 06:00:05