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

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

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?

SQL Invalid Identifier when using bind variable

Using my anonymous block I have written below, the bind variable is giving me the error: I've pasted a picture of the table below.
edit: ETHI1022 is the input I'm giving for the CORID
v_corid CHAR(8) := ETHI1022;
*
ERROR at line 3:
ORA-06550: line 3, column 24:
PLS-00201: identifier 'ETHI1022' must be declared
ORA-06550: line 0, column 0:
PL/SQL: Compilation unit analysis terminated
I realize my inner join is most likely messed up like no other. I'm trying to learn this stuff and am having a very hard time learning how taking values from the select statement and placing them into the variables in the declare section works.
DECLARE
v_marstuid MARKS.STUID%TYPE;
v_corid CHAR(8) := &corid;
v_avgmark NUMBER(2);
v_maxmark NUMBER(2);
v_minmark NUMBER(2);
v_cordesc VARCHAR2(255);
ex_CourseNotFound EXCEPTION;
BEGIN
SELECT Count(M.stuid),
M.corid,
Avg(M.mark),
Max(M.mark),
Min(M.mark),
C.descript
INTO v_marstuid, v_corid, v_avgmark, v_maxmark,
v_minmark, v_cordesc
FROM marks M
inner join courses C
ON M.corid = C.corid
WHERE C.corid = v_corid;
dbms_output.Put_line (v_corid
|| ' - '
|| v_cordesc);
dbms_output.Put_line ('Course Stats: ');
dbms_output.Put_line ('Number of students: '
|| v_marstuid);
dbms_output.Put_line ('Average: '
|| v_avgmark);
dbms_output.Put_line ('Marks Range: ');
dbms_output.Put_line ('High: '
|| v_maxmark);
dbms_output.Put_line ('Low: '
|| v_minmark);
EXCEPTION
WHEN ex_CourseNotFound THEN
DBMS_OUTPUT.PUT_LINE(v_corcode || ' Does not exist.');
END;
/
[Table used]
Instead of ETHI1022 it should be 'ETHI1022'. Your inner join is fine. From where you are getting value of corid. Make it of type varchar/string or something like that.
Or you might try v_corid CHAR(8) := ''''||&corid||'''';

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');

Why am i getting this: PLSQL String length constraint Error

