An SQL Query works fine when executed, But the same Query if I'm using in a PL/SQL it shows error - sql

Hi I've the following query which works fine in SQL, I mean it gets executed successfully.
SELECT id_ligne, n_res_id, n_rty_id, NVL (res_va_text, va_res_txt),
NVL (res_va_short_name, va_res_short_name), bl_has_bo, bl_asset,
bl_needs_profile, bl_update_name, bl_popup_mandatory, va_popup_name,
va_itrack_create, va_itrack_delete, va_generic_mail,
n_application_type, bl_mono_profile
FROM t_t_actres_ea4, t_resource_ea4
WHERE res_n_id(+) = n_res_id
AND t_t_actres_ea4.id_ligne NOT IN (
SELECT DISTINCT (t_erralim_ea4.err_n_lineid)
FROM t_erralim_ea4
WHERE 0235303 = t_erralim_ea4.err_n_code_trt
AND err_n_rejecttype = 0)
It gets executed and the desired output is fetched. But when I use the same query for the PL/SQL like below.
L_REQ Varchar2(1000) := 'select ID_LIGNE, N_RES_ID, N_RTY_ID, NVL(RES_VA_TEXT,VA_RES_TXT),NVL(RES_VA_SHORT_NAME,VA_RES_SHORT_NAME), '||
' BL_HAS_BO, BL_ASSET, BL_NEEDS_PROFILE, BL_UPDATE_NAME, BL_POPUP_MANDATORY, VA_POPUP_NAME, ' ||
' VA_ITRACK_CREATE, VA_ITRACK_DELETE, VA_GENERIC_MAIL, N_APPLICATION_TYPE, BL_MONO_PROFILE ' ||
' from T_T_ACTRES_EA4, T_RESOURCE_EA4 ' ||
' where RES_N_ID (+) = N_RES_ID AND T_T_ACTRES_EA4.ID_LIGNE not in ( select distinct(T_ERRALIM_EA4.ERR_N_LINEID) ' ||
' from T_ERRALIM_EA4 ' ||
' where 0235307 = T_ERRALIM_EA4.ERR_N_CODE_TRT AND ERR_N_REJECTTYPE = 0 and T_ERRALIM_EA4.ERR_VA_MSG not like ''Mono-profile%'')';
It gives me the error saying ORA-00936: missing expression
Note: This error is caused when I append this conditon to the sub-query.
and T_ERRALIM_EA4.ERR_VA_MSG not like ''Mono-profile%''
TIA.

Assuming what you provided is what you want, no execute immediate: it cannot compile.
You have
variable declaration := sql statement
You should have
DECLARE
variable declaration
BEGIN
sql statement
END;
The sql statement should have SELECT INTO syntax
SELECT id_ligne, n_res_id, n_rty_id, NVL (res_va_text, va_res_txt),
NVL (res_va_short_name, va_res_short_name), bl_has_bo, bl_asset,
bl_needs_profile, bl_update_name, bl_popup_mandatory, va_popup_name,
va_itrack_create, va_itrack_delete, va_generic_mail,
n_application_type, bl_mono_profile
INTO LREQ
FROM t_t_actres_ea4, t_resource_ea4
WHERE res_n_id(+) = n_res_id
AND t_t_actres_ea4.id_ligne NOT IN (
SELECT DISTINCT (t_erralim_ea4.err_n_lineid)
FROM t_erralim_ea4
WHERE 0235303 = t_erralim_ea4.err_n_code_trt
AND err_n_rejecttype = 0);
#Bob Jarvis is spot on also - you could do
DECLARE
L_REQ Varchar2(1000) := 'select ID_LIGNE, N_RES_ID, N_RTY_ID, NVL(RES_VA_TEXT,VA_RES_TXT),NVL(RES_VA_SHORT_NAME,VA_RES_SHORT_NAME), '||
' BL_HAS_BO, BL_ASSET, BL_NEEDS_PROFILE, BL_UPDATE_NAME, BL_POPUP_MANDATORY, VA_POPUP_NAME, ' ||
' VA_ITRACK_CREATE, VA_ITRACK_DELETE, VA_GENERIC_MAIL, N_APPLICATION_TYPE, BL_MONO_PROFILE ' ||
' from T_T_ACTRES_EA4, T_RESOURCE_EA4 ' ||
' where RES_N_ID (+) = N_RES_ID AND T_T_ACTRES_EA4.ID_LIGNE not in ( select distinct(T_ERRALIM_EA4.ERR_N_LINEID) ' ||
' from T_ERRALIM_EA4 ' ||
' where 0235307 = T_ERRALIM_EA4.ERR_N_CODE_TRT AND ERR_N_REJECTTYPE = 0 and T_ERRALIM_EA4.ERR_VA_MSG not like ''Mono-profile%'')';
BEGIN
EXECUTE IMMEDIATE L_REQ;
END;
This is probably not the best choice, static SQL offers better performance a lot of the time.

