How to execute a stored procedure in Oracle SQL Developer [closed] - stored-procedures

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
CREATE OR REPLACE PROCEDURE TEST_RATING
(
P_RID OUT NUMBER
, P_STARS OUT NUMBER
, P_DATE OUT DATE
) AS
BEGIN
SELECT RID, STARS, RATINGDATE
INTO P_RID, P_STARS, P_DATE
FROM RATING;
END TEST_RATING;

It seems unlikely that this procedure does what you want. Unless you are guaranteed that there will always e exactly 1 row in the rating table, you'll get an error at runtime that your query either returns no rows or that it returns multiple rows.
If you are guaranteed that you'll always have exactly 1 row in rating, you can call this procedure in an anonymous PL/SQL block
DECLARE
l_rid number;
l_stars number;
l_date date;
BEGIN
test_rating( l_rid,
l_stars,
l_date );
END;
Of course, you'd probably want to do something with the data that is returned even if that is just calling dbms_output.put_line to write it to the dbms_output buffer.

Related

Pyspark join datasets -duplicate column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i am trying to join 2 datasets having columns with same name in pyspark - and join failes with duplicate column error.
id name color
123456 Rose Yellow
456789 Jasmine white
789654 Lily Purple
2nd dataset looks like this
id name Place
123456 Rose Canada
456789 Jasmine US
333444 Lily Purple
I want to join these where id and name matches.. and get the output with inner join, where id and name matches in both datasets..
id name color Place
123456 Rose Yellow Canada
456789 Jasmine white US
I tried to write a pyspark function like this
def trial(df1,df2):
join_df = df1.join(df2, ["id","name"], how='inner')
join_df.show()
Please help.
Thanks.
Not sure if that is the problem but the join works for me standalone. Possibly you are missing indents in your function definition?
Did you try something like the below (note the indents)?
df1 = spark.createDataFrame(spark.sparkContext.parallelize([("123456","Rose","Yellow"),("123456","Jasmine","white"),("789654","Lily","Purple")])).toDF(*["id","name","color"])
df2 = spark.createDataFrame(spark.sparkContext.parallelize([("123456","Rose","Canada"),("123456","Jasmine","US"),("333444","Lily","Purple")])).toDF(*["id","name","Place"])
def trial(df1, df2):
df3 = df1.join(df2, ["id", "name"], how="inner")
df3.show()
trial(df1, df2)
That should result in:
+------+-------+------+------+
| id| name| color| Place|
+------+-------+------+------+
|123456|Jasmine| white| US|
|123456| Rose|Yellow|Canada|
+------+-------+------+------+
If that does not work for you perhaps enhance your question with version information or other details.

create columns in existing table and populate data using procedure