i have being working on this code for a while now and have tried everything i can think of.
This is the code block:
CREATE OR REPLACE PROCEDURE ADD_LOCATION_TO_DB (ploccode VARCHAR2, pminqty
NUMBER, pmaxqty NUMBER)AS
Err_Locode_Length EXCEPTION;
Err_Minqty_Range EXCEPTION;
Err_Maxqty_Range EXCEPTION;
Err_Maxqty_Greater_Minqty EXCEPTION;
BEGIN
IF LENGTH(ploccode) != 5 THEN
RAISE Err_Locode_Length;
ELSE IF pminqty > 10 OR pminqty < 0 THEN
RAISE Err_Minqty_Range;
ELSE IF pmaxqty > 10 OR pmaxqty < 0 THEN
RAISE Err_Maxqty_Range;
ELSE IF pminqty > pmaxqty THEN
RAISE Err_Maxqty_Greater_Minqty;
END IF;
END IF;
END IF;
END IF;
INSERT INTO LOCATION
VALUES (ploccode, pminqty, pmaxqty);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20081,'Duplicate Location ID');
WHEN Err_Locode_Length THEN
RAISE_APPLICATION_ERROR(-20082,'Location Code Lenght invalid');
WHEN Err_Minqty_Range THEN
RAISE_APPLICATION_ERROR(-20083,'Minium Qty is out of range');
WHEN Err_Maxqty_Range THEN
RAISE_APPLICATION_ERROR(-20084,'Maximum Qty is out of range');
WHEN Err_Maxqty_Greater_Minqty THEN
RAISE_APPLICATION_ERROR(-20086,'Minium Qty is Lager than Maximum Qty');
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Use Value of SQLERRM');
END;
/
CREATE OR REPLACE PROCEDURE ADD_LOCATION_VIASQLDEV AS
Dis_Msg VARCHAR2;
ploccode VARCHAR2;
pminqty NUMBER;
pmaxqty NUMBER;
BEGIN
dbms_output.put_line('--------------------------------------------------');
dbms_output.put_line('Adding location LocCode: ' || ploccode || ' MinQty: ' || pminqty || 'MaxQty' || pmaxqty);
ADD_LOCATION_TO_DB(ploccode, pminqty, pmaxqty);
dbms_output.put_line(Dis_Msg);
END;
/
When i run this set of procedures i get an error in the compiler stating:
Error(1,13): PLS-00215: String length constraints must be in range (1 .. 32767)
Error(2,14): PLS-00215: String length constraints must be in range (1 .. 32767)
I have tried adding constraints to the code that calls the main Procedure:
CREATE OR REPLACE PROCEDURE ADD_LOCATION_VIASQLDEV AS
Dis_Msg VARCHAR2(40);
ploccode VARCHAR2(5);
pminqty NUMBER(2);
pmaxqty NUMBER(2);
BEGIN
dbms_output.put_line('--------------------------------------------------');
dbms_output.put_line('Adding location LocCode: ' || ploccode || ' MinQty: ' || pminqty || 'MaxQty' || pmaxqty);
ADD_LOCATION_TO_DB(ploccode, pminqty, pmaxqty);
dbms_output.put_line(Dis_Msg);
END;
/
Adding these constrains does remove the errors with the compiler however i still revive errors when executing this testing code:
Error starting at line : 48 in command -
begin
dbms_output.put_line('Student ID: 1234567');
dbms_output.put_line('==========PART 3 TEST LOCATIONS==========================');
ADD_LOCATION_VIASQLDEV ('AF201',1,2);
ADD_LOCATION_VIASQLDEV('AF202',-3,4);
ADD_LOCATION_VIASQLDEV ('AF203',5,1);
ADD_LOCATION_VIASQLDEV ('AF204',6,7000);
ADD_LOCATION_VIASQLDEV ('AF20111',8,9);
end;
Error report -
ORA-06550: line 4, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 4, column 1:
PL/SQL: Statement ignored
ORA-06550: line 5, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
ORA-06550: line 6, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
ORA-06550: line 7, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 7, column 1:
PL/SQL: Statement ignored
ORA-06550: line 8, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 8, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Can some one please tell me what i'm doing wrong.
Apologies if this is to much information or my formating is wrong.
You are confused between the declaration section and arguments sections of the procedure.
Your first error is due to the fact that you placed your supposedly arguments in the declaration section. then when you tried to add length to it, it compiled, but you left the procedure without arguments. then you run it with four arguments which caused the error since the procedure is created accepting no arguments.
create or replace procedure_name(
parameter1 datatype, --arguments section
parameter2 datatype
) AS
/*declaration section*/
variable1 datatype(length);
variable2 datatype(length);
BEGIN
/*your code here*/
END;
try this revise script
CREATE OR REPLACE PROCEDURE ADD_LOCATION_VIASQLDEV (Dis_Msg VARCHAR2,
ploccode VARCHAR2,
pminqty NUMBER,
pmaxqty NUMBER ) AS
BEGIN
dbms_output.put_line('--------------------------------------------------');
dbms_output.put_line('Adding location LocCode: ' || ploccode || ' MinQty: ' || pminqty || 'MaxQty' || pmaxqty);
ADD_LOCATION_TO_DB(ploccode, pminqty, pmaxqty);
dbms_output.put_line(Dis_Msg);
END;
In your first set of errors, you declared the function as using a VARCHAR2 variable, but didn't state a length in brackets after it. State a length
In your second set of errors you called the function add_location_viasqldev which takes 0 arguments, but you provided 3. Provide another argument or call a different function
Also, you can make your if/else a bit neater by doing:
IF test
ELSIF test
ELSIF test
ELSE
END IF;