dynamic-sql

Update multiple columns that start with a specific string

让人想犯罪 __ 提交于 2019-12-02 01:25:58
I am trying to update a bunch of columns in a DB for testing purposes of a feature. I have a table that is built with hibernate so all of the columns that are created for an embedded entity begin with the same name. I.e. contact_info_address_street1 , contact_info_address_street2 , etc. I am trying to figure out if there is a way to do something to the affect of: UPDATE table SET contact_info_address_* = null; If not, I know I can do it the long way, just looking for a way to help myself out in the future if I need to do this all over again for a different set of columns. There's no handy

Select a dynamic set of columns from a table and get the sum for each

旧街凉风 提交于 2019-12-01 22:34:44
Is it possible to do the following in Postgres: SELECT column_name FROM information_schema WHERE table_name = 'somereport' AND data_type = 'integer'; SELECT SUM(coulmn_name[0]),SUM(coulmn_name[1]) ,SUM(coulmn_name[3]) FROM somereport; In other words I need to select a group of columns from a table depending on certain criteria, and then sum each of those columns in the table. I know I can do this in a loop, so I can count each column independently, but obviously that requires a query for each column returned from the information schema query. Eg: FOR r IN select column_name from information

Use variable set by psql meta-command inside of DO block

大兔子大兔子 提交于 2019-12-01 21:35:39
Here's what I would like to do: \set values foo,bar,baz DO $$ DECLARE value TEXT; values TEXT[] := string_to_array(:'values', ','); BEGIN FOREACH value IN ARRAY values LOOP raise notice 'v: %', value; END LOOP; END $$ LANGUAGE plpgsql; Which results in the following error: ERROR: syntax error at or near ":" SELECT string_to_array(:'values', ',') INTO values... ^ Here's the solution I have currently, but it feels hacky: \set values foo,bar,baz PREPARE get_values AS SELECT string_to_array(:'values', ','); DO $$ DECLARE value TEXT; values TEXT[]; BEGIN EXECUTE 'EXECUTE get_values' INTO values;

How to get a result from dynamic SQL in Postgres?

折月煮酒 提交于 2019-12-01 19:43:22
Raw Table for which rule are stored in one table named md_formula , which are used to map in destination table Drop/Create/Insert for raw_dbs_transactiondetailscash : DROP TABLE raw_dbs_transactiondetailscash CREATE TABLE raw_dbs_transactiondetailscash( accountnumber VARCHAR(100), referencecurrency VARCHAR(100), transactiondate datetime) INSERT INTO raw_dbs_transactiondetailscash( accountnumber, referencecurrency, transactiondate) SELECT 'XYZ','$','01/01/2016' Drop/Create/Insert for md_formula : DROP TABLE MD_Formula CREATE TABLE MD_Formula ( Format VARCHAR(20), tbl_Src VARCHAR(200), Col_src

Run the same query against multiple tables without dynamic sql

旧城冷巷雨未停 提交于 2019-12-01 19:02:02
I support a SQL database for a third party software package. They have a lot of what they call "Shadow Tables", really just audit tables. This is all fine and good but their system does not clean up these tables so it is up to me to do so. They also add new "Shadow Tables" without notice with every upgrade. The old way we were purging the tables was with a long list of DELETE FROM statements but this list has become very long and hard to maintain. To try to make the purge process easier to maintain and automatically catch new "Shadow Tables" I wrote the following stored procedure. The stored

Calculate number of rows affected by batch query in PostgreSQL

时光毁灭记忆、已成空白 提交于 2019-12-01 18:53:32
First of all, yes I've read documentation for DO statement :) http://www.postgresql.org/docs/9.1/static/sql-do.html So my question: I need to execute some dynamic block of code that contains UPDATE statements and calculate the number of all affected rows. I'm using Ado.Net provider. In Oracle the solution would have 4 steps: add InputOutput parameter "N" to command add BEGIN ... END; to command add :N := :N + sql%rowcount after each statement. It's done! We can read N parameter from command, after execute it. How can I do it with PostgreSQL? I'm using npgsql provider but can migrate to devard

Run the same query against multiple tables without dynamic sql

一笑奈何 提交于 2019-12-01 17:27:28
问题 I support a SQL database for a third party software package. They have a lot of what they call "Shadow Tables", really just audit tables. This is all fine and good but their system does not clean up these tables so it is up to me to do so. They also add new "Shadow Tables" without notice with every upgrade. The old way we were purging the tables was with a long list of DELETE FROM statements but this list has become very long and hard to maintain. To try to make the purge process easier to

SQL query SELECT FROM [value from column of another table]

☆樱花仙子☆ 提交于 2019-12-01 11:20:38
I have a table X where a trigger will insert a row when there's a changes to some tables. I've inserted the table name into table X. Now, I would like to select the data from table X while inner join with the actual table itself. Is it possible by using a value from a column of the select table as the table for inner join? The query should looks something like this SELECT X.a, Y.b, Y.c FROM X INNER JOIN [X.TableName] AS Y ON Y.ID = X.ID Executing select 'SELECT X.a, Y.b, Y.c FROM X INNER JOIN [' + X.TableName + '] AS Y ON X.ID = Y.ID where x.primarykey =' + x.primarykey from x Will output a

To ignore result in BEFORE TRIGGER of PostgreSQL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 11:07:05
This thread is a part challenge of this thread to which I am searching a better solution for one part by BEFORE TRIGGER. I just want to launch a trigger to convert to correct brackets. I am thinking whether I should return from the trigger NULL or something else in before trigger. Code CREATE OR REPLACE FUNCTION insbef_events_function() RETURNS TRIGGER AS $func$ DECLARE m int[]; BEGIN FOREACH m SLICE 1 IN ARRAY TG_ARGV[0]::int[] LOOP INSERT INTO events (measurement_id, event_index_start, event_index_end) SELECT NEW.measurement_id, m[1], m[2]; -- Postgres array starts with 1 ! END LOOP; -- do

PLSQL dynamic query

混江龙づ霸主 提交于 2019-12-01 10:55:52
I have a table A which has column A which holds table names as values. All these tables have a common column C . I need maximum value of this column for each table. I tried this using dynamic SQL but I'm getting errors. Please suggest. DECLARE query1 VARCHAR2(100); c_table VARCHAR2(40); c_obj VARCHAR2(20); Cursor cursor_a IS SELECT a FROM A; BEGIN Open cursor_a; LOOP Fetch cursor_a INTO c_table2; EXIT WHEN cursor_a%notfound; query1 := 'SELECT max(object_ref) AS "c_obj" FROM c_table' ; EXECUTE IMMEDIATE query1; dbms_output.put_line('Maximum value: '|| c_table || c_obj); END LOOP; Close cursor_a