I will try to keep the query as short as possible. This involves 2 tables - lets call them staging_data and audit_data. STAGING_DATA has 3 columns:
user_no with data type number,
update_date_time with data type as date in DD-MON-YYYY HH24:MI:SS format
status_code which is varchar(1).
audit_data table also has the same 3 columns. The ask is to add 3 columns to audit_data table
seq_no (which will be unique to every user),
active_from (date type without the time format)
active_to (date type without the time format).
There is a procedure that inserts data from staging_data to audit_data.
Sample of the table audit_data
That data in audit table should look like :
For the next record for user_no 523(lets assume update_date_time is '23-Nov-2020 10:20') seq_no becomes 3, active_from_date becomes '23-Nov-2020', active_to becomes 31-Dec-99 and the active_to of user_no 523 with seq_no 2 becomes '22-Nov-2020'. So the data should look like this :
Highlighted the 3rd record which will be added later in light green.
So here goes my solution : I suggested to use row_number() over(partition by user_no) analytical function to get seq_no for each user. I wanted to create a view based on that but Boss doesn't want a view. He strictly wants to use a procedure. Procedure should check if the user_no exists (in this example 523). If exists then seq_no increases and active_to of the previous record for 523 changes to latest active_from - 1 date. I will be honest - I have no clue how to achieve this in Procedure. I understand I can create a cursor with the query I had in my mind for the view. But to add seq_no and change active_to date is something that has puzzled me. Can anyone please guide me in right direction/s? Also I apologise in advance if I have left out any other details. Its midnight here now and after 8 hours of racking my brain on this I am very hungry!
edit 11th Mar : here is the code for the procedure I wrote to insert data into the audit table for situation when a particular user_no has no record in audit table :
create or replace procedure test_aud IS
user_found_audit number;
lv_user_no AUDIT_DATA.user_no%TYPE;
cursor member_no is select distinct user_no from STAGING_DATA;
begin
open member_no;
loop
fetch member_no into lv_user_no;
exit when member_no%notfound;
select count(*) into user_found_audit from AUDIT_DATA where user_no = lv_user_no;
if user_found_audit = 0 then
insert into AUDIT_DATA(user_no, update_date_time,status_code, seq_no, last_update_date, active_from, active_to)
select user_no, update_date_time,status_code,row_number() over(partition by user_no order by UPDATE_DATE_TIME) as seqno,
to_char(trunc(update_date_time),'DD-MON-YYYY'),
to_char(trunc(update_date_time),'DD-MON-YYYY'),
lead(to_char(trunc(update_date_time)-1,'DD-MON-YYYY'),1,'31-DEC-99') over(PARTITION BY user_no ORDER BY UPDATE_DATE_TIME) from STAGING_DATA where user_no = lv_user_no;
commit;
else
dbms_output.put_line(lv_user_no||' exists in audit table');
-- to code the block when user_no exists, involves an update and insert
end if;
end loop;
close member_no;
end;
/
Well you need to collect a couple things. The latest stage row and the latest audit row. Then it is just a matter of generating the new audit information and updating the previous latest one. The following makes a couple simplifying assumptions:
Only the latest stage data for a given user_no needs processed as
all prior have been processed, However it does not assume the stage
table has been cleared.
The sequencing of 'Y' and 'N' status_codes are properly order in
that manner. In fact it does not even check the value.
It need not concern itself with the inherent race condition. The
condition is derives from seq_no being generated as Max()+1. This
structure virtually guarantees a duplicate will eventually be
created.
The nested procedure "establish_audit" does all the actual work. The rest are just supporting characters, including a couple just for debug purpose. See fiddle.
create or replace
procedure generate_stage_audit(user_no_in staging_data.user_no%type)
as
k_end_of_time constant date := date '9999-12-31';
l_latest_user_stage staging_data%rowtype;
l_latest_user_audit audit_data%rowtype;
procedure establish_audit
is
begin
insert into audit_data(user_no, update_date_time, status_code
,seq_no, active_from, active_to)
select l_latest_user_stage.user_no
, l_latest_user_stage.update_date_time
, l_latest_user_stage.status_code
, coalesce(l_latest_user_audit.seq_no,0) + 1
, trunc(l_latest_user_stage.update_date_time)
, k_end_of_time
from dual;
update audit_data
set active_to = trunc(l_latest_user_stage.update_date_time - 1)
where user_no = l_latest_user_audit.user_no
and seq_no = l_latest_user_audit.seq_no;
end establish_audit;
procedure retrieve_latest_stage
is
begin
select *
into l_latest_user_stage
from staging_data
where (user_no, update_date_time) =
( select user_no, max(update_date_time)
from staging_data
where user_no = user_no_in
group by user_no
);
end retrieve_latest_stage;
procedure retrieve_latest_audit
is
begin
select *
into l_latest_user_audit
from audit_data
where (user_no, seq_no) =
( select user_no, max(seq_no)
from audit_data
where user_no = user_no_in
group by user_no
);
exception
when no_data_found then
null;
end retrieve_latest_audit;
---- for debugging ---
procedure show_stage
is
begin
dbms_output.put_line('-------- Stage Row -------');
dbms_output.put_line(' user_no==>' || to_char(l_latest_user_stage.user_no));
dbms_output.put_line('update_date_time==>' || to_char(l_latest_user_stage.update_date_time));
dbms_output.put_line(' status_code==>' || to_char(l_latest_user_stage.status_code));
end show_stage;
procedure show_audit
is
begin
dbms_output.put_line('-------- Audit Row -------');
dbms_output.put_line(' user_no==>' || to_char(l_latest_user_audit.user_no));
dbms_output.put_line('update_date_time==>' || to_char(l_latest_user_audit.update_date_time));
dbms_output.put_line(' status_code==>' || to_char(l_latest_user_audit.status_code));
dbms_output.put_line(' seq_no==>' || to_char(l_latest_user_audit.seq_no));
dbms_output.put_line(' active_from==>' || to_char(l_latest_user_audit.active_from));
dbms_output.put_line(' active_to==>' || to_char(l_latest_user_audit.active_to));
end show_audit;
begin -- the main event
retrieve_latest_stage;
show_stage;
retrieve_latest_audit;
show_audit;
establish_audit;
end generate_stage_audit;
A couple warnings:
It seems you may be tempted to use string data type for the audit
columns Active_Form and Active_to as you are trying to declare then
"date type without the time". However there is no such data type in
Oracle; time is part of all dates. Do not do so, store them as
standard dates. (Note Dates are not stored in any format, but an
internal structure. Formats are strictly a visual representation).
Just throwaway the time with the format on the query or by setting
nls_date_format.
You may be tempted to convert call this through a trigger. Do not,
it will likely result in an "ORA-04091: Table is mutating"
exception.

Return 2 resultset from cursor based on one query (nested cursor)

I'm trying to obtain 2 different resultset from stored procedure, based on a single query. What I'm trying to do is that:
1.) return query result into OUT cursor;
2.) from this cursor results, get all longest values in each column and return that as second OUT
resultset.
I'm trying to avoid doing same thing twice with this - get data and after that get longest column values of that same data. I'm not sure If this is even possible, but If It is, can somebody show me HOW ?
This is an example of what I want to do (just for illustration):
CREATE OR REPLACE PROCEDURE MySchema.Test(RESULT OUT SYS_REFCURSOR,MAX_RESULT OUT SYS_REFCURSOR)
AS
BEGIN
OPEN RESULT FOR SELECT Name,Surname FROM MyTable;
OPEN MAX_RESULT FOR SELECT Max(length(Name)),Max(length(Surname)) FROM RESULT; --error here
END Test;
This example compiles with "ORA-00942: table or view does not exist".
I know It's a silly example, but I've been investigating and testing all sorts of things (implicit cursors, fetching cursors, nested cursors, etc.) and found nothing that would help me, specially when working with stored procedure returning multiple resultsets.
My overall goal with this is to shorten data export time for Excel. Currently I have to run same query twice - once for calculating data size to autofit Excel columns, and then for writing data into Excel.
I believe that manipulating first resultset in order to get second one would be much faster - with less DB cycles made.
I'm using Oracle 11g, Any help much appreciated.
Each row of data from a cursor can be read exactly once; once the next row (or set of rows) is read from the cursor then the previous row (or set of rows) cannot be returned to and the cursor cannot be re-used. So what you are asking is impossible as if you read the cursor to find the maximum values (ignoring that you can't use a cursor as a source in a SELECT statement but, instead, you could read it using a PL/SQL loop) then the cursor's rows would have been "used up" and the cursor closed so it could not be read from when it is returned from the procedure.
You would need to use two separate queries:
CREATE PROCEDURE MySchema.Test(
RESULT OUT SYS_REFCURSOR,
MAX_RESULT OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN RESULT FOR
SELECT Name,
Surname
FROM MyTable;
OPEN MAX_RESULT FOR
SELECT MAX(LENGTH(Name)) AS max_name_length,
MAX(LENGTH(Surname)) AS max_surname_length
FROM MyTable;
END Test;
/
Just for theoretical purposes, it is possible to only read from the table once if you bulk collect the data into a collection then select from a table-collection expression (however, it is going to be more complicated to code/maintain and is going to require that the rows from the table are stored in memory [which your DBA might not appreciate if the table is large] and may not be more performant than compared to just querying the table twice as you'll end up with three SELECT statements instead of two).
Something like:
CREATE TYPE test_obj IS OBJECT(
name VARCHAR2(50),
surname VARCHAR2(50)
);
CREATE TYPE test_obj_table IS TABLE OF test_obj;
CREATE PROCEDURE MySchema.Test(
RESULT OUT SYS_REFCURSOR,
MAX_RESULT OUT SYS_REFCURSOR
)
AS
t_names test_obj_table;
BEGIN
SELECT Name,
Surname
BULK COLLECT INTO t_names
FROM MyTable;
OPEN RESULT FOR
SELECT * FROM TABLE( t_names );
OPEN MAX_RESULT FOR
SELECT MAX(LENGTH(Name)) AS max_name_length,
MAX(LENGTH(Surname)) AS max_surname_length
FROM TABLE( t_names );
END Test;
/

How to display number of filtered records in a label [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using an access database with delphi 7 and I have records of registered students.I want a code that can display the number of males students from the database on a label in a form and also the females students on another label. The fieldname in the access database is gender while the datatypes are male and female. So if the number of male students is 20 and 30 females, it should display 20 on a label and 30 females on another label.Is there a simple code I can to do this using an adoquery1 with a datasource1 which I have used to save the records on the database?
the field names include
Firstname Othername and Gender
Something like this should do the trick.
AdoQuery1.Active:= false;
AdoQuery1.SQL.Text:= ' select gender, count(*) as cnt from atable '
+' where something = 10 '
+' group by gender '
+' order by (gender = "M") ';
AdoQuery1.Active:= true;
DataSrc:= TDataSource.Create(Self);
DataSrc.DataSet:= AdoQuery1;
DataSrc.Enabled:= true;
DataSrc.FindFirst;
if lowercase((DataSrc.FieldByName('gender')) = 'm' then begin
LabelMale.Caption:= DataSrc.FieldByName('cnt').AsString;
Success:= DataSrc.FindNext;
end
else LabelMale.Caption:= 'none';
if (Success) and (lowercase((DataSrc.FieldByName('gender')) = 'f') then begin
LabelFemale.Caption:= DataSrc.FieldByName('cnt').AsString;
end
else LabelFemale.Caption:= 'none';

Removing duplicates from an adoquery

I have a adoQuery and would like to fill a listbox with the results, but with no duplicates.
with Fdeptlayout.ADOQuery2 do
begin
sql.Clear;
sql.BeginUpdate;
sql.Add('SELECT');
sql.Add(' *');
sql.Add('FROM');
sql.Add(' `MList`');
sql.Add(' ORDER BY `Basic Name`');
sql.EndUpdate;
open;
end;
while not fdeptlayout.ADOquery2.EOF do
fdeptlayout.ListBox1.Items.Add(fdeptlayout.ADOQuery2['Basic Name']);
end;
Currently this adds 350 items to the listbox, a lot of duplicates. This is too much. How can I alter the query to remove duplicates from the result? :( Any help would be great!
You forgot "Next".
while not fdeptlayout.ADOquery2.EOF do begin
fdeptlayout.ListBox1.Items.Add(fdeptlayout.ADOQuery2['Basic Name']);
fdeptlayout.ADOquery2.Next;
end;
Without next you get an endless loop.
Change the query:
sql.Add('SELECT DISTINCT');
sql.Add(' `Basic Name`');
sql.Add('FROM');
sql.Add(' `MList`');
sql.Add(' ORDER BY `Basic Name`');
DISTINCT filters out duplicates, and by selecting only the field you need, you save fetching possibly a lot of unneeded data. Also, other fields in the records may differ, causing DISTINCT to work sub-optimal.
As a general rule: Don't use * in queries and only select the fields you actually need.
[edit]
And, as agreed in the comments, calling Fdeptlayout.ADOQuery2.Next within the while loop certainly prevents your application locking up. ;-)

Resources