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 :: oracle drop sequence if exists 
Sql :: oracle drop sequence 
Sql :: test the postgresql db connection 
Sql :: sqlalchemy update row 
Sql :: check database size in gb mysql 
Sql :: create delete procedure mysql 
Sql :: sql create database 
Sql :: launch sql script from docker in mysql 
Sql :: sql extract from mail 
Sql :: alter column set not null to null postgres 
Sql :: sql injection payload list github 
Sql :: query to count the number of rows in a table in sqlalchemy 
Sql :: raiserror nowait sql server 
Sql :: mysql multiple order by 
Sql :: mysql order by multiple columns 
Sql :: error code 1215 cannot add foreign key constraint 
Sql :: sql server select last row of each item in group by column 
Sql :: oracle convert run duration to number 
Sql :: drop CHECK constraint sql 
Sql :: cast datetime to date in sql 
Sql :: mysql url data type 
Sql :: select random sample sql 
Sql :: sql in 
Sql :: snowflake select from stage 
Sql :: mysql auto increment column 
Sql :: oracle session statistics 
Sql :: postgres data location 
Sql :: oracle default date format 
Sql :: sql practice 
Sql :: pl sql search in all packages 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =