Combine the stored procedure and SQL query into one SQL statement - sql

I am a beginner in SQL and I don't know how to solve this question. How to combine the following SQL query and the stored procedure into a single SQL query. This is a PostgreSQL query and function. Any help is appreciated.
The following query calls a stored procedure/function for each value of the query:
SELECT t.trj_id, T2.trj_id, Similar_trj_woffset(t.trj_id, T2.trj_id)
FROM transitions T1,
transitions T2
WHERE T1.w_s = T2.w_s
AND T1.w_e = T2.w_e
AND T1.trans_id <> T2.trans_id
The stored procedure/function is:
create or replace function similar_trj_woffset(
IN t1 as integer,
IN t2 as integer,
OUT score as integer,
OUT offset as integer
) AS $$
declare
off_set integer :=0;
score integer := 0;
cou integer;
time1 integer;
time2 integer;
dist integer;
begin
for off_set in 0..10 LOOP
time1 := 0;
time2 := 0 + off_set;
cou := 0;
while time2 < 100 Loop
select vec_length(P.x-P2.x,P.y-P2.y,P.z-P2.z) into dist
from AtomPositions P, AtomPositions P2
where P.trj_id = t1
and P2.trj_id = t2
and P.t = time1
and P2.t = time2;
if dist < cuto`enter code here`ff then
cou : = cou + 1;
time1 := time1 + 1;
time2 := time2 + 1;
end loop
if cou > score then
score := cou ;
offset := off_set;
end if;
off_set := off_set + 1;
end loop;
end $$ language plpgsql;
Can some one tell me how to merge the query and the stored procedure into one single SQL query.

To call this function an split the returned record into individual columns:
SELECT t.trj_id, t2.trj_id, (similar_trj_woffset(t.trj_id, t2.trj_id)).*
FROM transitions t1
JOIN transitions t2 USING (w_s, w_e)
WHERE t1.trans_id <> t2.trans_id
Note the parenthesis around the function call.
I also rewrote the query to use proper ANSI JOIN syntax with a simplified equijoin condition (USING) and removed the spurious upper-casing.

You shouldn't merge them is your basic answer. One purpose of the code being in a function is so it can be called from multiple queries and be maintained in one place. Merging the two doesn't make sense when the query is already calling the function on each iteration.
What are you expecting to gain from doing this because simply moving the code isn't going to do anything positive?
Actually, if any other queries use it, they will break if you remove it.

Related

Error while writing to array plsql how to fix? Extend doesn't work also

so I am trying to write to an array in PL/SQL, and I always get the subscript outside of limit error. I've seen similar posts and implemented everything based on those answers, I can't seem to find what I'm doing wrong. The line giving the error is "arr_quartosLivres(counter) := q.id;" I've tried to extend the array and it still doesn't work, however, either way, the look only runs 21 times (because there are only 21 values in the table quarto) so it shouldn't even need to be extended. Any help would be highly appreciated! Thank you
SET SERVEROUTPUT ON;
DECLARE
p_idReserva reserva.id%type := 408;
v_dataEntradaReserva reserva.data_entrada%type;
counter integer := 0;
type arr_aux IS varray(21) of quarto.id%type;
arr_quartosLivres arr_aux := arr_aux();
BEGIN
SELECT data_entrada INTO v_dataEntradaReserva FROM reserva WHERE id = p_idreserva;
FOR q IN (SELECT * FROM quarto)
LOOP
BEGIN
IF isQuartoIndisponivel(q.id, v_dataEntradaReserva)
THEN DBMS_OUTPUT.PUT_LINE('nao disponivel' || counter);
arr_quartosLivres(counter) := q.id;
ELSE DBMS_OUTPUT.PUT_LINE('disponivel' || counter);
END IF;
counter := counter + 1;
END;
END LOOP;
END;
The index values for varray begin with 1. Your logic is trying to use index value 0. Thus index out of range. BTW extend does not apply to varray, when declared a varray has a fixed size. You have 3 solutions: initialize counter to 1 instead of 0, or move incrementing it prior to its use as an index. Since as it stands you increment every time through the loop, even when the IF condition returns false and you do not use the counter as an index, leaving a NULL value in the array.But you use counter for 2 different purposes: Counting rows processed and index into the array. Since the row value may not be put into the array then your 3rd option is to introduce another variable for the index. Further there is no need for the BEGIN ... End block in the loop.
declare
p_idreserva reserva.id%type := 408;
v_dataentradareserva reserva.data_entrada%type;
counter integer := 0;
type arr_aux is varray(21) of quarto.id%type;
arr_quartoslivres arr_aux := arr_aux();
varray_index integer := 1 ; -- index varaibal for varray.
begin
select data_entrada into v_dataentradareserva from reserva where id = p_idreserva;
for q in (select * from quarto)
loop
if isquartoindisponivel(q.id, v_dataentradareserva)
then
dbms_output.put_line('nao disponivel' || counter || ' at index ' || varray_index);
arr_quartoslivres(varray_index) := q.id;
varray_index := varray_index + 1;
else
dbms_output.put_line('disponivel' || counter);
end if;
counter := counter + 1;
end loop;
end;

