plpgsql

Dynamically execute query using the output of another query

别等时光非礼了梦想. 提交于 2020-01-21 16:57:08
问题 I have a function called generate_table, that takes 2 input parameters ( rundate::date and branch::varchar ) Now I am trying to work on a second function, using PLPGSQL, that will get a list of all branches and the newest date for each branch and pass this as a parameter to the generate_table function. The query that I have is this: select max(rundate) as rundate, branch from t_index_of_imported_files group by branch and it results on this: rundate;branch 2014-03-13;branch1 2014-03-12;branch2

Dynamically execute query using the output of another query

回眸只為那壹抹淺笑 提交于 2020-01-21 16:56:50
问题 I have a function called generate_table, that takes 2 input parameters ( rundate::date and branch::varchar ) Now I am trying to work on a second function, using PLPGSQL, that will get a list of all branches and the newest date for each branch and pass this as a parameter to the generate_table function. The query that I have is this: select max(rundate) as rundate, branch from t_index_of_imported_files group by branch and it results on this: rundate;branch 2014-03-13;branch1 2014-03-12;branch2

how to preserve column names on dynamic pivot

笑着哭i 提交于 2020-01-17 06:19:29
问题 Sales data contains dynamic product names which can contian any characters. Dynamic pivot table is created based on sample from Crosstab with a large or undefined number of categories translate() is used to remove bad characters. In result pivot table column names are corrupted: missing characters and spaces are removed. How to return data with same column names as in source data ? I tried to use quote_ident(productname) as tootjakood, instead of 'C'||upper(Translate(productname,'Ø. &/+-,%',

Python Postgres Best way to insert data from table on one DB to another table on another DB

折月煮酒 提交于 2020-01-17 05:50:10
问题 I have the following python code that copies content of a table on postgres DB1 and INSERTS into a similar table on postgres DB2. I want to speed it up by using BULK INSERTS. How do I achieve this import psycopg2 import sys import os all_data = [] try: connec = psycopg2.connect("host = server1 dbname = DB1 ") connecc = psycopg2.connect("host = server2 dbname = DB2 ") connec.autocommit = True connecc.autocommit = True except: print("I am unable to connect to the database.") cur = connec.cursor

Array not being returned by SELECT query inside plpgsql test case

一笑奈何 提交于 2020-01-16 05:03:08
问题 SELECT query I am using: SELECT ARRAY[table_name,pg_size_pretty(table_size)] FROM ( SELECT table_name, pg_table_size(table_name) AS table_size, pg_indexes_size(table_name) AS indexes_size, pg_total_relation_size(table_name) AS total_size FROM ( SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name FROM information_schema.tables where table_schema not in ('pg_catalog', 'information_schema')and table_schema not like 'pg_toast%' ) AS all_tables ORDER BY total_size DESC ) AS

@@Fetch_status in postgreSQL

Deadly 提交于 2020-01-15 12:11:13
问题 I'm transfering my database from MS SQL Server to PostgreSQL and I have a problem with this trigger: CREATE TRIGGER added_clients ON client FOR INSERT AS BEGIN DECLARE cursor_inserted CURSOR FOR SELECT name, surname FROM inserted; OPEN cursor_inserted DECLARE @name varchar(15), @surname varchar(20) FETCH NEXT FROM cursor_inserted INTO @name, @surname WHILE @@FETCH_STATUS = 0 BEGIN print 'Added: '+@name+' '+ @surname FETCH NEXT FROM cursor_inserted INTO @name, @surname END CLOSE cursor

Escaping a LIKE pattern or regexp string in Postgres 8.4 inside a stored procedure

Deadly 提交于 2020-01-15 07:04:58
问题 I am writing a stored procedure to find the sub-items of a main item and update it. The scenario is like this Id Item Name Parent Id 1 Item A 14.1 NULL 2 Item B 14.1.1 1 3 Item C 14.1.2 1 4 Item B 14.1.3 1 5 Item A 14.1.1.1 2 I have posted another question on SO String matching in PostgreSQL 8.4 to get the children of an item. According to that answer i have to escape the item code and have to use it in the query. But I haven't got any method to escape that. The SP is as below: CREATE OR

Update column in multiple tables

风格不统一 提交于 2020-01-15 05:36:09
问题 Let's say I have a column called partner in multiple tables within one schema: select table_name from information_schema.columns where column_name = 'partner'; How would I update all columns where value partner = 100 to partner = 101 ? 回答1: For a one-time operation, a DO statement executing dynamic SQL should serve just fine: DO $do$ DECLARE _tbl text; BEGIN FOR _tbl IN SELECT quote_ident(table_name) -- escape identifier! FROM information_schema.columns WHERE table_schema = 'public' -- your

How to select from variable that is a table name n Postgre >=9.2

痞子三分冷 提交于 2020-01-14 13:14:11
问题 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 回答1: Correct

How to select from variable that is a table name n Postgre >=9.2

拟墨画扇 提交于 2020-01-14 13:14:05
问题 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 回答1: Correct