Search
 
SCRIPT & CODE EXAMPLE
 

SQL

create function in postgresql

create [or replace] function function_name(param_list)
   returns return_type 
   language plpgsql
as $$
declare 
-- variable declaration
begin
 -- logic
end;
$$
Comment

postgresql function

Create or replace Function public.my_function(p_el1 int, p_el2 int, p_name char)
Returns table (id int, price int)
language plpgsql

as
$$
	declare
		v_total int;
	
	begin
		-- insert into first table
		insert into my_table1
			(added_name)
		values
			(p_name);

		-- Insert the result of a calculation in a variable
		select (p_el1 + p_el2) into v_total;
	
		-- Update a second table
		update my_table2 mt
		set
			price = v_total
		where
			mt.name = p_name;
		
		-- Return the result of a query
		return query (select
							mt.id,
							mt.price
						from
							my_table2 mt
						where
							mt.name = p_name);
		
	end;
$$

Comment

function in postgresql

CREATE OR REPLACE FUNCTION totalRecords ()
RETURNS integer AS $total$
declare
	total integer;
BEGIN
   SELECT count(*) into total FROM COMPANY;
   RETURN total;
END;
$total$ LANGUAGE plpgsql;
Comment

create function postgresql

CREATE OR REPLACE FUNCTION auditlogfunc() RETURNS TRIGGER AS $example_table$
   BEGIN
      INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, current_timestamp);
      RETURN NEW;
   END;
$example_table$ LANGUAGE plpgsql;
Comment

postgresql functions

CASE
     WHEN condition_1  THEN result_1
     WHEN condition_2  THEN result_2
     ...
     ELSE  result_n
END
Comment

PREVIOUS NEXT
Code Example
Sql :: sql delete duplicate rows 
Sql :: mysql add to value 
Sql :: @firebase/database: FIREBASE WARNING: update at /users failed: permission_denied 
Sql :: sql select where clause 
Sql :: psql no such file or directory 
Sql :: How to backup databases using psql 
Sql :: download sql server for mac 
Sql :: image for MSSQL Windows Docker 
Sql :: how to print sql query 
Sql :: change permission to database mysql 
Sql :: union vs intersect sql 
Sql :: oracle sql sort ascending 
Sql :: How to import CSV file into a MySQL table 
Sql :: mysql remove unique key 
Sql :: consecutive numbers sql 
Sql :: postgres windows import dump 
Sql :: oracle avg 
Sql :: mysql create table index 
Sql :: alter column to not null with default value sql server 
Sql :: SQL Error [42501]: ERROR: permission denied for table 
Sql :: mysql find db contarint 
Sql :: find a column by name in a sql server table 
Sql :: sql view index 
Sql :: how to start my sql server on mac 
Sql :: sql distinct 
Sql :: psql show db 
Sql :: mysql unique constraint 
Sql :: sql table backup 
Sql :: SELECT everything from a sql table 
Sql :: mysql date_format 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =