plpgsql

plpgsql CREATE FUNCTION syntax error at or near 'CREATE'

我的未来我决定 提交于 2019-12-25 08:05:14
问题 Can't seem to get this function to Create, even when I empty out all the DECLARE block and the BEGIN-END block and just try to return a string. Can anyone help me with what I've done wrong here? Trying to execute this in pgAdminIII (Ubuntu): CREATE OR REPLACE FUNCTION split_country() RETURNS text as $$ DECLARE titlecsv text; arCountries text[]; country_cursor CURSOR FOR SELECT DISTINCT country FROM festfriend.films; BEGIN OPEN country_cursor; LOOP FETCH NEXT FROM country_cursor INTO titlecsv;

PostgreSQL - dynamic INSERT on column names

痞子三分冷 提交于 2019-12-25 07:25:09
问题 I'm looking to dynamically insert a set of columns from one table to another in PostgreSQL. What I think I'd like to do is read in a 'checklist' of column headings (those columns which exist in table 1 - the storage table), and if they exist in the export table (table 2) then insert them in all at once from table 1. Table 2 will be variable in its columns though - once imported ill drop it and import new data to be imported with potentially different column structure. So I need to import it

Syntax error at or near ;

故事扮演 提交于 2019-12-25 04:26:40
问题 I have many trigger functions, and for some there is a strange error: " Syntax error at or near ; " Here is my code: CREATE OR REPLACE FUNCTION zajisti_vyplnenost() RETURNS trigger AS $$ BEGIN IF NEW.typ_vztahu != 1 THEN RETURN NEW; END IF; IF NEW.nad.typ_sj = 1 THEN IF NEW.nad.vrstva.vypln = true THEN ELSE RAISE EXCEPTION 'Totožné stratigrafické jednotky musejí být stejného typu!'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER zajisti_vyplnenost BEFORE INSERT OR UPDATE ON s

Postgres query to calculate matching strings

一曲冷凌霜 提交于 2019-12-25 04:24:38
问题 I have following table: id description additional_info 123 XYZ XYD And an array as: [{It is known to be XYZ},{It is know to be none},{It is know to be XYD}] I need to map both the content in such a way that for every record of table I'm able to define the number of successful match. The result of the above example will be: id RID Matches 1 123 2 Only the content at position 0 and 2 match the record's description / additional_info so Matches is 2 in the result. I am struggling to transform

How to execute plpgsql anonymous block in Oracle SQL Developer?

五迷三道 提交于 2019-12-25 04:11:59
问题 If I execute single SQL statements in worksheet (eg CREATE ROLE my_user LOGIN PASSWORD 'my_pwd' VALID UNTIL 'infinity'; ) then is works correctly. Wrapping it in anonymous block like this: DO $$ BEGIN CREATE ROLE my_user LOGIN PASSWORD 'my_pwd' VALID UNTIL 'infinity'; END $$; and I get following error message: Error starting at line : 3 in command - BEGIN CREATE ROLE my_user LOGIN PASSWORD 'my_pwd' VALID UNTIL 'infinity'; END $$; Error report - ERROR: syntax error at or near "CREATE" Position

How to organize parameters for a postgres application

风流意气都作罢 提交于 2019-12-25 03:28:15
问题 I am working on a postgres application. For the moment I am not sure how to manage application constant parameters best. For example I want to define a threshold variable which I am going to use in several functions. One idea is making a table "config" and query the variable every time I need them. And for a shortcut wrap the sql query into an other function i.e.: t := get_Config('Threshold'); But in fact I am not really lucky with this. What is the best way to handle custom application

How to convert this T-SQL code to PostgreSQL / plpgsql

时间秒杀一切 提交于 2019-12-25 02:44:09
问题 I need to convert the following tsql function code into a plpgsql function and I have absolutely no idea how: BEGIN DECLARE @StartDate DATETIME DECLARE @ResultDate DATETIME SET @StartDate = CONVERT(DATETIME, 0) SET @ResultDate = CASE @Type WHEN 0 THEN DATEADD(mi, FLOOR(DATEDIFF(mi, @StartDate, @Date) / CAST(@Interval AS FLOAT)) * @Interval, @StartDate) WHEN 1 THEN DATEADD(mi, CEILING(DATEDIFF(mi, @StartDate, @Date) / CAST(@Interval AS FLOAT)) * @Interval, @StartDate) ELSE @Date END RETURN

Looping through a postgres table and returning the wekid

≡放荡痞女 提交于 2019-12-25 02:29:51
问题 I have a requirement to loop through a table having all the reporting periods right from 2010. I need to loop through this table so that i when I pass the date string, it takes at and compares it if it is less than the reporting_periods_o and returns back the reporting_period_string. CREATE OR REPLACE FUNCTION rpt.reportingperiods(IN v_date text) RETURNS SETOF text AS $BODY$ DECLARE v_in_date INT := CAST(v_date AS INT); i INT; v_sk INT; BEGIN For i IN SELECT rpt_sk FROM rpt.REPORTING_PERIODS

Dynamic table name for INSERT INTO query

自作多情 提交于 2019-12-25 01:17:20
问题 I am trying to figure out how to write an INSERT INTO query with table name and column name of the source as parameter. For starters I was just trying to parametrize the source table name. I have written the following query. For now I am declaring and assigning the value of the variable tablename directly, but in actual example it would come from some other source/list. The target table has only one column. CREATE OR REPLACE FUNCTION foo() RETURNS void AS $$ DECLARE tablename text; BEGIN

Using text in pl/pgsql brings empty set of results

痞子三分冷 提交于 2019-12-25 00:53:08
问题 I am new to pl/pgsql and trying to create a dynamic query. What I have now is a basic combination of parameters for testing. When it works properly, I will gradually add more dynamic parts, to create a dynamic, all-in-one query. The problem is that this should work, but instead I see an empty search_creator as Data Output in the pgadmin4. This is the code CREATE FUNCTION search_creator(creator text) RETURNS TABLE(place_id bigint, place_geom geometry, event_name character(200)) AS $$ BEGIN