plpgsql

Postgresql dynamic function with current table name

依然范特西╮ 提交于 2019-12-12 02:26:01
问题 I have a function ( audit.create_audit_table() ) that accepts an array of table names. It creates a single function audit.if_modified_func() and then loops through each table name and creates an audit table and applies a trigger to the main table. The function compiles and is created with no errors. When I run the function select audit.create_audit_table(ARRAY['organization']) I keep getting the following error and I am not sure why because I thought that TG_TABLE_NAME is a automatic variable

How to pass a parameter into a date function

我是研究僧i 提交于 2019-12-12 01:25:02
问题 I am trying to create a simple function and I cannot seem to pass in a parameter into the date function. Here is the function: CREATE OR REPLACE FUNCTION test(source int,days text) RETURNS integer AS $totals$ declare totals integer; BEGIN select count(id) into totals from ad where createdate::date = date(current_date - interval '$2' day) and source = $1; RETURN totals; END; $totals$ LANGUAGE plpgsql; 回答1: @IMSoP already shed light upon your syntax error. However, this can be simpler, faster

Sanitize input to a column in postgres

≯℡__Kan透↙ 提交于 2019-12-12 00:46:49
问题 So, I think this should be fairly simple, but the documentation makes it seem somewhat more complicated. I've written an SQL function in PostgreSQL (8.1, for now) which does some cleanup on some string input. For what it's worth, the string is an LDAP distinguished name, and I want there to consistently be no spaces after the commas - and the function is clean_dn(), which returns the cleaned DN. I want to do the same thing to force all input to another couple of columns to lower case, etc -

PostgreSQL stored procedure using iBatis

瘦欲@ 提交于 2019-12-11 23:53:08
问题 The error occurred while applying a parameter map: --- Check the newSubs-InlineParameterMap. --- Check the statement (query failed). --- Cause: org.postgresql.util.PSQLException: ERROR: wrong record type supplied in RETURN NEXT Where: PL/pgSQL function "getnewsubs" line 34 at return next the function detail is as below.... CREATE OR REPLACE FUNCTION getnewsubs(timestamp without time zone, timestamp without time zone, integer) RETURNS SETOF record AS $BODY$declare v_fromdt alias for $1; v_todt

plpgsql function not inserting data as intended

点点圈 提交于 2019-12-11 18:13:27
问题 I have the below function compiled successfully. When I do select schema.funtion_name(); , the function gets executed but there are no rows inserted in the table schema.table_insert : CREATE OR REPLACE FUNCTION schema.function_name() RETURNS void AS $BODY$ DECLARE cur_1 CURSOR FOR Select col1 from schema.table1 union select col1 from schema.table2 union select col1 from schema.table3 union select col1 from schema.table4; BEGIN FOR rec_i in cur_1 LOOP insert into schema.table_insert (col1,col2

migration oracle to postgresql invalid byte sequence for encoding “UTF8”: 0x00

天大地大妈咪最大 提交于 2019-12-11 16:58:03
问题 Im migrating an application from oracle to postgresql. In one of the functions that I already migrated I copy data from a different oracle db (db link in oracle, oracle_fdw extension in postgresql) from a few tables into a local table in my postgresql db. However, I`m getting the next error : invalid byte sequence for encoding "UTF8": 0x00 I saw that some people had this kind of issue in this forum but they didnt try to copy the data directly from a remote database (they loaded data from a

Postgres: How to overload functions having single record type input parameter

假装没事ソ 提交于 2019-12-11 16:56:54
问题 I have a user defined type: create type match_input as ( _values text[], _name text, _norm_fn text, _operator text ); and I use this as the input parameter for a function: get_matches(mi match_input) I want to be able to call the same function, but passing a single text value for _values. So I defined a new type: create type match_input_simple as ( _values text, _name text, _norm_fn text, _operator text ); If I try and overload the function with the following: create or replace function get

PostgreSQL sql-state: 42601 on SELECT INTO

落爺英雄遲暮 提交于 2019-12-11 16:47:45
问题 Well, i'm trying to create a simple procedure, that check if user with such login, and if no - adding new row in users table. But stuck with unexpected problem. CREATE OR REPLACE FUNCTION register_user(character varying, character varying, character varying,character varying,character varying) RETURNS bigint AS $BODY$ DECLARE new_user_login ALIAS FOR $1; new_user_password ALIAS FOR $2; new_user_email ALIAS FOR $3; new_user_first_name ALIAS FOR $4; new_user_last_name ALIAS FOR $5; login_exist

Postgres split LineStrings and convert

情到浓时终转凉″ 提交于 2019-12-11 14:48:13
问题 my question is how to build a function in PL/pgSQL to do this : i have many linestrings like this. Linestring((3.584731 60.739211,3.590472 60.738030,3.592740 60.736220)) I need to split every Linestring in a Substring and split it up again in 2 coordinates like this. 3.584731 60.739211 x1 y1 3.590472 60.738030 x2 y2 3.592740 60.736220 x3 y3 And so on with the other points. Save the answers and converting into a double so I can calculate with the points. 回答1: Use ST_DumpPoints like this:

Can someone explain this SQL? (and how may I 'parametrize' it and invoke as a function?)

落花浮王杯 提交于 2019-12-11 14:28:48
问题 I have come across this piece of 'voodoo' SQL which is being used to perform custom grouping of data from a table. I would like to understand how it does it magic, but I am unable to grok it. Can a SQL expert out there explain in simple English to someone who is not very SQL literate, the various parts of this snippet, which allows it to do its magic? select ceil(rnk/10.0) as grp, col1, col2, col3, col4, col5, col6, col7 from (select e.col1, e.col2, e.col3, e.col4, e.col5, e.col6, e.col7,