plpgsql

How to INSERT INTO table from dynamic query?

空扰寡人 提交于 2019-12-13 16:13:50
问题 My version of Postgres is: "PostgreSQL 9.4.4, compiled by Visual C++ build 1800, 32-bit" Let's say I have two tables Table1 and Table2 , which are having column col1 and col2 respectively. CREATE TABLE Table1(col1 int); CREATE TABLE Table2(col2 int); There is another table Table3 storing a formula for migrating data from Table1 to Table2 : CREATE TABLE Table3 ( tbl_src varchar(200), col_src varchar(500), tbl_des varchar(200), col_des varchar(100), condition varchar(500) ); INSERT INTO Table3

How to sort objects in an array inside a json or jsonb value by a property of the objects?

纵然是瞬间 提交于 2019-12-13 15:10:01
问题 I have this pl/pgsql function to aggregate rows from two tables in a jsonb value ( data_table_1 and data_table_2 ). fk_id is a common foreign key id in both tables: DECLARE v_my_variable_1 jsonb; v_my_variable_2 jsonb; v_combined jsonb; BEGIN SELECT json_agg( data_table_1 ) INTO v_my_variable FROM data_table_1 WHERE fk_id = v_id; SELECT json_agg( data_table_2 ) into v_my_variable_2 FROM data_table_2 WHERE fk_id = v_id; SELECT v_my_variable || v_my_variable_2 into v_combined; Now I want to

Cannot create plpgsql function using psql -f filename option

可紊 提交于 2019-12-13 09:46:58
问题 I am using Postgres 8.4 and I have tried to run a create function ... script from the command line using psql dbname -U username -f filename or psql -f filename -d dbname -U username and it always results in the following error psql:mergenodedata.sql:40: ERROR: syntax error at or near "create" LINE 1: create or replace FUNCTION updNode (oldnodename varchar, ne... where line 40 is the end of the file $$ LANGUAGE plpgsql; If I cut -and-paste file contents of the file into pgadmin or an open

how to reference a schema variable in plpgsql

故事扮演 提交于 2019-12-13 07:12:53
问题 I am trying to learn plpgsql code to automate some data cleaning in a database. My current task is to replace all of '999' values in numeric fields with 'NaN'. What I am trying to do is: 1) find all columns in a schema that are numeric 2) loop through these and use 'update/replace' My code is below. I think my main problem is finding out how to reference the schema.table in the update statement (but I am sure there are other things I have not done too well). The error that I am getting is

Returning empty data from dynamic pivot is there is no data

左心房为你撑大大i 提交于 2019-12-13 06:25:18
问题 Code below from how to preserve column names on dynamic pivot is used to create dynamic pivot table. If source table contains no data, sql error occurs since create table column list end with comma (there are no pivot columns). How to fix this so that empty table is returned ? To reproduce, remove insert commands insert into sales values ( '2016-1-1', 'Ø 12.3/3mm', 2); insert into sales values ( '2016-1-1', '+-3,4%/3mm', 52); insert into sales values ( '2016-1-3', '/3,2m-', 246); from code.

Run plpgsql program to update the data in table

自闭症网瘾萝莉.ら 提交于 2019-12-13 05:47:07
问题 I am using Postgres 8.4. I want to update the data from using plpgsql and a cursor. When I try to run the plpgsql it generates an error. CREATE OR REPLACE FUNCTION updateScore() RETURNS void AS $$ DECLARE singleTopicCriteriaPercentage DECIMAL(6,10); sitePercentage DECIMAL(6,10); singleSiteCriteriaPercentage DECIMAL(6,10); totalSocre DECIMAL(6,10); cursor1 CURSOR FOR select id from sitereviews order by id; cursor2 CURSOR FOR select weight into rating from sitereviews_ratingcriteria where site

Error: No function matches the given name and argument types

只谈情不闲聊 提交于 2019-12-13 04:56:58
问题 I try to get this function running. When I use pgadmin and manually call this function with SELECT calculate_something(7) or SELECT common.calculate_something(7) ERROR: function calculate_something(integer) doesn't exist hint no function matches the given name and argument types (translated from german) I already tried to cast the call SELECT calculate_something(cast(7 as bigint)); What is wrong with that function or cast? :/ CREATE OR REPLACE FUNCTION common.calculate_something(id bigint)

NOTICE: using pg_pltemplate information instead of CREATE LANGUAGE parameters

纵饮孤独 提交于 2019-12-13 04:44:00
问题 I am getting this error message (shown in title), when I run this SQL statement in PG 8.4: psql -h localhost -U postgres -d mydb -c "CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql' HANDLER plpgsql_call_handler VALIDATOR plpgsql_validator" What's causing this (it used to work with earlier versions of PG), and how do I resolve it? 回答1: This procedure has been simplified. For plpgsql simply use: CREATE LANGUAGE plpgsql; The error message tells you, that plpgsql is among a number of predefined

To update several non dupliated rows at one time using “Group by”

别来无恙 提交于 2019-12-13 04:19:50
问题 I followed the help from Generate a random number of non duplicated random number in [0, 1001] through a loop . But I can't apply that to my case. When I do: update weighed_directed_edge set endpoint= trunc(1000 * random()+ 1) from generate_series(1,10) group by 1 where startpoint= from_point; to update endpointId it complains: ERROR: syntax error at or near "group" LINE 1: ...nc(1000 * random()+ 1) from generate_series(1,10) group by 1. I also tried: insert into weighed_directed_edge

Multirow subselect as parameter to `execute using`

血红的双手。 提交于 2019-12-13 04:15:32
问题 The multirow subselect will be used in the right hand side of the in operator in the where clause: create table t (a integer); insert into t (a) values (1), (9); drop function if exists f(); create function f() returns void as $$ begin execute ' select a from t where a in $1 ' using (select 1 union select 2); end;$$ language plpgsql; select f(); ERROR: more than one row returned by a subquery used as an expression CONTEXT: SQL statement "SELECT (select 1 union select 2)" PL/pgSQL function "f"