I'm trying to write an UPSERT statement to insert or update a row in a PostgresSQL database containing a geometry column. My input is a KML fragment and the following statement works for me as long as the KML is valid.
UPDATE area SET shape = ST_GeomFromKML('{the KML}') WHERE area_code = '{the area}';
INSERT INTO area(area_code, shape) SELECT '{the area}', ST_GeomFromKML('{the KML}') WHERE NOT EXISTS (SELECT 1 FROM area WHERE area_code = '{0}');
In case it's relevant I am calling this from a C# ASP.NET MVC application using a SqlCommand object, but that shouldn't matter as long as the SQL statement is correct.
The changes I want are to use ST_IsValid and ST_MakeValid to ensure that the column is correct. Unfortunately my recent database experience is mostly SQL Server with a little MySQL and PostgresSQL statements don't appear to handle variables in the same way.
What I would like is for something like:
DECLARE #shape geometry;
SELECT #shape = ST_GeomFromKML('{the KML}');
IF NOT (ST_IsValid(#shape)) SELECT #shape = ST_MakeValid(#shape);
UPDATE area SET shape = #shape WHERE area_code = '{the area}';
INSERT INTO area(area_code, shape) SELECT '{the area}', #shape WHERE NOT EXISTS (SELECT 1 FROM area WHERE area_code = '{0}');
so that I am checking validity and correcting it once in the code. However, even after reading the documentation, I don't understand how to use variables to do this.
In PostgreSQL you need to write a stored procedure in the PL/pgSQL language (assuming that your kml fragment and "the area" are strings):
CREATE FUNCTION myFunc(kml text, zip text) RETURNS void AS $$
DECLARE shp geometry;
BEGIN
shp := ST_GeomFromKML(kml);
IF NOT (ST_IsValid(shp)) THEN
shp := ST_MakeValid(shp);
END IF;
UPDATE area SET shape = shp WHERE area_code = zip;
INSERT INTO area (area_code, shape)
SELECT zip, shp
WHERE NOT EXISTS (SELECT 1 FROM area WHERE area_code = zip);
END;
$$ LANGUAGE plpgsql;
Documentation on PL/pgSQL can be found here.
Related
I want to use a loop to calculate the distance traveled between two nodes 152 and 17720 (I use the function pgr_dijkstra) by deleting each time a cell. A cell contains several road links. the grid_edges_routard table contains the road links and the corresponding cell.
Iwant to have for each blocked cell the distance traveled between the two nodes.
I must use pgr_dijkstra to display in a second time the links traveled.
CREATE OR REPLACE FUNCTION get_dist_grid()
RETURNS TABLE (
celref_blocked INT,
dist INT
) AS $$
DECLARE
var_r record;
BEGIN
FOR var_r IN(SELECT distinct(cellule)as cel from grid_edges_routard )
LOOP
SELECT * FROM pgr_dijkstra('SELECT id, source, target,cost
FROM road_routard.edges_vulnerabilite
where id not in (select edge_id
from grid_edges_routard
where cellule=var_r) ',152 ,17720, FALSE)
where edge=-1;
celref_blocked := var_r.cel ;
RETURN NEXT;
END LOOP;
END; $$
LANGUAGE 'plpgsql';
select get_dist_grid()
I have an error message: ERROR: column « var_r » does not exist.
I use postgresql 9.5.
Define a new variable (var_q) of type record, then Execute your select query into your defined variable like this Execute 'SELECT * FROM pgr_dijkstra(''SELECT id, source, target,cost FROM road_routard.edges_vulnerabilite where id not in (select edge_id from grid_edges_routard where cellule='||var_r||') '',152 ,17720, FALSE) where edge=-1' into var_q
This might give some errors as we have to escape the quotes for inner query, Try escaping quotes if it doesn't work and then you can use the out of the query in similar way as you have used celref_blocked := var_r.cel
I'm defining a simple stored procedure in DB2 as follows but it gives me syntax error
CREATE OR REPLACE PROCEDURE Schema1.TESTSP1 ()
DYNAMIC RESULT SETS 1
P1: BEGIN
if( exists(
select 1 from syscat.tables where tabschema = 'Schema' and tabname = 'SPTEST'
)) then
drop table Schema.SPTEST ;
create table Schema.SPTEST as
(select * from Schema.XYZ) WITH DATA ;
end if;
END P1
What is wrong here?
You need to study the IBM example stored procedures for SQL PL.
Get the sample procedures working in your environment to build knowledge and skills.
You need to understand the difference between dynamic-sql and static-SQL and when to use either one.
You need to add exception handlers for any exceptions.
As you specified that it returns one result-set you need to open a cursor on that final result-set.
The examples below are for the Db2-LUW Version 11.1.3.3 CLP (command line processor), i.e. on Microsoft Windows that is the what runs when you open db2cwadmin.bat, and on Linux/Unix the bash/ksh shell:
Note: the WITH DATA requires Db2-LUW 11.1.3.3, older versions supported only WITH NO DATA. If you cannot upgrade, then use instead 'CREATE TABLE ... LIKE ...' followed by a separate statement INSERT INTO ... SELECT ...FROM ...
Here is a skeleton resembling your example, using static SQL on Db2-LUW V11.1.3.3:
--#SET TERMINATOR #
CREATE OR REPLACE PROCEDURE Schema1.TESTSP1 ()
DYNAMIC RESULT SETS 1
P1: BEGIN
DECLARE SQLCODE integer;
DECLARE table_exists integer default 0;
select 1 into table_exists from syscat.tables where tabschema = 'SOMESCHEMA' and tabname = 'SPTEST';
if table_exists = 1
then
drop table SOMESCHEMA.SPTEST ;
create table SOMESCHEMA.SPTEST as (select * from SCHEMA.SPTEST ) with data ;
end if;
END P1
#
Here is a skeleton which resembles your example but uses dynamic-SQL on Db2-LUW V11.1.3.3, which would be useful if you don't know in advance the table/column names and they are available in parameters to the stored procedure (not shown) in which case you can dynamically build the SQL and execute it:
--#SET TERMINATOR #
CREATE OR REPLACE PROCEDURE Schema1.TESTSP1 ()
DYNAMIC RESULT SETS 1
P1: BEGIN
DECLARE SQLCODE integer;
DECLARE table_exists integer default 0;
select 1 into table_exists from syscat.tables where tabschema = 'SOMESCHEMA' and tabname = 'SPTEST';
if table_exists = 1
then
execute immediate('drop table SOMESCHEMA.SPTEST') ;
execute immediate('create table SOMESCHEMA.SPTEST as (select * from SCHEMA.SPTEST ) with data') ;
end if;
END P1
#
I have a trigger function that is called by several tables when COLUMN A is updated, so that COLUMN B can be updated based on value from a different function. (More complicated to explain than it really is). The trigger function takes in col_a and col_b since they are different for the different tables.
IF needs_updated THEN
sql = format('($1).%2$s = dbo.foo(($1).%1$s); ', col_a, col_b);
EXECUTE sql USING NEW;
END IF;
When I try to run the above, the format produces this sql:
($1).NameText = dbo.foo(($1).Name);
When I execute the SQL with the USING I am expecting something like this to happen (which works when executed straight up without dynamic sql):
NEW.NameText = dbo.foo(NEW.Name);
Instead I get:
[42601] ERROR: syntax error at or near "$1"
How can I dynamically update the column on the record/composite type NEW?
This isn't going to work because NEW.NameText = dbo.foo(NEW.Name); isn't a correct sql query. And I cannot think of the way you could dynamically update variable attribute of NEW. My suggestion is to explicitly define behaviour for each of your tables:
IF TG_TABLE_SCHEMA = 'my_schema' THEN
IF TG_TABLE_NAME = 'my_table_1' THEN
NEW.a1 = foo(NEW.b1);
ELSE IF TG_TABLE_NAME = 'my_table_2' THEN
NEW.a2 = foo(NEW.b2);
... etc ...
END IF;
END IF;
First: This is a giant pain in plpgsql. So my best recommendation is to do this in some other PL, such as plpythonu or plperl. Doing this in either of those would be trivial. Even if you don't want to do the whole trigger in another PL, you could still do something like:
v_new RECORD;
BEGIN
v_new := plperl_function(NEW, column_a...)
The key to doing this in plpgsql is creating a CTE that has what you need in it:
c_new_old CONSTANT text := format(
'WITH
NEW AS (SELECT (r).* FROM (SELECT ($1)::%1$s r) s)
, OLD AS (SELECT (r).* FROM (SELECT ($2)::%1$s r) s
'
, TG_RELID::regclass
);
You will also need to define a v_new that is a plain record. You could then do something like:
-- Replace 2nd field in NEW with a new value
sql := c_new_old || $$SELECT row(NEW.a, $3, NEW.c) FROM NEW$$
EXECUTE sql INTO v_new USING NEW, OLD, new_value;
I have this code, to add trigger for a View in my postgresql database
CREATE OR REPLACE FUNCTION changeCityProc() RETURNS TRIGGER AS $changeCity$
BEGIN
UPDATE vvs.tb_company SET city = "LAL" WHERE cardpresso.cardinfo.tb_company_city = vvs.tb_company.city;
RETURN null;
END;
$changeCity$ LANGUAGE plpgsql;
CREATE TRIGGER mytrigger
INSTEAD OF UPDATE ON
cardpresso.cardinfo FOR EACH ROW EXECUTE PROCEDURE changeCityProc();
pgAdmin says "syntax error at or near "INSTEAD""
Let's simplify this. First, the two schemas.
CREATE SCHEMA cardpresso;
CREATE SCHEMA vvs;
Next, the simplest possible table.
CREATE TABLE vvs.tb_company
(
city character varying(25)
);
INSERT INTO vvs.tb_company VALUES ('foo');
And a fairly useless view.
CREATE VIEW cardpresso.cardinfo AS
SELECT 'test' as tb_company_city UNION ALL
SELECT 'Orem' UNION ALL
SELECT 'Wibble'
;
Simplify the function. Warning: This will destructively update the table. Be careful if you copy stuff from this function into something headed for production.
CREATE OR REPLACE FUNCTION changecityproc()
RETURNS trigger AS
$BODY$
BEGIN
UPDATE vvs.tb_company SET city = 'bar';
RETURN null;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Finally, the trigger.
CREATE TRIGGER mytrigger
INSTEAD OF UPDATE
ON cardpresso.cardinfo
FOR EACH ROW
EXECUTE PROCEDURE changecityproc();
Now, if we update the view, we'd expect all the rows in vvs.tb_company to change to 'bar'.
select * from vvs.tb_company;
city
--
foo
update cardpresso.cardinfo
set tb_company_city = 'wibble';
select * from vvs.tb_company;
city
--
bar
So it looks like the problem is in the UPDATE statement of your function.
UPDATE vvs.tb_company SET city = "LAL"
WHERE cardpresso.cardinfo.tb_company_city = vvs.tb_company.city;
I'm not certain what you intended that statement to do, but it's not valid in PostgreSQL, regardless of whether "LAL" is supposed to be a column name or a string literal. As a string literal, 'LAL', it will raise an error.
ERROR: missing FROM-clause entry for table "cardinfo"
LINE 2: WHERE cardpresso.cardinfo.tb_company_city = vvs.tb_compa...
i have a variable that is a name of a table. How can i select or update from this using variable in query , for example:
create or replace function pg_temp.testtst ()
returns varchar(255) as
$$
declare
r record; t_name name;
begin
for r in SELECT tablename FROM pg_tables WHERE schemaname = 'public' limit 100 loop
t_name = r.tablename;
update t_name set id = 10 where id = 15;
end loop;
return seq_name;
end;
$$
language plpgsql;
it shows
ERROR: relation "t_name" does not exist
Correct reply is a comment from Anton Kovalenko
You cannot use variable as table or column name in embedded SQL ever.
UPDATE dynamic_table_name SET ....
PostgreSQL uses a prepared and saved plans for embedded SQL, and references to a target objects (tables) are deep and hard encoded in plans - a some characteristics has significant impact on plans - for one table can be used index, for other not. Query planning is relatively slow, so PostgreSQL doesn't try it transparently (without few exceptions).
You should to use a dynamic SQL - a one purpose is using for similar situations. You generate a new SQL string always and plans are not saved
DO $$
DECLARE r record;
BEGIN
FOR r IN SELECT table_name
FROM information_schema.tables
WHERE table_catalog = 'public'
LOOP
EXECUTE format('UPDATE %I SET id = 10 WHERE id = 15', r.table_name);
END LOOP;
END $$;
Attention: Dynamic SQL is unsafe (there is a SQL injection risks) without parameter sanitization. I used a function "format" for it. Other way is using "quote_ident" function.
EXECUTE 'UPDATE ' || quote_ident(r.table_name) || 'SET ...