Related

Relation "" already exist dynamic query

I'm trying to execute a query within a loop which is within another loop, which is within an anonymous code block.
I'm trying to run this query
query := 'CREATE VIEW hopsPartialDistance AS '
||'SELECT AvgDistance '
||'FROM Distance '
||'WHERE PlanetOrigin = ' || rHops.PlanetOrigin
||' AND PlanetDestination = ' || rHops.PlanetDestination;
EXECUTE query;
But I keep getting the error
relation "hopspartialdistance" already exists
I then run
\d hopspartialdistance;
And the output is
Did not find any relation named "hopspartialdistance".
So I don't know where the error is coming from.
Here's the full function
DO
$$
DECLARE
routeDistance real := 0.0;
hopDistance real := 0.0;
rRoute record;
rHops record;
query text := '';
BEGIN
FOR rRoute IN
SELECT MonitoringKey FROM TradingRoute
LOOP
query := 'CREATE VIEW PortsOfCall AS '
||'SELECT PlanetID, VisitOrder '
||'FROM EnrichedCallsAt '
||'WHERE MonitoringKey = ' || rRoute.MonitoringKey
||' ORDER BY VisitOrder';
EXECUTE query;
CREATE VIEW Hops AS
SELECT A.PlanetID AS PlanetOrigin,
B.PLanetID AS PlanetDestination
FROM PortsOfCall A INNER JOIN PortsOfCall B ON A.VisitOrder + 1 = B.VisitOrder;
routeDistance = 0.0;
FOR rHops IN
SELECT PlanetOrigin, PlanetDestination FROM Hops
LOOP
query := 'CREATE VIEW hopsPartialDistance AS '
||'SELECT AvgDistance '
||'FROM Distance '
||'WHERE PlanetOrigin = ' || rHops.PlanetOrigin
||' AND PlanetDestination = ' || rHops.PlanetDestination;
EXECUTE query;
hopDistance = (SELECT SUM(AvgDistance) FROM hopsPartialDistance) ;
routeDistance = routeDistance + hopDistance;
END LOOP;
INSERT INTO RouteLength (RouteMonitoringKey, RouteTotalDistance)
SELECT rRoute.MonitoringKey, routeDistance;
DROP VIEW Hops
CASCADE;
DROP VIEW PortsOfCall
CASCADE;
END LOOP;
END;
$$;

How can i turn this pl/sql into a procedure

