Error type casts Postgresql function - sql

Hello i have this error
Error: psql:/Samsung/VisualCodeStudioWorkspace/1524335164735.pgsql:95: ERROR: function altaplayer(character varying, integer, integer, character varying, character varying) does not exist
LINE 1: select altaPlayer( varchar 'Drazen Petrovic',52,80, varchar ...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
psql end.
I cant solve for more than two days... ive tried everyhing...
CREATE TYPE player AS (
nom varchar(20),
alçada integer,
pes integer,
posicio varchar(20),
equip varchar(20)
);
CREATE TABLE player_list of player(
primary key(nom)
);
CREATE TABLE team (
jugador player
);
INSERT INTO team VALUES(row('Pepelu',190,90,'Base','Barça'));
INSERT INTO player_list VALUES('Asterix',50,30,'Base','Barça');
INSERT INTO player_list VALUES('Pepelui',100,90,'Alero','Barça');
INSERT INTO player_list VALUES('Manolo',10,50,'Base','Barça');
INSERT INTO player_list VALUES('Juan',50,30,'Base','Barça');
INSERT INTO player_list VALUES('Fernando Martin',50,30,'Pivot','Barça');
INSERT INTO player_list VALUES('Audie Norris',50,30,'Pivot','Barça');
create or replace function altaPlayer ( varchar(20),int,int,varchar(20),varchar(20)) as $$
BEGIN
INSERT INTO player_list VALUES($1,$2,$3,$4,$5);
END
$$ LANGUAGE plpgsql;
select altaPlayer('Drazen Petrovic',52,80,'Escolta','Cibona');

The function result type must be specified:
create or replace function altaplayer ( varchar(20),int,int,varchar(20),varchar(20))
returns void -- !!!
as $$
begin
insert into player_list values($1,$2,$3,$4,$5);
end
$$ language plpgsql;

You missed to put the keyword RETURNS.
create or replace function altaPlayer
(varchar(20),int,int,varchar(20),varchar(20))
RETURNS void as $$

Related

How to put argument (table name) of function?

I am using PostgreSQL 14.4 . My script
-- 0.
DROP TABLE IF EXISTS tenant;
CREATE TABLE tenant
(
id smallint primary key,
company_tax_code character varying(14),
period character varying(16), -- 2021070420220705
created timestamp with time zone
);
CREATE OR REPLACE FUNCTION set_id_tenant()
RETURNS trigger AS
$$
DECLARE
BEGIN
new.id = (select coalesce(max(id), -32769) from tenant) + 1;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER trigger_insert_without_id_tenant
BEFORE INSERT
ON tenant
FOR EACH ROW
EXECUTE PROCEDURE set_id_tenant();
CREATE INDEX tenant_idx ON tenant (id);
COMMIT;
--------------------------------------------------------------------------------
-- 4.
DROP TABLE IF EXISTS account_object_bank_account;
CREATE TABLE account_object_bank_account
(
id smallint,
account_object_id smallint not null,
bank_account character varying(64),
bank_name character varying(128),
bank_id smallint,
sort_order smallint,
bank_branch_name character varying(256),
province character varying(128),
tenant_id smallint,
PRIMARY KEY (id, tenant_id),
CONSTRAINT fk_tenant FOREIGN KEY (tenant_id) REFERENCES tenant (id)
);
CREATE OR REPLACE FUNCTION account_object_bank_account_setId()
RETURNS trigger AS
$$
DECLARE
BEGIN
new.id = (select coalesce(max(id), -32769) from account_object_bank_account where tenant_id = new.tenant_id) + 1;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER account_object_bank_account_trig_insertWithoutId
BEFORE INSERT
ON account_object_bank_account
FOR EACH ROW
EXECUTE PROCEDURE account_object_bank_account_setId();
CREATE INDEX account_object_bank_account_idx ON account_object_bank_account (id, tenant_id);
COMMENT ON TABLE public.account_object_bank_account IS 'Bảng lưu tài khoản ngân hàng của khách hàng';
COMMENT ON COLUMN public.account_object_bank_account.id IS 'PK';
COMMENT ON COLUMN public.account_object_bank_account.account_object_id IS 'FK';
COMMENT ON COLUMN public.account_object_bank_account.bank_account IS 'Số TK ngân hàng';
COMMENT ON COLUMN public.account_object_bank_account.bank_name IS 'Tên ngân hàng';
COMMIT;
--------------------------------------------------------------------------------
-- 5.
DROP TABLE IF EXISTS account_object_belong_to_group;
CREATE TABLE account_object_belong_to_group
(
id smallint,
account_object_id smallint,
account_object_group_id smallint,
tenant_id smallint,
PRIMARY KEY (id, tenant_id),
CONSTRAINT fk_tenant FOREIGN KEY (tenant_id) REFERENCES tenant (id)
);
CREATE OR REPLACE FUNCTION account_object_belong_to_group_setId()
RETURNS trigger AS
$$
DECLARE
BEGIN
new.id = (select coalesce(max(id), -32769) from account_object_belong_to_group where tenant_id = new.tenant_id) + 1;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER account_object_belong_to_group_trig_insertWithoutId
BEFORE INSERT
ON account_object_belong_to_group
FOR EACH ROW
EXECUTE PROCEDURE account_object_belong_to_group_setId();
CREATE INDEX account_object_belong_to_group_idx ON account_object_belong_to_group (id, tenant_id);
COMMIT;
--------------------------------------------------------------------------------
I want re-use this custom function, then put argument of function is table name:
account_object_bank_account_setId()
account_object_belong_to_group_setId()
to
set_id( table_name )
argument sample: account_object_bank_account, account_object_belong_to_group.
How to do?
update your 2 triggers with set_id('table_name')
and use TG_ARGV[] to retrieve the parameter in the trigger function
https://www.postgresql.org/docs/14/plpgsql-trigger.html

PostgreSQL syntax error at end of input not sure why

CREATE OR REPLACE FUNCTION customerGetByLargestSpend()
RETURNS TABLE(
customerID INTEGER,
firstName VARCHAR(20),
Surname VARCHAR(40),
totalBookings BIGINT,
totalSpend Numeric
)
ERROR: syntax error at end of input
LINE 8: )
^
SQL state: 42601
Character: 213
Not sure why this is happening.Any help would be very appreciated. thanks
The function is defined to return a row set, but does not return anything.
The parser expects to find something that returns rows, eg a select statement.
For example:
CREATE OR REPLACE FUNCTION customerGetByLargestSpend()
RETURNS TABLE(
customerID INTEGER,
firstName VARCHAR(20),
Surname VARCHAR(40),
totalBookings BIGINT,
totalSpend Numeric
)
AS $$ SELECT .... $$
LANGUAGE SQL