How to use two columns with clause definescore oracle text

I have this code:
declare
sName varchar(25);
iRank number := 0;
sDesc varchar(510);
cursor q is
SELECT *
FROM trec_topics ORDER BY num;
BEGIN
for ql in q
loop
sDesc := replace(replace(replace(ql.title, '?', '{?}'), ')', '{)}'), '(', '{(}');
--dbms_output.put_line(ql.num||'-'||sDesc);
declare
cursor c is
SELECT /*+ FIRST_ROWS(100) */ docno,
CASE
WHEN SCORE(10) >= SCORE(20) THEN SCORE(10)
ELSE SCORE(20)
END AS SCORE
FROM txt_search_docs WHERE CONTAINS(txt, 'DEFINESCORE(ql.title, OCCURRENCE)', 10) > 0 OR
CONTAINS(txt, 'DEFINESCORE(sDesc, OCCURRENCE)', 20) > 0
order by SCORE desc;
begin
iRank := 1;
for c1 in c
loop
dbms_output.put_line(ql.num||' Q0 '||c1.docno||' '||lpad(iRank,3, '0')||' '||lpad(c1.score, 2, '0')||' myUser');
iRank := iRank + 1;
exit when c%rowcount = 100;
end loop;
end;
end loop;
end;
As you can see I'm doing select on two different tables, however, I need to change the standard score, as it did not perform well. I'm trying to use the DEFINESCORE clause that has this 'DEFINESCORE (query_term, scoring_expression)' format.
How can I call the table columns within this clause? That is, I need to call my columns instead of "query_term", as there are several documents to do the search. Because the way I’m calling him, he’s looking for exactly the term ql.title
Anyone a suggestion to help me with this problem?
I finally managed to solve it.
It was about:
create a variable: topics varchar (525);
store the column value: topics := replace(replace(replace(ql.title, '?', '{?}'), ')', '{)}'), '(', '{(}');
and after calling it in the CONTAINS clause: FROM txt_search_docs WHERE CONTAINS(txt, 'DEFINESCORE(('''||topics||'''), OCCURRENCE)', 1) > 0

Database PL/SQL

I have to make an assignment for school but I get two errors:
Encountered the symbol "FETCH" when expecting on of the following:
constant exception <an identifier> <a double-quoted
delimited-identifier> table LONG_ double ref char time timestamp
interval date binary national character nchar
and
Encountered the symbol "end-of-file" when expecting one of the
following: end not pragma final instantiable order overriding static
member constructor map
Here is the link to my code: http://pastebin.com/h4JN9YQY
CREATE OR REPLACE PROCEDURE generate_bonus
AS
cursor student_info is
select distinct students.id,
events.begindatetime,
events.enddatetime,
count(items.number_of_coupons) as coupons_collected,
events.type from students
join applies on applies.students_id = students.id
join schedules on schedules.id = applies.schedules_id
join events on events.id = schedules.events_id
join orders on orders.students_id = students.id
join orderitems on orderitems.orders_id = orders.id
join items on items.id = orderitems.items_id
join bars on bars.id = orders.bars_id
where applies.status = 'PLANNED'
and orderitems."NUMBER" is not null
and bars.name is not null
group by students.id, events.begindatetime, events.enddatetime, events.type
order by students.id;
BEGIN
DECLARE
s_id integer(256);
s_beginDate date;
s_endDate date;
s_noCoupons number(256);
s_eventType varchar2(256);
s_workedHours number(24) := 8;
calculated_bonus number(256);
count_rows integer(256);
OPEN student_info;
LOOP
FETCH student_info into s_id, s_beginDate, s_endDate, s_noCoupons, s_eventType;
Select count(*) into count_rows from student_bonus where students_id = s_id and rownum <= 1;
EXIT WHEN count_rows = 1;
IF (s_eventType = 'ROUGH') THEN
calculated_bonus := s_workedHours * (s_workedHours / 100 * 7) * s_noCoupons;
INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);
calculated_bonus := 0;
ELSIF (s_eventType = 'NORMAL') THEN
calculated_bonus := s_workedHours * (s_workedHours / 100 * 4) * s_noCoupons;
INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);
calculated_bonus := 0;
ELSE
calculated_bonus := s_workedHours * (s_workedHours / 100 * 2) * s_noCoupons;
INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);
calculated_bonus := 0;
END IF;
END LOOP;
CLOSE student_info;
END generate_bonus;
In my opinion much easier for juniors to use is cursor loop, in this project You will avoid this kind of errors. Syntax like:
FOR row_variable IN cursor LOOP
dbms_output.put_line(row_variable.id);
END LOOP;
row_variable holds valeus from each cursor row, and you can easily acces it with '.' (dot) operator like row_variable.id
Using cursor loop lets You avoid problems with fetching data, taking care about open/close cursor and worry about output outside cursor space.
The loop will make loops exactly how many items cursor is pointing, like for each loop.
put this line:
EXIT WHEN student_info%NOTFOUND;
after this line:
FETCH student_info into s_id, s_beginDate, s_endDate, s_noCoupons, s_eventType;
You had reached the end of your cursor but there is no code which tells it to exit.
Cursor should follow steps in following manner..
Open Cursor
LOOP
FETCH cursor
EXIT Cursor condtion
{--
Your rest of the code here
--}
end loop;

Oracle : String Concatenation is too long

I have below SQL as a part of a view. In one of the schema I am getting "String Concatenation is too long" error and not able to execute the view.
Hence I tried the TO_CLOB() and now VIEW is not throwing ERROR, but it not returning the result as well it keep on running..
Please suggest....
Sql:
SELECT Iav.Item_Id Attr_Item_Id,
LISTAGG(La.Attribute_Name
||'|~|'
|| Lav.Attribute_Value
||' '
|| Lau.Attribute_Uom, '}~}') WITHIN GROUP (
ORDER BY ICA.DISP_SEQ,LA.ATTRIBUTE_NAME) AS ATTR
FROM Item_Attribute_Values Iav,
Loc_Attribute_Values Lav,
Loc_Attribute_Uoms Lau,
Loc_Attributes La,
(SELECT *
FROM Item_Classification Ic,
CATEGORY_ATTRIBUTES CA
WHERE IC.DEFAULT_CATEGORY='Y'
AND IC.TAXONOMY_TREE_ID =CA.TAXONOMY_TREE_ID
) ICA
WHERE IAV.ITEM_ID =ICA.ITEM_ID(+)
AND IAV.ATTRIBUTE_ID =ICA.ATTRIBUTE_ID(+)
AND Iav.Loc_Attribute_Id =La.Loc_Attribute_Id
AND La.Locale_Id =1
AND Iav.Loc_Attribute_Uom_Id =Lau.Loc_Attribute_Uom_Id(+)
AND Iav.Loc_Attribute_Value_Id=Lav.Loc_Attribute_Value_Id
GROUP BY Iav.Item_Id;
Error:
ORA-01489: result of string concatenation is too long
01489. 00000 - "result of string concatenation is too long"
*Cause: String concatenation result is more than the maximum size.
*Action: Make sure that the result is less than the maximum size.
You can use the COLLECT() function to aggregate the strings into a collection and then use a User-Defined function to concatenate the strings:
Oracle Setup:
CREATE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/
CREATE FUNCTION concat_List(
strings IN stringlist,
delim IN VARCHAR2 DEFAULT ','
) RETURN CLOB DETERMINISTIC
IS
value CLOB;
i PLS_INTEGER;
BEGIN
IF strings IS NULL THEN
RETURN NULL;
END IF;
value := EMPTY_CLOB();
IF strings IS NOT EMPTY THEN
i := strings.FIRST;
LOOP
IF i > strings.FIRST AND delim IS NOT NULL THEN
value := value || delim;
END IF;
value := value || strings(i);
EXIT WHEN i = strings.LAST;
i := strings.NEXT(i);
END LOOP;
END IF;
RETURN value;
END;
/
Query:
SELECT Iav.Item_Id AS Attr_Item_Id,
CONCAT_LIST(
CAST(
COLLECT(
La.Attribute_Name || '|~|' || Lav.Attribute_Value ||' '|| Lau.Attribute_Uom
ORDER BY ICA.DISP_SEQ,LA.ATTRIBUTE_NAME
)
AS stringlist
),
'}~}'
) AS ATTR
FROM your_table
GROUP BY iav.item_id;
LISTAGG is limited to 4000 characters unfortunately. So you may want to use another approach to concatenate the values.
Anyway ...
It is strange to see LISTAGG which is a rather new feature combined with error-prone SQL1992 joins. I'd suggest you re-write this. Are the tables even properly joined? It looks strange that there seems to be no relation between Loc_Attributes and, say, Loc_Attribute_Values. Doesn't have Loc_Attribute_Values a Loc_Attribute_Id so an attribute value relates to an attribute? It would be hard to believe that there is no such relation.
Moreover: Is it guaranteed that your classification subquery doesn't return more than one record per attribute?
Here is your query re-written:
select
iav.item_id as attr_item_id,
listagg(la.attribute_name || '|~|' || lav.attribute_value || ' ' || lau.attribute_uom,
'}~}') within group (order by ica.disp_seq, la.attribute_name) as attr
from item_attribute_values iav
join loc_attribute_values lav
on lav.loc_attribute_value_id = iav.loc_attribute_value_id
and lav.loc_attribute_id = iav.loc_attribute_id -- <== maybe?
join loc_attributes la
on la.loc_attribute_id = lav.loc_attribute_id
and la.loc_attribute_id = lav.loc_attribute_id -- <== maybe?
and la.locale_id = 1
left join loc_attribute_uoms lau
on lau.loc_attribute_uom_id = iav.loc_attribute_uom_id
and lau.loc_attribute_id = iav.loc_attribute_id -- <== maybe?
left join
(
-- aggregation needed to get no more than one sortkey per item attribute?
select ic.item_id, ca.attribute_id, min (ca.disp_seq) as disp_seq
from item_classification ic
join category_attributes ca on ca.taxonomy_tree_id = ic.taxonomy_tree_id
where ic.default_category = 'y'
group by ic.item_id, ca.attribute_id
) ica on ica.item_id = iav.item_id and ica.attribute_id = iav.attribute_id
group by iav.item_id;
Well, you get the idea; check your keys and alter your join criteria where necessary. Maybe this gets rid of duplicates, so LISTAGG has to concatenate less attributes, and maybe the result even stays within 4000 characters.
Xquery approach.
Creating extra types or function isn't necessary.
with test_tab
as (select object_name
from all_objects
where rownum < 1000)
, aggregate_to_xml as (select xmlagg(xmlelement(val, object_name)) xmls from test_tab)
select xmlcast(xmlquery('for $row at $idx in ./*/text() return if($idx=1) then $row else concat(",",$row)'
passing aggregate_to_xml.xmls returning content) as Clob) as list_in_lob
from aggregate_to_xml;
I guess you need to write a small function to concatenate the strings into a CLOB, because even when you cast TO_CLOB() the LISTAGG at the end, this might not work.
Here´s a sample-function that takes a SELECT-Statement (which MUST return only one string-column!) and a separator and returns the collected values as a CLOB:
CREATE OR REPLACE FUNCTION listAggCLob(p_stringSelect VARCHAR2
, p_separator VARCHAR2)
RETURN CLOB
AS
cur SYS_REFCURSOR;
s VARCHAR2(4000);
c CLOB;
i INTEGER;
BEGIN
dbms_lob.createtemporary(c, FALSE);
IF (p_stringSelect IS NOT NULL) THEN
OPEN cur FOR p_stringSelect;
LOOP
FETCH cur INTO s;
EXIT WHEN cur%NOTFOUND;
dbms_lob.append(c, s || p_separator);
END LOOP;
END IF;
i := length(c);
IF (i > 0) THEN
RETURN dbms_lob.substr(c,i-length(p_separator));
ELSE
RETURN NULL;
END IF;
END;
This function can be used f.e. like this:
WITH cat AS (
SELECT DISTINCT t1.category
FROM lookup t1
)
SELECT cat.category
, listAggCLob('select t2.name from lookup t2 where t2.category = ''' || cat.category || '''', '|') allcategorynames
FROM cat;

Postgres while loop really slow

I got this sproc where i get the volume depending on a "FlowVolume" it could be 1, 10, 100. The only way that I found to make this work is the current implementation, but is really slow. The other way is to have intervals in the "Timestamp" it could be every 12 seconds, every 2 mins or every 20 mins intervals.
CREATE OR REPLACE FUNCTION "getBLTPoints"(IN id integer)
RETURNS TABLE(times text, bio text, particles text) AS
$BODY$
DECLARE
currVol real;
currbio integer;
currpart integer;
times timestamp with time zone;
totalvol integer;
profileid integer;
analysisopt integer;
analysisvol integer;
counter integer;
BEGIN
--Getting the right profile to get the line,autosample,analysis volume
select "ProfileID"into profileid from "Samples" where "ID" = id;
--Getting the mode that the system was in when sample was done
select "optBaseVolume" into analysisopt from "Profiles" where "ID" = profileid;
if(analysisopt = 0) then
analysisvol = 1;
elseif(analysisopt = 1) then
analysisvol = 10;
else
analysisvol = 100;
end if;
counter = analysisvol;
--Getting the totalvolume to run it in the while loop until it reaches the totalvolume
select floor("FlowVolume")::integer into totalvol from "Results" where "SampleID" = id order by "FlowVolume" desc limit 1;
WHILE counter <= totalvol LOOP
RETURN QUERY
select extract('epoch' from "Timestamp")::text,"BioAccumulated"::text, "TotalParticlesAccum"::text from
"Results" where "SampleID" = id and "FlowVolume" = counter;
counter = counter + analysisvol;
END LOOP;
END;
using a RETURN QUERY inside any cycle cannot be fast. WHILE cycle is fast as your nested query is fast. But you can use significantly faster technique .. you can generate a counter series by function generate_series, and you can join this series to your "Result" table:
RETURN QUERY SELECT extract('epoch' FROM "Timestamp")::text,
"BioAccumulated"::text,
"TotalParticlesAccum"::text
FROM "Result"
JOIN generate_series(analysisvol, totalvol, analysisvol) g(v)
ON "Result"."FlowVolume" = g.v
WHERE "SampleID" = id;