1

I'm trying to create a function that allows me to perform multiple Queries in my database. It's the first time that I try to use the command line for this objective. I'm working in a linux system (Ubuntu), using the PostgreSQL.

My Pgsql:

create function my_function(idteste integer, nome character varying, tipo integer, morada character varying, cpostal character varying, telefone character varying, nif character varying, nacionalidade character varying, iban character varying, moradaimag character varying, cc1 character varying, ccimage1 character varying, ccimage2 character varying, datanascimento date)
returns character varying as $tempvar$
declare tempvar character varying;
begin
tempvar := 'null';
if (select count(id_teste) from singulares where id_idteste=idteste) = 0 then
update pessoas set  
nome_pessoal=nome,
tipo_pesoal = tipo,     
morada_pessoal=morada,
cpostal_pessoal=cpostal,
telefone_pessoal=telefone,
nif_pessoal=nif,
nacionalidade_pessoal=nacionalidade,
iban_pessoal=iban,
cmorada_img=cmoradaimag
where
id_teste=idteste;
insert into singulares (pessoas_idteste, cc, cc_img1, cc_img2, data_nascimento) values(idteste, cc1, ccimage1, ccimage2, datanascimento);
tempvar := 'gravado com sucesso';
else
update pessoas set  
nome_pessoal=nome,
tipo_pesoal = tipo,     
morada_pessoal=morada,
cpostal_pessoal=cpostal,
telefone_pessoal=telefone,
nif_pessoal=nif,
nacionalidade_pessoal=nacionalidade,
iban_pessoal=iban,
cmorada_img=cmoradaimag
where
id_teste=idteste;
update singulares set 
cc=cc1,
cc_img1=ccimage1,
cc_img2=ccimage2
where 
pessoas_idteste=idteste;
tempvar := 'alterado com sucesso';
end if;
return tempvar; 
end
$tempvar$  language plpgsql; 

My first question is how can I create the function through the command line?

Second question when creating the function, will this be visible from PGmyadmin?

1 Answer 1

5

Assuming your function is stored in a script file create_my_function.sql and you're to create it in database testdb owned by user testuser, here's one way to create it:

psql -h localhost -U testuser -d testdb -f create_my_function.sql

Not sure I understand your second question, but once the function is created it should be visible in any working db admin tool.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help.Your way works, but if we copy the function directly to the command line, it works the way I wanted it to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.