postgresql 使用游标

-- 临时表返回结果
BEGIN;
DO $$
    DECLARE
        temp_geometry st_geometry;  
        geometry_record RECORD;
        cur_geometry CURSOR FOR SELECT shape as shape FROM wgs84_gd_bas_mainbasin;
    BEGIN
        OPEN cur_geometry;
        FETCH cur_geometry INTO temp_geometry;
        LOOP
            FETCH cur_geometry INTO geometry_record;
            EXIT WHEN NOT FOUND;
            temp_geometry := st_union(temp_geometry,geometry_record.shape);
        END LOOP;
        CLOSE cur_geometry;

        DROP TABLE IF EXISTS temp_table;
        CREATE TEMP TABLE temp_table AS 
        SELECT st_envelope(temp_geometry) shape;
    END; 
$$;
COMMIT;
SELECT st_astext(shape) FROM temp_table;


-- 函数返回结果
CREATE OR REPLACE FUNCTION get_basin_data(code varchar,section_shape geometry)
RETURNS VOID AS $$
DECLARE
    record RECORD;
    cur_basin CURSOR(code varchar,section_shape geometry) FOR SELECT objectid,shape FROM sde.main_basin;
BEGIN
    OPEN cur_basin(code,section_shape);
    LOOP
        FETCH cur_basin INTO record;
        EXIT WHEN NOT FOUND;
        --插入数据
        IF st_intersects(section_shape,record.shape) THEN
             INSERT INTO public.water_section_main_basin(water_section_code, main_basin_object_id) VALUES (code, record.objectid);
        END IF;
    END LOOP;
    CLOSE cur_basin;
END; $$
LANGUAGE plpgsql;


猜你喜欢

转载自www.cnblogs.com/kerwincui/p/9122108.html