how to create function in postgres

how to create function in PostgreSQL am using below code
create table sample(id int primary key)
create table sample2(id int primary key)
create view sampleall
As
select id from sample
union
select id from sample2
while creating below function for above tables
Create FUNCTION CheckFunction (ID INT)
RETURNS BIT AS
$$
BEGIN
IF EXISTS (SELECT 1 FROM sampleall WHERE id = #ID)
BEGIN
RETURN 1
END
RETURN 0
END
$$
LANGUAGE plpgsql;
CREATE TABLE sample3 (ID INT NOT NULL CONSTRAINT CHK_ID CHECK (dbo.CheckFunction(ID) = 1))
and getting below error
ERROR: syntax error at or near "BEGIN"
LINE 8: BEGIN
^
********** Error **********
ERROR: syntax error at or near "BEGIN"
SQL state: 42601
Character: 132
please help to solve the issue
As documented in the manual an IF requires a THEN. Additionally, parameters cannot be prefixed with a #. To avoid a name clash between a parameter name and a column name you should use a different name for the parameter. A common approach is to prefix parameters with p_
However your function is needlessly complex and can be simplified substantially if you return a proper boolean rather than a bit:
Create FUNCTION check_function (p_ID INT)
RETURNS boolean AS
$$
select exists (SELECT 1 FROM sampleall WHERE id = p_id);
$$
LANGUAGE sql;
Your check constraint can then be simplified to:
CREATE TABLE sample3 (ID INT NOT NULL CONSTRAINT CHK_ID CHECK (check_Function(ID));

Can I use an SQL statement in assignment inside a PL/pgSQL function?

I've these two tables (Encomenda and Informacaofaturacao) and I'm trying to create a trigger to insert a new line on Informacaofaturacao before insert on Encomenda and put the ID of new line of Informacaofaturacao on new line of Encomenda.
What am I doing wrong?
Thanks
CREATE TABLE Encomenda
(
EncomendaID SERIAL,
ClienteID integer NOT NULL,
MoradaFaturacaoID integer NOT NULL,
MoradaEnvioID integer NOT NULL,
InformacaofaturacaoID integer NULL,
Data timestamp NOT NULL,
Estado EstadoEncomenda NOT NULL DEFAULT 'Em processamento',
CONSTRAINT PK_Encomenda PRIMARY KEY (EncomendaID)
)
;
CREATE TABLE Informacaofaturacao
(
InformacaofaturacaoID SERIAL,
MetodopagamentoID integer NULL,
Portes real NULL,
Iva real NULL,
Total real NULL,
CONSTRAINT PK_Informacaofaturacao PRIMARY KEY (InformacaofaturacaoID)
)
;
CREATE OR REPLACE FUNCTION insert_encomenda()
RETURNS TRIGGER
AS $$
BEGIN
NEW.InformacaofaturacaoID := (INSERT INTO Informacaofaturacao RETURNING InformacaofaturacaoID);
RETURN NEW;
END $$ LANGUAGE plpgsql;
CREATE TRIGGER insert_encomenda_trigger
BEFORE INSERT OR UPDATE ON Encomenda
FOR EACH ROW
EXECUTE PROCEDURE insert_encomenda();
Postgres doesn't accept a data modifying SQL statement (INSERT, UPDATE or DELETE) in assignment. The documentation states:
... the expression in such a statement is evaluated by means of an SQL SELECT command sent to the main database engine.
You should use this form of executing a query with a single-row result instead.
Also, INSERT command must have VALUES part, it can be: DEFAULT VALUES, VALUES(...) or query see the syntax in the documentation.
CREATE OR REPLACE FUNCTION insert_encomenda()
RETURNS TRIGGER
AS $$
BEGIN
INSERT INTO Informacaofaturacao(InformacaofaturacaoID)
VALUES(DEFAULT)
RETURNING InformacaofaturacaoID
INTO NEW.InformacaofaturacaoID;
RETURN NEW;
END $$ LANGUAGE plpgsql;

audit trigger postgres

I want to store my old records in a table when something has changed. I tried to do it with this documentation. But i still get a error. The error is: relation serie doesn't exist.
The original table:
CREATE TABLE "Terra_nub_v1"."Serie"
(
uuid uuid NOT NULL, -- t.b.v. pakbon
serie_nr integer NOT NULL,
aantal_slijpplaten integer,
producent text,
locatie text,
klaar boolean,
onderzocht boolean,
gerapporteerd boolean,
opmerking text,
verwijzing_hoofdrapportage text,
verwijzing_overige_rapportages text,
onderzoeksgebied polygon,
CONSTRAINT "Serie_pkey" PRIMARY KEY (serie_nr)
)
The archive table:
CREATE TABLE "Terra_nub_v1".serie_history
(
uuid uuid NOT NULL,
serie_nr integer NOT NULL,
aantal_slijpplaten integer,
producent text,
locatie text,
klaar boolean,
onderzocht boolean,
gerapporteerd boolean,
opmerking text,
verwijzing_hoofdrapportage text,
verwijzing_overige_rapportages text,
onderzoeksgebied polygon,
datumtijd_wijziging timestamp without time zone,
aangepast_door text,
CONSTRAINT serie_history_pkey PRIMARY KEY (serie_nr)
)
I make the trigger function this way:
CREATE OR REPLACE FUNCTION "Terra_nub_v1".serie_history_trigger()
RETURNS trigger AS $BODY$
BEGIN
IF (TG_OP ='UPDATE') THEN
INSERT INTO Terra_nub_v1.serie_history SELECT OLD.*,NOW(), USER ;
RETURN OLD;
ELSEIF (TG_OP = 'DELETE') THEN
INSERT INTO Terra_nub_v1.serie_history SELECT OLD.*,NOW(), USER ;
RETURN OLD;
END IF;
RETURN NULL;
END;
$BODY$ LANGUAGE plpgsql VOLATILE;
Create Trigger serie_history_trigger
BEFORE UPDATE OR DELETE ON Terra_nub_v1.serie
FOR EACH ROW EXECUTE PROCEDURE process_emp_audit();
I m using postgres
Greetings