plpgsql

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

穿精又带淫゛_ 提交于 2020-01-14 13:12:04
问题 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 find all combinations (subset) of any size of an array in postgresql [duplicate]

本秂侑毒 提交于 2020-01-14 03:08:08
问题 This question already has an answer here : Array combinations without repetition (1 answer) Closed 5 years ago . Given an array, how to find all combinations (subsets) of elements of a certain size in postgresql. For example, given an array [1, 2, 3, 4] all combinations of size 3 would be [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4] Order is not important in combinations and therefore [1, 2, 3] and [3, 2, 1] are considered the same combination. Update: The size of the combinations required must

Updating integer column from jsonb member fails with: column is of type integer but expression is of type jsonb

泪湿孤枕 提交于 2020-01-13 22:52:38
问题 In a PostgreSQL 9.5 table I have an integer column social . When I try to update it in a stored procedure given the following JSON data (an array with 2 objects, each having a "social" key) in the in_users variable of type jsonb : '[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum, Germany","female":0,"stamp":1450102770}, {"sid"

Updating integer column from jsonb member fails with: column is of type integer but expression is of type jsonb

最后都变了- 提交于 2020-01-13 22:52:21
问题 In a PostgreSQL 9.5 table I have an integer column social . When I try to update it in a stored procedure given the following JSON data (an array with 2 objects, each having a "social" key) in the in_users variable of type jsonb : '[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum, Germany","female":0,"stamp":1450102770}, {"sid"

Iterating through PostgreSQL records. How to reference data from next row?

↘锁芯ラ 提交于 2020-01-13 13:13:17
问题 I'm new to PostgreSQL and writing functions here is tough as nails. So I'm hoping someone can help let me know how to do what I'm trying to do. I have a table of stock prices and dates. I want to calculate the percent change from the previous day for each entry. For the earliest day of data, there won't be a previous day, so that entry can simply be Nil. Can someone look over my function and help me with a) how to reference data from the next row and b) help me clean it up? I'm aware that the

Iterating through PostgreSQL records. How to reference data from next row?

那年仲夏 提交于 2020-01-13 13:10:11
问题 I'm new to PostgreSQL and writing functions here is tough as nails. So I'm hoping someone can help let me know how to do what I'm trying to do. I have a table of stock prices and dates. I want to calculate the percent change from the previous day for each entry. For the earliest day of data, there won't be a previous day, so that entry can simply be Nil. Can someone look over my function and help me with a) how to reference data from the next row and b) help me clean it up? I'm aware that the

How to md5 all columns regardless of type

大兔子大兔子 提交于 2020-01-12 18:46:47
问题 I would like to create a sql query (or plpgsql) that will md5() all given rows regardless of type. However, below, if one is null then the hash is null: UPDATE thetable SET hash = md5(accountid || accounttype || createdby || editedby); I am later using the hash to compare uniqueness so null hash does not work for this use case. The problem was the way it handles concatenating nulls. For example: thedatabase=# SELECT accountid || accounttype || createdby || editedby FROM thetable LIMIT 5;

Session based global variable in Postgresql stored procedure?

此生再无相见时 提交于 2020-01-12 14:30:09
问题 In Oracle's PL/SQL I can create a session based global variable with the package definition. With Postgresql's PLpg/SQL, it doesn't seem possible since there are no packages, only independent procedures and functions. Here is the syntax for PL/SQL to declare g_spool_key as a global... CREATE OR REPLACE PACKAGE tox IS g_spool_key spool.key%TYPE := NULL; TYPE t_spool IS REF CURSOR RETURN spool%ROWTYPE; PROCEDURE begin_spool; PROCEDURE into_spool ( in_txt IN spool.txt%TYPE ); PROCEDURE reset

PostgreSQL drop constraint with unknown name

…衆ロ難τιáo~ 提交于 2020-01-11 05:11:45
问题 I have an SQL script that needs to drop several constraints and restore them at the end, but the constraint names are auto-generated and will be different each time the script is run. I know how to get the constraint name from the table names, but it doesn't seem possible to use this information in the drop statement. select conname from pg_constraint where conrelid = (select oid from pg_class where relname='table name') and confrelid = (select oid from pg_class where relname='reference table

Update record of a cursor where the table name is a parameter

我怕爱的太早我们不能终老 提交于 2020-01-11 02:36:19
问题 I am adjusting some PL/pgSQL code so my refcursor can take the table name as parameter. Therefore I changed the following line: declare pointCurs CURSOR FOR SELECT * from tableName for update; with this one: OPEN pointCurs FOR execute 'SELECT * FROM ' || quote_ident(tableName) for update; I adjusted the loop, and voilà, the loop went through. Now at some point in the loop I needed to update the record (pointed by the cursor) and I got stuck. How should I properly adjust the following line of