[英]How to pick data from list in pgsql
In PostgreSQL I am able to pick data from the query of a table. 在PostgreSQL中,我可以从表查询中选择数据。 As for example I want to display entityname from t1 using for loop.
例如,我想使用for循环从t1显示实体名称。
DO
$do$
DECLARE
e varchar;
BEGIN
FOR e IN (SELECT entityname
FROM t1)
LOOP
raise notice 'Value: %', e;
END LOOP;
END
$do$;
But If I already have list of entityname and I just want to display it. 但是,如果我已经有实体名称列表,而只想显示它。 What will be the correct way, I tried using
我尝试使用的正确方法是什么
DO
$do$
DECLARE
e varchar;
BEGIN
FOR e IN ('t1','t2')
LOOP
raise notice 'Value: %', e;
END LOOP;
END
$do$;
But it is giving error 但这给了错误
ERROR: syntax error at or near "'t1'"
LINE 6: FOR e IN ('t1','t2')
What will be the right way to do this ? 正确的方法是什么?
The correct statement is FOREACH target IN ARRAY , which iterates over a array: 正确的语句是FOREACH目标IN ARRAY ,它遍历数组:
FOREACH target [ SLICE number ] IN ARRAY expression LOOP
statements
END LOOP;
Also in you case: 同样在您的情况下:
DO $do$ DECLARE e varchar;
BEGIN
FOREACH e IN ARRAY ARRAY['t1','t2'] LOOP
raise notice 'Value: %', e;
END LOOP;
END $do$;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.