2013-05-21 57 views
0

我從我製作的交叉表中提取所有column_names(cname1)。有成千上萬的這些列名,所以我將它們組合成一個數組。然後我想使用動態sql(或其他任何工作)來使用這些column_names來創建一個基於該交叉表的記錄的數組。我不斷收到錯誤:動態Sql:使用列名稱數組從記錄創建數組

ERROR: missing "LOOP" at end of SQL expression 

CREATE OR REPLACE FUNCTION mffcu.test_ty_hey() 
RETURNS setof record 
LANGUAGE plpgsql 
AS $function$ 
Declare 
     cname1 text;   
Begin 

for cname1 in select array_agg(column_name) as useme 
from(
select column_name::text 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'crosstab_183' 
and ordinal_position != 1 
) as fin 
join mffcu.crosstab_183 a on fin.id = a.id; 

loop 
sql2 ='select distinct array['|| columnname ||'] from mffcu.crosstab_183'; 
execute sql2; 
end loop; 
END; 
$function$ 

我不能爲我的生活找出爲什麼我得到這個錯誤。

回答

1
 
for cname1 in select array_agg(column_name) as useme 
from(
select column_name::text 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'crosstab_183' 
and ordinal_position != 1 
) as fin 
join mffcu.crosstab_183 a on fin.id = a.id; --here should not be semicolon! 
loop