plpgsql

Postgresql Create Trigger Before Deleting A Row

不问归期 提交于 2020-01-01 19:37:10
问题 so i have these two tables: Table user columns: id,name,surname, password,token,earnedmoney Table addlisting columns: id, user_fk,price,date_added Here is my problem: I would like to create a trigger so that when I delete a listing from the table addlisting, the price of the listing gets added to the column "earnedmoney" which is in the table user. Could somebody help me? Thank you! 回答1: CREATE OR REPLACE FUNCTION add_money() RETURNS trigger AS $$BEGIN UPDATE "user" SET earnedmoney =

Generate HTML from PostgreSQL function

做~自己de王妃 提交于 2020-01-01 14:35:55
问题 Can anyone help me with this? I have a task to write a function, which would generate HTML tables from given table name in PostgreSQL(plpgsql language). I have written this, but it's far from what I need. It would generate a table for columns I would give (at the moment just one), but I need to just give the table a name. CREATE OR REPLACE FUNCTION genhtml2(tablename text, columnname text) RETURNS text AS $BODY$ DECLARE result text := ''; searchsql text := ''; var_match text := ''; BEGIN

Generate HTML from PostgreSQL function

十年热恋 提交于 2020-01-01 14:35:06
问题 Can anyone help me with this? I have a task to write a function, which would generate HTML tables from given table name in PostgreSQL(plpgsql language). I have written this, but it's far from what I need. It would generate a table for columns I would give (at the moment just one), but I need to just give the table a name. CREATE OR REPLACE FUNCTION genhtml2(tablename text, columnname text) RETURNS text AS $BODY$ DECLARE result text := ''; searchsql text := ''; var_match text := ''; BEGIN

Constraints and Assertions in PostgreSQL

喜你入骨 提交于 2020-01-01 12:11:46
问题 I am trying to create a simple database where I have a table of customer data and a table of order data. I am trying to write a constraint that makes it so a customer can't order more than a specific amount of items on a given day. Here's what I have: CREATE TABLE CUSTOMER ( CUSTOMER_NUM CHAR(3) PRIMARY KEY, CUSTOMER_NAME CHAR(35) NOT NULL, STREET CHAR(15), CITY CHAR(15), STATE CHAR(3), ZIP CHAR(5), ); CREATE TABLE ORDERS ( ORDER_NUM CHAR(5) PRIMARY KEY, ORDER_DATE DATE, CUSTOMER_NUM CHAR(3),

PostgreSQL vs Oracle: “compile-time” checking of PL/pgSQL

一世执手 提交于 2020-01-01 05:11:13
问题 Executive summary: PostgreSQL is amazing, but we are facing many issues at work due to the fact that it postpones many checks on PL/pgSQL code until runtime. Is there a way to make it more like Oracle's PL/SQL in this respect ? For example... Try executing this in any Oracle DB: create function foo return number as begin select a from dual; return a; end; Oracle will immediately (i.e. at compile-time !) respond with: [Error] ORA-00904: invalid identifier Now try the semantically equivalent

How to use PostgreSQL triggers?

我怕爱的太早我们不能终老 提交于 2020-01-01 03:54:06
问题 I am trying to use PostgreSQL triggers in my rails app. So I tried using this migration where execution of triggers is supposedly easy: -- class AddTriggersToProducts < ActiveRecord::Migration def self.up table :products execute %q{ create trigger trig1 before insert on products for each row begin price = price + 5 end; } end def self.down execute 'DROP TRIGGER trig1' end end But this didn't change anything. I don't know where to write the procedure or function if I am going to use one here .

plpgsql: calling a function with 2 OUT parameters

不问归期 提交于 2019-12-31 20:43:07
问题 I'm trying to fetch to values from a plpgsql function with 2 OUT paramenters but I have some problem. These are the functions: CREATE OR REPLACE FUNCTION get_test(OUT x text, OUT y text) AS $$ BEGIN x := 1; y := 2; END; $$ LANGUAGE plpgsql; ---------------------------------------------------------------- CREATE OR REPLACE FUNCTION get_test_read() RETURNS VOID AS $$ DECLARE xx text; yy text; BEGIN SELECT get_test() INTO xx, yy; RAISE INFO 'x: <%>', xx; RAISE INFO 'y: <%>', yy; END; $$ LANGUAGE

plpgsql: calling a function with 2 OUT parameters

荒凉一梦 提交于 2019-12-31 20:40:11
问题 I'm trying to fetch to values from a plpgsql function with 2 OUT paramenters but I have some problem. These are the functions: CREATE OR REPLACE FUNCTION get_test(OUT x text, OUT y text) AS $$ BEGIN x := 1; y := 2; END; $$ LANGUAGE plpgsql; ---------------------------------------------------------------- CREATE OR REPLACE FUNCTION get_test_read() RETURNS VOID AS $$ DECLARE xx text; yy text; BEGIN SELECT get_test() INTO xx, yy; RAISE INFO 'x: <%>', xx; RAISE INFO 'y: <%>', yy; END; $$ LANGUAGE

Are PL/pgSQL and SQL in PostgreSQL both at the same level as SQL/PSM standard, instead of as SQL standard only? [closed]

馋奶兔 提交于 2019-12-31 05:11:48
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . Terminology: In the following, there are two kinds of languages understood by PostgreSQL server PL/pgSQL; I use both "PostgreSQL SQL" and "SQL in PostgreSQL" to refer to the same thing, the default language of the commands received by PostgreSQL server. "PostgreSQL SQL" i.e. "SQL in

PostgreSQL generate_series() with SQL function as arguments

老子叫甜甜 提交于 2019-12-30 11:39:10
问题 I have a SQL function called get_forecast_history(integer,integer) that takes two arguments, a month and a year. The function returns a CUSTOM TYPE created with: CREATE TYPE fcholder AS (y integer, m integer, product varchar, actual real); The first line of the function definition is: CREATE OR REPLACE FUNCTION get_forecast_history(integer, integer) RETURNS SETOF fcholder AS $$ Calling: SELECT * FROM get_forecast_history(10, 2011); For example produces the following table (the result type of