postgres procedure syntax error near »end« - postgresql

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...

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 when try to make search filter [duplicate]

I am trying to fetch data from remote db by using dblink through function but getting an error "query has no destination for result data". I am using plpgsql language to do the same.
Function:
CREATE OR REPLACE FUNCTION fun()
RETURNS text AS
$$
begin
select dblink_connect(
'port=5432 dbname=test user=postgres password=****');
WITH a AS (
SELECT *
FROM dblink(
'SELECT slno,fname,mname,lname
FROM remote_tbl'
) AS t (slno int, fname text, mname text, lname text)
)
, b AS (
INSERT INTO temptab1
SELECT slno, name
FROM a
)
, c AS (
INSERT INTO temptab2
SELECT slno, name
FROM a
)
INSERT INTO temptab3
SELECT slno, name
FROM a;
select dblink_disconnect();
end;
$$
LANGUAGE plpgsql;
Calling Function:
select fun();
Error: query has no destination for result data
The stored procedure won't just return the result of the last SELECT. You need to actually return the value:
CREATE OR REPLACE FUNCTION fun() RETURNS text AS $$
BEGIN
--- ....
RETURN(SELECT dblink_disconnect());
END
$$ LANGUAGE plpgsql;
You're getting the error because Postgres expects the function to return something of type text, but your function doesn't return anything.
Use a plain SQL function instead of PL/PgSQL, or use SELECT INTO and ordinary RETURN.
Reason for the error you're getting is because there is no return in between your BEGIN and END for example:
BEGIN
update mytable set lastcodeused = to_char(cast(lastcodeused as INTEGER)+1, 'FM999999999999999999') where
classid = classIdVar and appid= appIdInt
RETURNING concat(pageUniqueCode,lastcodeused) as pageUniqueCode
into taskcode;
return taskcode;
END;
If you have this error using a pgplsql procedure or function, and you are sure that the return is defined correctly, there exists a different non-intuitive cause. I needed some time to realize this so I think it is worth sharing. I had a function like this:
CREATE OR REPLACE FUNCTION "db".fn_x(
id integer)
RETURNS TABLE(b_val varchar(100), c_val varchar(100))
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
var_b_val varchar(100);
var_c_val varchar(100);
BEGIN
select var_b, var_c
-- Missing INTO clause was the cause of the error.
var_b_val, var_c_val
from "db".table_y where y_id = id;
return query(select var_b_val, var_c_val);
END;
$BODY$;
Just adding that missing INTO clause made the function work correctly.
In conclusion, this error can also trigger on silent syntax errors.

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

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

Create multiple stored procedure by running Postgresql script

I wanted to execute a Postgresql scripts file in my vm containing 3 stored procedure. But only the first one gets executed. Any workaround for this? Scripts file:
alter table ex add column if not exists json_column jsonb;
alter table im add column if not exists json_column jsonb;
alter table imag add column if not exists json_column jsonb;
create or replace procedure update_ex( //first stored procedure
js jsonb,
id bigint
)
language plpgsql
as $$
begin
update ex set json_column = js where ex_id=id;
end;$$ // error: SQL Error [42601]: ERROR: syntax error at or near "create"
create or replace procedure update_im( //second stored procedure
js jsonb,
id bigint
)
language plpgsql
as $$
begin
update im set json_column = js where im_set_id=id;
end;$$
create or replace procedure update_imag(
js jsonb,
id bigint
)
language plpgsql
as $$
begin
update imag set json_column = js where imag_id=id;
end;$$
You missing a ";" at the end.
end;$$;

PostgreSQL: Query has no destination for result data

I am trying to fetch data from remote db by using dblink through function but getting an error "query has no destination for result data". I am using plpgsql language to do the same.
Function:
CREATE OR REPLACE FUNCTION fun()
RETURNS text AS
$$
begin
select dblink_connect(
'port=5432 dbname=test user=postgres password=****');
WITH a AS (
SELECT *
FROM dblink(
'SELECT slno,fname,mname,lname
FROM remote_tbl'
) AS t (slno int, fname text, mname text, lname text)
)
, b AS (
INSERT INTO temptab1
SELECT slno, name
FROM a
)
, c AS (
INSERT INTO temptab2
SELECT slno, name
FROM a
)
INSERT INTO temptab3
SELECT slno, name
FROM a;
select dblink_disconnect();
end;
$$
LANGUAGE plpgsql;
Calling Function:
select fun();
Error: query has no destination for result data
The stored procedure won't just return the result of the last SELECT. You need to actually return the value:
CREATE OR REPLACE FUNCTION fun() RETURNS text AS $$
BEGIN
--- ....
RETURN(SELECT dblink_disconnect());
END
$$ LANGUAGE plpgsql;
You're getting the error because Postgres expects the function to return something of type text, but your function doesn't return anything.
Use a plain SQL function instead of PL/PgSQL, or use SELECT INTO and ordinary RETURN.
Reason for the error you're getting is because there is no return in between your BEGIN and END for example:
BEGIN
update mytable set lastcodeused = to_char(cast(lastcodeused as INTEGER)+1, 'FM999999999999999999') where
classid = classIdVar and appid= appIdInt
RETURNING concat(pageUniqueCode,lastcodeused) as pageUniqueCode
into taskcode;
return taskcode;
END;
If you have this error using a pgplsql procedure or function, and you are sure that the return is defined correctly, there exists a different non-intuitive cause. I needed some time to realize this so I think it is worth sharing. I had a function like this:
CREATE OR REPLACE FUNCTION "db".fn_x(
id integer)
RETURNS TABLE(b_val varchar(100), c_val varchar(100))
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
var_b_val varchar(100);
var_c_val varchar(100);
BEGIN
select var_b, var_c
-- Missing INTO clause was the cause of the error.
var_b_val, var_c_val
from "db".table_y where y_id = id;
return query(select var_b_val, var_c_val);
END;
$BODY$;
Just adding that missing INTO clause made the function work correctly.
In conclusion, this error can also trigger on silent syntax errors.