plpgsql

Create procedure to execute query in PostgreSQL

狂风中的少年 提交于 2019-12-11 14:08:33
问题 Maybe it's not new case, but I'm stack about it. This is the procedure that I use to run query, it run normally in MySQL, but not in PostgreSQL and I don't know how to do that. The procedure(in MySQL) looks like : CREATE PROCEDURE runstatement(IN statement TEXT) BEGIN set @s = statement; IF LENGTH(@s) <> 0 THEN PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt; END IF; END The questions : How do I convert it to PostgreSQL version? How do I call this procedure(runstatement) when I

How Can I Call PL/pgSQL Function From C++ Code

谁都会走 提交于 2019-12-11 13:45:09
问题 I am trying to call a function which is declared in PostgreSQL with PL/pgSQL. For that I write the code below. My function is working but after that I am taking a "PGRES_FATAL_ERROR". Also when I changed "select removestopwords()" with an sql query like "DELETE * FROM TABLE1" it's working successfully. I am considering, that error can cause some big problem in future even if now working. How can I call a PL/pgSQL function without taking error? void removeStopWordsDB(PGconn* conn) { PGresult

Copy records with dynamic column names

大憨熊 提交于 2019-12-11 13:22:24
问题 I have two tables with different columns in PostgreSQL 9.3: CREATE TABLE person1( NAME TEXT NOT NULL, AGE INT NOT NULL ); CREATE TABLE person2( NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); INSERT INTO person2 (Name, Age, ADDRESS, SALARY) VALUES ('Piotr', 20, 'London', 80); I would like to copy records from person2 to person1 , but column names can change in program, so I would like to select joint column names in program. So I create an array containing the

Using query to set the column type in PostgreSQL

元气小坏坏 提交于 2019-12-11 12:53:40
问题 After the excellent answer by Alexandre GUIDET, I attempted to run the following query: create table egg (id (SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) as Datatype FROM pg_catalog.pg_attribute a WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = ( SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ '^(TABLENAME)$' AND pg_catalog.pg_table_is_visible(c.oid) ) and a.attname = 'COLUMNNAME')); PostgreSQL,

Postgres pl/java caveats [closed]

試著忘記壹切 提交于 2019-12-11 11:26:19
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I would very much prefer writing DB side code in Java but would like to know if doing so has any disadvantages compared to pl/pgsql (especially performance & error handling related). 回答1: As of my expirience pl/java has some major issues: It's difficult to install it into a

Sum across partitions with window functions

二次信任 提交于 2019-12-11 11:12:22
问题 I have the following problem... Time | A | B | C -- Sum should be 1 a1 b1 c1 a1 + b1 + c1 2 a2 b2 x a2 + b1 + c1 3 a3 x x a3 + b2 + c1 4 x b3 c2 a3 + b3 + c2 Essentially, the sum needs to be across the most recent value in time for each of the three rows. Each data column doesn't necessarily have a value for the current time. I have tried several approaches using window functions and have been unsuccessful. I have written a stored procedure that does what I need, but it is SLOW. CREATE OR

How to replace all subsets of characters based on values of other tables in pl/pgsql?

混江龙づ霸主 提交于 2019-12-11 11:00:38
问题 I've been doing some research on how to replace a subset of string of characters of a single row base on the values of the columns of other rows, but was not able to do so since the update are only for the first row values of the other table. So I'm planning to insert this in a loop in a plpsql function. Here are the snippet of my tables. Main table: Table "public.tbl_main" Column | Type | Modifiers -----------------------+--------+----------- maptarget | text | expression | text | maptarget

Select row cells as new columns

a 夏天 提交于 2019-12-11 10:44:58
问题 Basically, I have a table that stores column names with some restrictions: infos , and another one that stores the values for those columns: info_data . I want to get a table which has the columns from infos and data from info_data . I've tried with crosstab function but it doesn't have the desired effect. I have 2 tables: CREATE TABLE infos (id serial PRIMARY KEY, name text NOT NULL, id_member integer NOT NULL, title text, min_length integer NOT NULL DEFAULT 0, max_length integer NOT NULL

plpgsql function: Return rows from a view created from random table

邮差的信 提交于 2019-12-11 10:44:02
问题 I want to create a function that returns rows from a view created from a unknown table(s): CREATE OR REPLACE FUNCTION tt_query(text, timestamp without time zone) RETURNS SETOF record AS $$ DECLARE orig_name ALIAS FOR $1; data_tt ALIAS FOR $2; BEGIN [...] EXECUTE 'create OR REPLACE TEMP view temp as select * from ' ||orig_name ||' where trigger_changed >' ||quote_literal(data_tt) ||' ORDER BY trigger_changed DESC'; [...]--other work on view temp --NOW I WANT RETURN THE ROW OF view temp END; $$

SQL Injection-safe call of polymorphic function

孤街醉人 提交于 2019-12-11 10:36:54
问题 Several times I've found myself refactoring web application code and end up wanting to do something like this (Groovy in this case, but could be anything): Map getData(String relationName, Integer rowId) { def sql = Sql.newInstance([...]) def result = sql.firstRow('SELECT getRelationRow(?,?)', relationName, rowId) sql.close() return new HashMap(result) } where the stored procedure getRelationRow(relname text, rowid integer) executes dynamic sql to retrieve the row of the specified rowid in