Store result of a query inside a function

Jepessen

I've the following function:

DO
$do$
DECLARE
maxgid      integer;
tableloop   integer;
obstacle    geometry;
simplifyedobstacle  geometry;
BEGIN

    select max(gid) from public.terrain_obstacle_temp into maxgid;

    FOR tableloop IN 1 .. maxgid
    LOOP
        insert into public.terrain_obstacle (tse_coll,tse_height,geom) select tse_coll,tse_height,geom
        from public.terrain_obstacle_temp where gid = tableloop;

    END LOOP;

END
$do$;

I need to modify this function in order to execute different queries according to the type of a column of public.terrain_obstacle_temp.

This is a temporary table created by reading a shapefile, and I need to know the kind of the geom column of that table. I have a query that give the data to me:

SELECT type 
FROM geometry_columns 
WHERE f_table_schema = 'public' 
AND f_table_name = 'terrain_obstacle' 
and f_geometry_column = 'geom';

It returns me a character_varying value (in this case MULTIPOLYGON).

Ho can I modify the function in order to get the result of the query, and create an if statement that allows me to execute some code according to the result of that query?

Glenn

Is the intention to copy all the records from the temp table to the actual table? If so, you may be able to skip the loop:

insert into public.terrain_obstacle (tse_coll, tse_height, geom)
  select tse_coll, tse_height, geom
    from public.terrain_obstacle_temp
;

Do terrain_obstacle and terrain_obstacle_temp have the same structure? If so, then the "insert into ... select ..." should work fine provided the column types are the same.

If conditional typing is required, use the CASE WHEN syntax:

v_type  geometry_columns.type%TYPE;

...

SELECT type
  INTO v_type 
  FROM geometry_columns 
  WHERE f_table_schema = 'public' 
    AND f_table_name = 'terrain_obstacle' 
    AND f_geometry_column = 'geom'
;

insert into public.terrain_obstacle (tse_coll, tse_height, geom)
  select tse_coll
        ,tse_height
        ,CASE WHEN v_type = 'MULTIPOLYGON' THEN my_func1(geom)
              WHEN v_type = 'POINT' THEN my_func2(geom)
              ELSE my_default(geom)
         END
    from public.terrain_obstacle_temp
;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Store result of function applications in a tuple inside a DO-block

From Dev

pass a query to row_to_json function inside a store procedure in postgres

From Dev

Store function result into variable

From Dev

Store function result into variable

From Dev

Store function inside an array

From Dev

Store function inside an array

From Dev

How do I store the result of a query inside a temporary table in a stored procedure?

From Dev

Store the result of a Dynamic Query in a variable

From Dev

How to store a query result in a variable

From Dev

MySQL Multi Query store result second query

From Dev

Store into variable result of function VBA

From Dev

Laravel: Return a database query result using the MySQL SUM function and store it in a variable

From Dev

MySQL function with query inside it

From Dev

How to extract the result of a query from JSON inside a custom javascript function in OrientDB

From Dev

Store and iterate over result of query in mysqli

From Dev

How to store query result (a single document) into a variable?

From Dev

store result of select query into array variable

From Dev

Store MYSQL result and use as IN statment in different query

From Dev

store the result of xpath into an variable to assist in future query

From Dev

Store a sql query result in pentaho variable

From Dev

store mysqli_query result in session

From Dev

Store database query result in an object in Python 3

From Dev

How to store MySQL query result in a string

From Dev

Athena Best Practice to store query result

From Dev

How to store SQL Query result in table column

From Dev

Can I store LINQ query result in an array?

From Dev

How to store and process result of a query in PHP array?

From Dev

Store MYSQL result and use as IN statment in different query

From Dev

how to store the query result into a variable in mysql

Related Related

  1. 1

    Store result of function applications in a tuple inside a DO-block

  2. 2

    pass a query to row_to_json function inside a store procedure in postgres

  3. 3

    Store function result into variable

  4. 4

    Store function result into variable

  5. 5

    Store function inside an array

  6. 6

    Store function inside an array

  7. 7

    How do I store the result of a query inside a temporary table in a stored procedure?

  8. 8

    Store the result of a Dynamic Query in a variable

  9. 9

    How to store a query result in a variable

  10. 10

    MySQL Multi Query store result second query

  11. 11

    Store into variable result of function VBA

  12. 12

    Laravel: Return a database query result using the MySQL SUM function and store it in a variable

  13. 13

    MySQL function with query inside it

  14. 14

    How to extract the result of a query from JSON inside a custom javascript function in OrientDB

  15. 15

    Store and iterate over result of query in mysqli

  16. 16

    How to store query result (a single document) into a variable?

  17. 17

    store result of select query into array variable

  18. 18

    Store MYSQL result and use as IN statment in different query

  19. 19

    store the result of xpath into an variable to assist in future query

  20. 20

    Store a sql query result in pentaho variable

  21. 21

    store mysqli_query result in session

  22. 22

    Store database query result in an object in Python 3

  23. 23

    How to store MySQL query result in a string

  24. 24

    Athena Best Practice to store query result

  25. 25

    How to store SQL Query result in table column

  26. 26

    Can I store LINQ query result in an array?

  27. 27

    How to store and process result of a query in PHP array?

  28. 28

    Store MYSQL result and use as IN statment in different query

  29. 29

    how to store the query result into a variable in mysql

HotTag

Archive