我怎麼能遍歷整數[]如果我有:
operators_ids = string_to_array(operators_ids_g,',')::integer[];
我想遍歷operators_ids
。
我不能做這樣:
FOR oid IN operators_ids LOOP
這:
FOR oid IN SELECT operators_ids LOOP
oid
是integer
;爲什麼你需要這樣做:遍歷整數[]在PLPGSQL
0
A
回答
1
你能像
DO
$body$
DECLARE your_array integer[] := '{1, 2, 3}'::integer[];
BEGIN
FOR i IN array_lower(your_array, 1) .. array_upper(your_array, 1)
LOOP
-- do something with your value
raise notice '%', your_array[i];
END LOOP;
END;
$body$
LANGUAGE plpgsql;
但在我看來,主要問題是迭代一個數組?有機會,你可以用更好的方式解決你的問題,例如:
DO
$body$
DECLARE i record;
BEGIN
FOR i IN (SELECT operators_id FROM your_table)
LOOP
-- do something with your value
raise notice '%', i.operators_id;
END LOOP;
END;
$body$
LANGUAGE plpgsql;
0
我認爲Dezso是正確的。您不需要使用索引循環使用數組。 如果你犯了一個select語句通過爲person_id組合分組與限制1,你有結果集,你想:
create or replace function statement_example(p_data text[]) returns int as $$
declare
rw event_log%rowtype;
begin
for rw in select * from "PRD".events_log where (event_type_id = 100 or event_type_id = 101) and person_id = any(operators_id::int[]) and plc_time < begin_date_g order by plc_time desc group by person_id limit 1 loop
raise notice 'interesting log: %', rw.field;
end loop;
return 1;
end;
$$ language plpgsql volatile;
這應該執行好得多。 如果你還是喜歡循環整數數組,有很多person_ids的照顧,那麼可能你考慮使用輕量級設計模式:
create or replace function flyweight_example(p_data text[]) returns int as $$
declare
i_id int;
i_min int;
i_max int;
begin
i_min := array_lower(p_data,1);
i_max := array_upper(p_data,1);
for i_id in i_min .. i_max loop
raise notice 'interesting log: %',p_data[i_id];
end loop;
return 1;
end;
$$ language plpgsql volatile;
相關問題
- 1. 遍歷整個div
- 2. 在C++中按字節遍歷整數
- 3. 在PL/pgSQL中遍歷整數[]
- 4. 在整數中循環遍歷,ruby
- 5. 遍歷可選的整數列表ifPresent
- 6. For循環不遍歷整個數組
- 7. 的Javascript遍歷字符串和整數
- 8. 遍歷樹遍歷
- 9. 遍歷數組
- 10. 遍歷數組
- 11. 遍歷數組
- 12. 遍歷數組
- 13. 遍歷數組
- 14. 遍歷數組
- 15. 遍歷數組
- 16. 遍歷數組
- 17. 遍歷數組
- 18. 在C中自動遍歷樹遍歷#
- 19. 在DbCommand中遍歷參數
- 20. 遍歷JSON數據在JavaScript
- 21. 在python遍歷計數對
- 22. 在javascript中遍歷數組?
- 23. LinkedHashMap遍歷鍵遍歷
- 24. 如何遍歷整數數組列表的元素在Java中
- 25. 操作者不存在:整數=整數[] PLPGSQL錯誤
- 26. 遍歷在Signalr
- 27. 遍歷在Go
- 28. 遍歷在PHP
- 29. 遍歷在Python
- 30. 遍歷在Python
因爲operators_ids是,我想在SELECT語句中使用的字段循環:FOR oid IN SELECT operators_ids LOOP \t RETURN QUERY SELECT * FROM「PRD」.events_log WHERE(event_type_id = 100 OR event_type_id = 101)AND ** person_id = oid ** AND plc_time
user1756277
但我從函數參數中提供了operators_id作爲文本(整數用逗號分隔) – user1756277
然後,您應該重新設計您的模式或訪問數據的方式。 – dezso