I had to write this query for an assignement. So we have a database and we are pulling information from it, this is going to work with some back end c# eventually. Is there anything i can do , knowing im going to reuse this, in order to make it better and more adaptable when the day comes when i have to connect it all.
set serveroutput on
DECLARE
LV_DATE HVK_RESERVATION.RESERVATION_START_DATE%TYPE;
LV_SERV VARCHAR(100);
CURSOR LCUR_RES IS
SELECT *
FROM HVK_RESERVATION R
INNER JOIN HVK_PET_RESERVATION PR
ON R.RESERVATION_NUMBER = PR.RES_RESERVATION_NUMBER
INNER JOIN HVK_PET P
ON P.PET_NUMBER = PR.PET_PET_NUMBER
INNER JOIN HVK_OWNER OW
ON OW.OWNER_NUMBER = P.OWN_OWNER_NUMBER
WHERE R.RESERVATION_START_DATE < LV_DATE
AND R.RESERVATION_END_DATE > LV_DATE;
CURSOR LCUR_SERVICE(PET_RES_NUM NUMBER) IS
SELECT *
FROM HVK_SERVICE S
INNER JOIN HVK_PET_RESERVATION_SERVICE PRS
ON PRS.SERV_SERVICE_NUMBER = S.SERVICE_NUMBER
AND PRS.PR_PET_RES_NUMBER = PET_RES_NUM;
BEGIN
LV_DATE := TO_DATE('&logdate', 'yy-mm-dd');
DBMS_OUTPUT.PUT_LINE('Kennel log for ' || '' || LV_DATE);
DBMS_OUTPUT.PUT_LINE('-------------------------------');
FOR I IN LCUR_RES LOOP
DBMS_OUTPUT.PUT_LINE('Run:' || '' || I.RUN_RUN_NUMBER || ' ' ||
'Pet: ' || '' || I.PET_NAME || ' ' ||
I.OWNER_LAST_NAME || ' Pet Reservation: ' || '' ||
I.PET_RES_NUMBER);
DBMS_OUTPUT.PUT_LINE('Reservation start/end ' || ' ' ||
I.RESERVATION_START_DATE || ' ' ||
I.RESERVATION_END_DATE);
DBMS_OUTPUT.PUT('Services : ');
FOR X IN LCUR_SERVICE(I.PET_RES_NUMBER) LOOP
DBMS_OUTPUT.PUT(X.SERVICE_DESCRIPTION || ' ');
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
FOR LREC_LOG IN (SELECT *
FROM HVK_KENNEL_LOG KL
WHERE KL.PR_PET_RES_NUMBER = I.PET_RES_NUMBER
) LOOP
DBMS_OUTPUT.PUT_LINE('Notes: ' || '' ||
LREC_LOG.KENNEL_LOG_SEQUENCE_NUMBER || ' ' ||
'Log Note: ' || '' || LREC_LOG.KENNEL_LOG_NOTES);
END LOOP;
DBMS_OUTPUT.PUT_LINE(' ');
END LOOP;
END;
It it supposed to output the run number , reservation number , pet name , and any relate notes.
you can replace DECLARE with CREATE OR REPLACE PROCEDURE my_proc(in_logdate in date) IS.
in that case my_proc will be the name of your procedure.
you should also use a parameter instead of &logdate
so e.g. parameter name in_logdate of type date
...
LV_DATE := in_logdate;
...

Postgres Type of Parameter does not match

