ERROR: syntax error at or near "END" when creating function - postgresql

ERROR: syntax error at or near "END"
LINE 9: END;
^
SQL state: 42601
Character: 350
===========================
My procedure
CREATE OR REPLACE PROCEDURE proc_insert(empid bigint,ename character varying(256),
email character varying(256),enum bigint,
eadd bigint)
language 'plpgsql'
as $$
BEGIN
insert into proc_insert(empid,ename,email,enum,eadd)
END;
$$;

Missing semicolon after the command.
CREATE OR REPLACE PROCEDURE proc_insert(empid bigint
, ename varchar
, email varchar
, enum bigint
, eadd bigint)
LANGUAGE plpgsql AS
$proc$
BEGIN
INSERT INTO proc_insert -- add target column list!
VALUES(empid,ename,email,enum,eadd);
END
$proc$;
(While the one after END is optional.)
Plus, your INSERT statement was broken anyway. I fixed it, but you better add a target column list, too. See:
Cannot create stored procedure to insert data: type mismatch for serial column

You are getting this error because your insert query syntax is incorrect.
create or replace PROCEDURE proc_insert(empid bigint,ename character varying(256),
email character varying(256),enum bigint,
eadd bigint)
LANGUAGE plpgsql AS $$
declare
begin
insert into proc_insert values(empid,ename,email,enum,eadd);
end
$$;
Demo in DBfiddle

Related

psql pgadmin Procedure error column "trip" does not exist LINE 7: CALL example2(Trip)

I want to create a stored procedure in pgadmin that will output the number of rows from the "Trip" table. The table itself is output in cmd, the number of rows in this table is also output in cmd. But when writing a procedure and calling it, such an error comes out. I have psql version 15. How can I fix this error?
My code:
CREATE PROCEDURE example2(INOUT _name character varying) AS $$
BEGIN
SELECT count(*) FROM "_name";
END;
$$ LANGUAGE plpgsql;
CALL example2(Trip)
Error:
ERROR: ERROR: The "trip" column does not exist
LINE 7: CALL example2(Trip)
You are better off using a function then a procedure as it is easier to get value out. Using dynamic SQL along with the format function to properly quote the _name parameter.
CREATE OR REPLACE FUNCTION public.example2(_name character varying)
RETURNS integer
LANGUAGE plpgsql
AS $function$
DECLARE
ct integer;
BEGIN
EXECUTE format('SELECT count(*) FROM %I', _name) INTO ct;
RETURN ct;
END;
$function$
select example2('animals');
example2
----------
8

query has no destination for result data i am facing this error

PostgreSQL 14
TABLE
CREATE TABLE IF NOT EXISTS settings.tbl_tmp
(
pk_sys_qr_settings_id bigint NOT NULL DEFAULT nextval('settings.tbl_tmp_pk_sys_qr_settings_id_seq'::regclass),
sin_product smallint DEFAULT 0,
vhr_key character varying(10) COLLATE pg_catalog."default" NOT NULL,
vhr_value character varying(250) COLLATE pg_catalog."default",
CONSTRAINT tbl_tmp_pkey PRIMARY KEY (pk_sys_qr_settings_id),
CONSTRAINT tbl_tmp_vhr_key_key UNIQUE (vhr_key)
)
--FUNCTION 1 - CALL FROM fn_test
CREATE OR REPLACE FUNCTION fn_test1(sinProduct SMALLINT,
vhrKey VARCHAR(10),
vhrValue VARCHAR(250))
RETURNS VOID AS $$
BEGIN
INSERT INTO settings.tbl_tmp
(sin_product, vhr_key, vhr_value)
VALUES
(sinProduct, vhrKey, vhrValue);
END;
$$ LANGUAGE plpgsql;
FUNCTION 2
CREATE OR REPLACE FUNCTION fn_test(sinProduct SMALLINT,
vhrKey VARCHAR(10),
vhrValue VARCHAR(250))
RETURNS VOID AS $$
DECLARE
a SMALLINT;
BEGIN
BEGIN
SELECT * FROM fn_test1(1::SMALLINT, 'Anil-5'::VARCHAR(10), 'KV-5'::VARCHAR(250));
INSERT INTO settings.tbl_tmp
(sin_product, vhr_key, vhr_value)
VALUES
(sinProduct, vhrKey, vhrValue);
END;
END;
$$ LANGUAGE plpgsql;
--ERROR
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function fn_test(smallint,character varying,character varying) line 7 at SQL statement
SQL state: 42601
Quote from the manual
Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement:
As the called function doesn't return anything to begin with, it's safe to use perform.
So use:
perform fn_test1(1::SMALLINT, 'Anil-5'::VARCHAR(10), 'KV-5'::VARCHAR(250));
Instead of select * from fn_test(..)
As you are using Postgres 14, you could also use a procedure in both cases, as you don't want to return a result.

