I'm trying to write select statement inside if in PostgreSQL but getting error like ERROR: syntax error at or near "SELECT" please help me to solve this error
LINE 39: SELECT random()*power( 10, 8 ) INTO STRICT W_OTPNUM;
BEGIN
if p_mobile_no=usersdefotp_mobile_no
then
P_OTP :='123456'
else
SELECT random()*power( 10, 8 ) INTO STRICT W_OTPNUM;
P_OTP :=LPAD(W_OTPNUM::TEXT, 6, '0');
INSERT INTO usersdefotp(P_OTP)
end if
EXCEPTION
WHEN NO_DATA_FOUND THEN
P_OTP := NULL;
END;
You can use declarations in a Procedure as an Array on PostgreSQL, like this one:
do $$
declare
randoms text[];
begin
-- store data
randoms = ARRAY(SELECT random()*power( 10, 8 ) INTO STRICT W_OTPNUM;
P_OTP :=LPAD(W_OTPNUM::TEXT, 6, '0'));
if something
then
do something
else
return format("%s", randoms)
end if;
end
$$;
That way you can store your select and then use him when needed to return;
Related
I am trying to modify an existing sessions procedure to add cycle count.The error I am getting is
SQL Error [42601]: ERROR: syntax error at or near "END"
Position: 3587
--call transactions_packs.tep_session()
CREATE OR REPLACE PROCEDURE transactions_packs.tep_session()
LANGUAGE plpgsql
AS $procedure$
DECLARE
session "transactions_packs"."simple_sessions";
"session_toSearch" TEXT;
"end_timestamp" TIMESTAMP WITH TIME ZONE;
"energy" NUMERIC;
"charge" NUMERIC;
"duration" NUMERIC;
"cycle_count" numeric;
"f" record ;
BEGIN
cycle_count = '0';
-- go to statement fore session reset
FOR session IN SELECT * FROM "transactions_packs"."simple_sessions" WHERE "sessionDuration" IS NULL
LOOP
BEGIN
IF session."sessionType" = 0 THEN
"session_toSearch" := 'Charging';
ELSIF session."sessionType" = 1 THEN
"session_toSearch" := 'Discharging';
END IF;
-- Session_count:Start
EXECUTE FORMAT('
FOR f IN select (current' || '%s' || '), "timestamp"
FROM "transactions_packs"."basic_measurements_packs" a order by a."timestamp" desc
LOOP
BEGIN
IF AVG((current' || '%s' || '))
OVER (ORDER BY "f"."timestamp" ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) > 0.01
then cycle_count = cycle_count + 1;
END IF;
END
END LOOP;',"session_toSearch","session_toSearch")
-- get value from If and else statement to fetch records from charging and discharging col
--Session_count :End
END ;
END LOOP;
end;
$procedure$
;
where -
schema is transactions_packs
tables are -
simple_sessions
basic_measurements_packs
Please let me know if there is any part of query is which cannot be understood.
The variable "hell" sounds like useless here, try something like this :
create or replace procedure "public"."extract"(arg json)
language plpgsql as $$
begin
raise notice 'test:%', arg->'global'->>'packetType';
raise notice 'test1:%', arg-> 'transactional'->>'A';
-- freeze the input
end ;
$$;
I have a syntax error on a procedure (PostgreSQL) and I can't find the mistake. I need a procedure with the variable declaration, a for loop, and a transaction.
Here is a sample of the structure I'm using.
CREATE OR REPLACE PROCEDURE repor_dados()
LANGUAGE plpgsql AS
$$
DECLARE
utilizador int;
data_acesso date;
r record;
BEGIN
BEGIN -- begin transaction
select 2 ,4 into data_acesso,utilizador;
IF EXISTS(select 2) then -- condition
BEGIN
-- for each select result
FOR r IN SELECT 1,2,3
LOOP
-- do something
IF r = 1 THEN
SELECT 3;
ELSE
SELECT 4;
END IF;
END LOOP;
END;
COMMIT; -- commit transaction
END
$$
I am working with PostgreSQL 13.2.
The error is:
ERROR: syntax error at end of input LINE 30: $$
After reading the documentation for ASSERT, I am still confused how to use it, and can't find any examples online of how I would do something simple using ASSERT in a .sql script.
For example, say I want to ASSERT that the number of rows returned from SELECT * FROM my_table WHERE my_col = 3 is equal to 10.
Can someone provide a working example of that?
I would assume you try todo smth similar?
so=# select count(*) from pg_database;
count
-------
21
(1 row)
so=# do $$ begin assert (select count(*) from pg_database) = 21, 'not 21!';end;$$;
DO
so=# do $$ begin assert (select count(*) from pg_database) = 22, 'not 22!';end;$$;
ERROR: not 22!
CONTEXT: PL/pgSQL function inline_code_block line 1 at ASSERT
do $$
begin
ASSERT 1 = 2;
end;
$$ LANGUAGE plpgsql;
But note, it works only starting from PostgreSql 9.5. In older versions you can define your own assert-function such like this
CREATE OR REPLACE FUNCTION __assert(boolean) RETURNS VOID AS $$
BEGIN
IF NOT $1 THEN
RAISE EXCEPTION 'ASSERTING FAILED';
END IF;
END;
$$ LANGUAGE plpgsql;
And then use in this way
do $$
declare
tmp char;
begin
tmp := __assert(tmp_to_https('https') = 'https');
end;
$$ LANGUAGE plpgsql;
do
$xyz$
declare
y text;
i record;
begin
y := to_char(current_timestamp, 'YYYYMMDDHHMMSS');
raise notice '%',y;
execute 'CREATE TEMP TABLE someNewTable'
||y
||' AS select * from ( VALUES(0::int,-99999::numeric), (1::int, 100::numeric)) as t (key, value)';
for i in (select * from someNewTable||y) loop
raise notice '%',i.key;
end loop;
end;
$xyz$ language 'plpgsql'
ERROR: syntax error at or near "||"
LINE 13: for i in (select * from someNewTable||y) loop
Im unable to understand why the error is at the PIPE symbol. Please help me. I have been trying in Oracle db too, but same error. Am I doing anything wrong here?
The query in for ... loop statement also has to be dynamic, so you should use execute twice.
Use the format() function which is very convenient in conjunction with execute:
do $xyz$
declare
y text;
i record;
begin
y := to_char(current_timestamp, 'YYYYMMDDHHMMSS');
raise notice '%', y;
execute format($ex$
create temp table somenewtable%s
as select * from (
values
(0::int, -99999::numeric),
(1::int, 100::numeric)
) as t (key, value)
$ex$, y);
for i in
execute format($ex$
select * from somenewtable%s
$ex$, y)
loop
raise notice '%',i.key;
end loop;
end;
$xyz$ language 'plpgsql';
am writing procedure in pl/sql. when i run the procedure am getting error.Here i enclosed the procedure.
code
PROCEDURE get_CDR_rs_phone (CDR_recordset OUT SYS_REFCURSOR)AS
BEGIN
OPEN CDR_recordset FOR
SELECT zkv.CISCOCUIC_TBL.FLD_callingPartyNumber FROM zkv.CISCOCUIC_TBL;
END get_CDR_rs_phone;
/
getting error when i run this proc
Create your procedure as
CREATE OR REPLACE PROCEDURE get_cdr_rs_phone (
cdr_recordset OUT sys_refcursor
)
AS
BEGIN
OPEN cdr_recordset FOR
SELECT zkv.ciscocuic_tbl.fld_callingpartynumber
FROM zkv.ciscocuic_tbl;
END get_cdr_rs_phone;
/
and execute your procedure
variable r refcursor;
DECLARE
BEGIN
get_cdr_rs_phone (:r);
END;
/
print r
Update 1
If your procedure is doing only a select you could do this using a function which return sys_refcursor and the function can be executed from a sql statement.
Create function as
CREATE OR REPLACE FUNCTION get_cdr_rs_phone_func
RETURN sys_refcursor
AS
out_cursor sys_refcursor;
BEGIN
OPEN out_cursor FOR
SELECT zkv.ciscocuic_tbl.fld_callingpartynumber
FROM zkv.ciscocuic_tbl;
END get_cdr_rs_phone;
RETURN out_cursor;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
--raise error
WHEN OTHERS
THEN
--raise error
END get_cdr_rs_phone_func;
/
and call this function as
select get_cdr_rs_phone_func from dual;