I am having an odd problem with Postgres (10.5). I have a function, generate_unique_name which takes in three text values. It works fine; however, calling this function seems to be an issue. When I call the function using:
SELECT generate_unique_name('basic', 'seeds', 'project=' || 2)
It works without issue. I can make the same call several times. Now, when I try the same call, but change the second parameter as below:
SELECT generate_unique_name('basic', 'queue', 'project=' || 2)
Then it seems to fail with the error:
ERROR: type of parameter 9 (text) does not match that when preparing
the plan (character varying) CONTEXT: PL/pgSQL function
generate_unique_name(text,text,text) line 12 at assignment SQL state:
42804
I have tried changing the query to:
SELECT generate_unique_name('basic'::text, 'queue'::text, ('project=' || 2)::text)
But this also fails. If I then kill the connection to postgres DB, and create a new one, and instead start with the second query, it now works, but the first stops functioning.
It seems like postgres decides to stop treating the parameters as text part way through, for no apparent reason. Am I missing something?
EDIT: Code for generate_unique_name
CREATE OR REPLACE FUNCTION public.generate_unique_name(
proposed_name text,
table_name text,
condition text)
RETURNS text
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
DECLARE
unique_name text;
name_counter integer;
r record;
names_to_check text[];
BEGIN
unique_name = proposed_name;
name_counter = 0;
FOR r IN EXECUTE 'SELECT name FROM ' || table_name || ' WHERE ' || condition LOOP
names_to_check = array_append(names_to_check, r.name::text);
END LOOP;
WHILE unique_name = ANY(names_to_check) LOOP
name_counter = name_counter + 1;
unique_name = proposed_name || ' (' || name_counter || ')';
END LOOP;
RETURN unique_name;
END;
$BODY$;
My guess is there's a value in the name column of the queue table that causes an issue with
names_to_check = array_append(names_to_check, r.name::text)
As Joe mentioned, the issue was with the array_append, which I could not figure out a way to fix. Instead, the generate_unique_names functions was changed to just query the DB continuously.
CREATE OR REPLACE FUNCTION generate_unique_name (proposed_name text, table_name text, condition text) RETURNS text AS
$BODY$
DECLARE
unique_name text;
name_counter integer;
not_unique boolean;
BEGIN
unique_name = proposed_name;
name_counter = 0;
EXECUTE 'SELECT COUNT(*)!=0 FROM ' || table_name || ' WHERE ' || condition || ' AND name = ''' || unique_name || '''' INTO not_unique;
WHILE not_unique LOOP
name_counter = name_counter + 1;
unique_name = proposed_name || ' (' || name_counter || ')';
EXECUTE 'SELECT COUNT(*)!=0 FROM ' || table_name || ' WHERE ' || condition || ' AND name = ''' || unique_name || '''' INTO not_unique;
END LOOP;
RETURN unique_name;
END;
$BODY$ LANGUAGE plpgsql;

Execute immediate UPDATE Statement

I try to make an update table CHECK_COMPRESSER into PROCEDURE and I use EXECUTE IMMEDIATE :
EXECUTE immediate 'update CHECK_COMPRESSER
set NEW_SIZE_MB = '||''''||TABLE_P_ENTRY.NEW_SIZE_MB || '''' ||
' WHERE EXEC_ID = ' || '''' || EXEC_ID || '''' || ' AND TABLE = ' || '''' || TABLE_P_ENTRY.SEGMENT_NAME || '''' || ' AND PARTITION = ' || '''' || TABLE_P_ENTRY.PARTITION_NAME || '''';
dbms_output.put_line shows:
update CHECK_COMPRESSER set NEW_SIZE_MB = '182' WHERE EXEC_ID = '43' AND TABLE = 'MA_CONTACT_COMPRESS' AND PARTITION = 'P_OLD'
but there is an error:
ORA-00936: missing expression ORA-06512: at "SASDBA.COMPRESS_TABLE",
line 50
so, how should I edit this code?
TABLE is a keyword. It can be used as identifier only if quoted: "TABLE".
P.S. PARTITION is the same.

PostgreSQL : ERROR: function geta(integer, integer, unknown) does not exist

I am creating a dynamic query generation function which helped me out in my work. The CREATE FUNCTION script parsed successfully but i am not able execute it. While executing the function, it shows an error.
I have tried a lot.
Please look below for code.
CREATE OR REPLACE FUNCTION "public"."GetA" (headcategoriesid int4,cdfid int4,
colName text)
RETURNS varchar AS
$BODY$
DECLARE
sql1 text := 'select string_agg(answer, '','') as HeadName from
' || $3 || 'where cdfid = ' || $2 || ' and headcategoriesid = '|| $1;
BEGIN
-- RETURN QUERY
Execute sql1;
-- 'select string_agg(answer, '','') as HeadName from ' || $3 ||
'where cdfid = ' || $2 || ' and headcategoriesid = '|| $1;
-- RETURN QUERY EXECUTE format(
-- 'select string_agg(answer, '','') as HeadName from %I WHERE
cdfid = %L and headcategoriesid = %L', colName, 7, 96
-- );
END
$BODY$
LANGUAGE plpgsql;
Am using Postgresql 9.2.4
Call function as below:
SELECT "GetA"(7,96,'educationdetails'::text);
when you call the function GetA without ""(quotes) than it will be considerd as geta (small letter). but in your code you are using "" so it is created as GetA.