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

PREVIOUS NEXT
Code Example
Sql :: oracle select json_table example 
Sql :: Expression number 1 of select list is not in group by clause 
Sql :: Write an SQL query to fetch worker names with salaries = 50000 and <= 100000. 
Sql :: Assign value to variable inside Dynamic SQL 
Sql :: update value sql 
Sql :: sql latlng 
Sql :: sqlite commands 
Sql :: SQL Error [42501]: ERROR: permission denied for table 
Sql :: how to select month from date in sql 
Sql :: mysql add hours to time field 
Sql :: express mysql 
Sql :: find a column by name in a sql server table 
Sql :: sql compiler online 
Sql :: Select with remove white spaces in sql 
Sql :: oracle tablespace usage 
Sql :: mysql delete from where like 
Sql :: how to get max salary in each department in sql 
Sql :: mysql decimal 
Sql :: split string and get first and last element in sql server 
Sql :: mssql describe stored procedure sqlcmd 
Sql :: mysql search replace 
Sql :: postgresql powershell query 
Sql :: sql 2 way of select unique 
Sql :: difference between in and between in sql 
Sql :: missing left parenthesis error in sql 
Sql :: plpgsql coalesce equivalent for empty string 
Sql :: mysql custom sort order 
Sql :: insert data into multiple tables mysql 
Sql :: exclude last comma separated string mysql 
Sql :: oracle insert from select 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =