retrieve information using a composite variable - sql

I am trying to retrieve information using a composite variable with the following lines, but it is not working. The error report is further below, and the suppose output is at the bottom. I thank you in advance for any advise.
set serveroutput on
DECLARE
TYPE type_shopper IS RECORD(
shopperid bb_shopper.idshopper%TYPE,
firstname bb_shopper.firstname%TYPE,
lastname bb_shopper.lastname%TYPE,
email bb_shopper.email%TYPE);
rec_shopper type_shopper;
lv_days_num NUMBER(1);
lv_shopper_num NUMBER(3) :=30;
BEGIN
SELECT idshopper, firstname, lastname, email
INTO rec_shopper
FROM bb_shopper
WHERE idshopper = lv_shopper_num
lv_days_num := SYSDATE - rec_shopper.created;
lv_days_num := SYSDATE - rec_shopper.created;
DBMS_OUTPUT.PUT_LINE(rec_shopper.firstname);
DBMS_OUTPUT.PUT_LINE(rec_shopper.lastname);
DBMS_OUTPUT.PUT_LINE(rec_shopper.email);
END;
/
The error report is the following.
Error report -
ORA-06550: line 15, column 3:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 11, column 3:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
This is suppose to be the output.
Shopper ID: 37
First Name: John
Last Name: Doe
Became shopper 1 days ago

Related

PL/SQL Script - DECLARE statement - component must be declared error

So I have this script that I am trying to fix. I am supposed to declare variables and use them to print the final results. I looked at other examples and tried to write my script from what I've read.
here is what i have:
SET serveroutput ON
DECLARE
cust_name customer.cust_fname%type;
loc locations.location_name%type;
book_date booking.booking_date%type;
BEGIN
FOR rec IN (
SELECT
(customer.cust_fname || ', ' || customer.cust_sname) AS name,
locations.location_name AS location,
booking.booking_date
INTO cust_name, loc, book_date
FROM booking
INNER JOIN customer ON customer.cust_id = booking.cust_id
INNER JOIN locations ON locations.location_id = booking.location_id
WHERE booking.booking_date = '20 June 2017'
)
LOOP
dbms_output.put_line('CUSTOMER: ' || rec.cust_name);
dbms_output.put_line('LOCATION: ' || rec.loc);
dbms_output.put_line('PARTICIPANTS: ' || rec.book_date);
dbms_output.put_line('------------------------');
END LOOP;
END;
here is the errors I get:
Error report -
ORA-06550: line 19, column 50:
PLS-00302: component 'CUST_NAME' must be declared
ORA-06550: line 19, column 9:
PL/SQL: Statement ignored
ORA-06550: line 20, column 50:
PLS-00302: component 'LOC' must be declared
ORA-06550: line 20, column 9:
PL/SQL: Statement ignored
ORA-06550: line 21, column 54:
PLS-00302: component 'BOOK_DATE' must be declared
ORA-06550: line 21, column 9:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
This is the results I want from the script:
anonymous block completed
CUSTOMER: Philip, Jackson
LOCATION: Durban
PARTICIPANTS: 20 June 2017
--------------------------------------------------
CUSTOMER: Sarah, Jones
LOCATION: Cape Town
PARTICIPANTS: 20 June 2017
--------------------------------------------------
The script works well if i simply don't use the declarations, but due to the circumstances I have to.
I am completely lost as to why this doesn't work with the declarations. Any help will be appreciated!
In a loop you can't select .. into, only select is possible. And using loop makes variable not needed at all. But since you're somehow forced to use variables, you may use them within loop.
SET serveroutput ON
DECLARE
cust_name customer.cust_fname%type;
loc locations.location_name%type;
book_date booking.booking_date%type;
BEGIN
FOR rec IN (
SELECT
(customer.cust_fname || ', ' || customer.cust_sname) AS cust_name,
locations.location_name,
booking.booking_date
FROM booking
INNER JOIN customer ON customer.cust_id = booking.cust_id
INNER JOIN locations ON locations.location_id = booking.location_id
WHERE booking.booking_date = '20 June 2017'
)
LOOP
cust_name := rec.cust_mame;
loc := rec.location_name;
book_date := rec.booking_date;
dbms_output.put_line('CUSTOMER: ' || cust_name);
dbms_output.put_line('LOCATION: ' || loc);
dbms_output.put_line('PARTICIPANTS: ' || book_date);
dbms_output.put_line('------------------------');
END LOOP;
END;
Can it be that you've been told to use collections, not variables?

Getting a "Not a group function error" PLSQL

I have to create a block that receives a region via prompt and returns its number of countries. If the region id is negative and no data is found, have to raise exceptions
DECLARE
region Regions.Region_Name%type := '&Insert_the_name_of_the_region';
id_region Regions.Region_Id%type;
negative_exception exception;
countries_nr number;
BEGIN
select r.region_id into id_region, count(c.country_id) into countries_nr from countries c inner join regions r on c.region_id=r.region_id where r.region_name = region group by r.region_id;
If regiao_id < 0 Then
raise negativo_exception;
Else
DBMS_OUTPUT.PUT_LINE(countries_nr);
End If;
EXCEPTION
WHEN No_data_found then
dbms_output.put_line('No country found on region'||region);
WHEN negative_exception THEN
dbms_output.put_line('Region with negative ID');
END;
/
Im getting the error
ORA-06550: line 8, column 36:
PL/SQL: ORA-00934: group function is not allowed here
ORA-06550: line 8, column 1:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
You are using INTO incorrectly.
Use it as follows:
select r.region_id , count(c.country_id)
into id_region , countries_nr
.....

Wrong number or types of arguments error using cursors with parameters

I am getting this error trying to have an input parameter "var_city" in my cursor so I can use the user's input of a city.
Error report -
ORA-06550: line 17, column 5:
PLS-00306: wrong number or types of arguments in call to 'CUSTOMERS'
ORA-06550: line 17, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
p_city is commented out because I was trying to use that input parameter as the value used but it didn't work as well.
SET SERVEROUTPUT ON
SET VER OFF
-- ACCEPT p_city PROMPT 'Enter city name: '
DECLARE
v_custname si.customer.custname%TYPE;
v_total_spent NUMBER;
v_total_cars NUMBER;
CURSOR customers (var_city varchar2) IS
SELECT
custname
FROM
si.customer
WHERE
UPPER(custcity) = UPPER(var_city);
BEGIN
dbms_output.put_line('Customer Name'
|| 'Total Cars'
|| 'Total Spent');
OPEN customers;
LOOP
FETCH customers INTO v_custname;
EXIT WHEN customers%NOTFOUND;
cust_total_spending_and_cars(v_custname, v_total_cars, v_total_spent);
dbms_output.put_line(v_custname
|| v_total_cars
|| v_total_spent);
END LOOP;
CLOSE customers;
END;
/
An argument must be passed to the OPEN statement
Change this line:
OPEN customers;
To something like this:
OPEN customers('city name here');

Xml bulk insert in oracle table

Im new to oracle,
what im trying is,
I have an xml, i try to insert the same in a oracle database table, i have formed a query, when i try to insert it. i get some Error like
Error report -
ORA-06550: line 35, column 84:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 5, column 2:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
i couldnt able to figure out what i missed, Suggest me how to change the query,
heres my XML and Query which im trying to work.
- <call>
- <callSummary>
<indId>100</indId>
<notificationNo>notification</notificationNo>
<orderNo>orderno</orderNo>
</callSummary>
- <callList>
- <callDetails>
<maintenancetype>1</maintenancetype>
<serialNo>1</serialNo>
<unitType>unit type</unitType>
</callDetails>
- <callDetails>
<maintenancetype>1</maintenancetype>
<serialNo>2</serialNo>
<unitType>unit type</unitType>
</callDetails>
- <callDetails>
<maintenancetype>2</maintenancetype>
<serialNo>1</serialNo>
<unitType>unit type</unitType>
</callDetails>
- <callDetails>
<maintenancetype>2</maintenancetype>
<serialNo>2</serialNo>
<unitType>unit type</unitType>
</callDetails>
</callList>
</call>
my Query is
DECLARE
call_xml XMLTYPE := xmltype('<call><callSummary><indId>100</indId><notificationNo>notification</notificationNo><orderNo>orderno</orderNo><</callSummary><callList><callDetails><maintenancetype>1</maintenancetype><serialNo>1</serialNo><unitType>unit type</unitType></callDetails><callDetails><maintenancetype>1</maintenancetype><serialNo>2</serialNo><unitType>unit type</unitType></callDetails><callDetails><maintenancetype>2</maintenancetype><serialNo>1</serialNo><unitType>unit type</unitType></callDetails><callDetails><maintenancetype>2</maintenancetype><serialNo>2</serialNo><unitType>unit type</unitType></callDetails></callList></call>');
BEGIN
INSERT INTO ORDER_DETAILS (
IND_ID,NOTIFICATION_NO,ORDER_NO,MAINT_TYPE,SERIAL_NO,UNIT_TYPE)
SELECT
call_xml.value('call/callSummary/indId[1]','CLNT(3)'),
call_xml.value('call/callSummary/notificationNo[1]','CHAR(12)'),
call_xml.value('call/callSummary/orderNo[1]','CHAR(12)'),
call_xml.value('call/callSummary/callList/callDetails/maintenancetype[1]','CHAR(1)'),
call_xml.value('call/callSummary/callList/callDetails/serialNo[1]','CHAR(1)'),
call_xml.value('call/callSummary/callList/callDetails/unitType[1]','CHAR(20)') from call_xml.nodes('call');
END;
I hope you understand my question, Thanx in advance.
You'll want to use the xmlsequence function. It will allow you to select a list of nodes from your XML object. If you'd like to use pl/sql, the replace xmltype, with your variable.
SELECT
extractValue(column_value,'callSummary/indId[1]'),
extractValue(column_value,'callSummary/notificationNo[1]'),
extractValue(column_value,'callSummary/orderNo[1]'),
extractValue(column_value,'callSummary/callList/callDetails/maintenancetype[1]'),
extractValue(column_value,'callSummary/callList/callDetails/serialNo[1]'),
extractValue(column_value,'callSummary/callList/callDetails/unitType[1]') from table (
xmlsequence(
extract(
xmltype('<call><callSummary><indId>100</indId><notificationNo>notification</notificationNo><orderNo>orderno</orderNo></callSummary><callList><callDetails><maintenancetype>1</maintenancetype><serialNo>1</serialNo><unitType>unit type</unitType></callDetails><callDetails><maintenancetype>1</maintenancetype><serialNo>2</serialNo><unitType>unit type</unitType></callDetails><callDetails><maintenancetype>2</maintenancetype><serialNo>1</serialNo><unitType>unit type</unitType></callDetails><callDetails><maintenancetype>2</maintenancetype><serialNo>2</serialNo><unitType>unit type</unitType></callDetails></callList></call>'),'/call/callSummary')));

Alias required in SELECT list of cursor to avoid duplicate column names

I am having an issue doing a plsql command. The idea is to show the courier, location, and yes or not if it is over 20.
DECLARE
yesnobool VARCHAR(3);
CURSOR orders_list IS
SELECT shipping.ship_courier,
shipping.weightpership,
shipping.shipping#,
purchaseorder.shipping#,
storelocation.location#,
STORELOCATION.LOC_CITY,
purchaseorder.location#
FROM shipping,
purchaseorder,
storelocation
WHERE purchaseorder.shipping# = shipping.shipping#
and storelocation.location# = purchaseorder.location#;
BEGIN
dbms_output.Put_line('Courier || Location || Too Heavy for 1 person lift?');
dbms_output.Put_line('------ -------- -----------------------------------');
FOR an_order IN orders_list LOOP
IF SHIPPING.WEIGHTPERSHIP > 20 THEN
yesnobool := 'YES';
ELSE
yesnobool := 'NO';
END IF;
dbms_output.Put_line(Rpad(an_order.ship_courier, 6) || ' ' ||
Rpad(an_order.loc_city, 8) || ' ' ||
Rpad(yesnobool, 19));
END LOOP;
END;
The Error message i am getting is:
Error report -
ORA-06550: line 21, column 2:
PLS-00402: alias required in SELECT list of cursor to avoid duplicate column names
ORA-06550: line 21, column 2:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Im not very good with plsql so i dont really know how to pin down the issue. Thank you in advance for any and all advice.
Columns shipping# and location# are duplicated in your select field list.
shipping.shipping#
purchaseorder.shipping#
storelocation.location#,
purchaseorder.location#
The cursor is unable to distinguish which shipping or location its supposed to use.
The solution is to alias the fields. Something like this:
shipping.shipping# as shipping,
purchaseorder.shipping# as poshipping
And:
storelocation.location# as storeLocation,
purchaseorder.location# as poLocation