postgres procedure syntax error near »end«

I am facing an issue when I try to run my procudere. I have a table where I have all values stored in text format and try to convert to int or timestamp
CREATE OR REPLACE PROCEDURE public.textnummerictest(
)
LANGUAGE 'sql'
AS $BODY$
create or replace procedure textnummerictest()
language plpgsql
as $$
begin
insert into sensorhistory_test(
datetime,
sensorid )
select from sensorhistory_temp (
cast(sensorid as integer),
datetime::timestamp )
end; $$
$BODY$;
ALTER PROCEDURE public.textnummerictest()
OWNER TO postgres;
Always getting the error Syntaxerror near end.
Any help really appreciated
I found the error....
there must be a ';' before end...

function does not exists in postgreSQL .. Why ?

Need your help please , can't understand why i got the following error , i am not a professional postgresql developer ..
As you can see the function created , so why the function not exist occurred ?
create or replace function loginAttempt (u_email character varying, u_password character varying, date_time timestamptz, OUT attempt smallint) returns smallint AS $$
BEGIN
INSERT INTO login_attempts (typed_password, date_time, attempt_nu, email) VALUES (u_password, date_time, attempt_nu, email);
IF attempt = 3 THEN INSERT INTO warnings (u_email,u_password) VALUES (u_email,u_password);
END IF;
END;
$$ LANGUAGE plpgsql;
select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987', now(), 1);
ERROR: function loginattempt(unknown, unknown, timestamp with time zone, integer) does not exist
LINE 1: select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987',...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 8
You have defined the last parameter as an OUT parameter, that means you can't pass a value for it.
You need to use:
select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987', now());
As you are not writing to the parameter attempts I don't see a reason to define it as an out parameter to begin with. You can simply return the value if you need it:
create or replace function loginAttempt (u_email character varying, u_password character varying, u_date_time timestamptz, u_attempt smallint)
returns smallint
AS $$
BEGIN
INSERT INTO login_attempts (typed_password, date_time, attempt_nu, email)
VALUES (u_password, u_date_time, u_attempt, u_email);
IF u_attempt = 3 THEN
INSERT INTO warnings (u_email,u_password) VALUES (u_email,u_password);
END IF;
return u_attempt;
END;
$$ LANGUAGE plpgsql;
As the value 1 is assumed to be an integer, you need to cast that value when calling the function:
select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987', now(), 1::smallint);
Online example: https://rextester.com/YNIQ55561

write a function in pl/pgsql

This is my query.
select origindept, `count(am_course_name)` as total_course
from am_courseoffered
group by origindept;
I am trying to create a function who will return this query.
CREATE OR REPLACE FUNCTION getcourse ()
RETURNS TABLE (
course_origindept character varying,
course_ count(am_course_name) character varying
)
AS $$
BEGIN
RETURN QUERY select origindept, count(am_course_name) as number_total_course
from am_courseoffered
group by origindept;
END; $$
LANGUAGE 'plpgsql';
There are some error in my function.
ERROR: syntax error at or near "character"
LINE 4: course_ count(am_course_name) character varying
How i create function who will return this query.
Off the top of my head, the count function should return a datatype of bigint, so I would think declaring the count as a varchar would cause a datatype mismatch. Something like this should fix that:
CREATE OR REPLACE FUNCTION getcourse ()
RETURNS TABLE (
course_origindept character varying,
course_count bigint -- change here
) AS $$
BEGIN
RETURN QUERY
select origindept, count(am_course_name) as number_total_course
from am_courseoffered
group by origindept;
END;
$$
LANGUAGE plpgsql;
I do have to ask, though... is this an academic exercise to understand functions? The use case is questionable enough, as a view would be more appropriate.