how to use multi select ? (merged 2) [message #408299] |
Mon, 15 June 2009 12:00  |
pure_colors74
Messages: 9 Registered: June 2009
|
Junior Member |
|
|
hi all,
i'm new in pl sql issue and i'm trying to get tel numbers from adsltest table using cursor then use the tel in another select statment to get DOWN_EQPT_ID where the telno in ccbs.pairs_status_view table = tel_no in adsltest.
this is argent question anyone can help please?
DECLARE
V_Eqp varchar(40);
tel varchar(40) := & value_of_tel;
V_Sec varchar(40);
V_Third varchar(40);
a varchar(40);
Begin
Cursor Telcursor is
SELECT tel_no
FROM adsltest;
OPEN Telcursor;
loop
FETCH Telcursor INTO a;
EXIT WHEN Telcursor%NOTFOUND;
SELECT DOWN_EQPT_ID
INTO V_Eqp
FROM ccbs.pairs_status_view
WHERE TELNO= a.tel_no AND cable_name_down is null;
DBMS_OUTPUT.PUT_LINE(V_Eqp) ;
end loop;
end;
|
|
|
|
|
Re: how to use multi select ? (merged 2) [message #408305 is a reply to message #408299] |
Mon, 15 June 2009 12:28   |
pure_colors74
Messages: 9 Registered: June 2009
|
Junior Member |
|
|
thanks for your replay.
i don't think that i can do that in one select because the tel_no in adsltest table is a set of telno found in ccbs.pairs_status_view table, so i can't say adsltest.tel_no=ccbs.pairs_status_view.telno. i get one tel number from adsltest table and check if its found in the other table to get DOWN_EQPT_ID value for existing table.
what is more, i'll use the DOWN_EQPT_ID value to get another one from another table and do the same for fourth table.
[Updated on: Mon, 15 June 2009 12:58] Report message to a moderator
|
|
|
|
Re: how to use multi select ? (merged 2) [message #408313 is a reply to message #408299] |
Mon, 15 June 2009 13:58   |
pure_colors74
Messages: 9 Registered: June 2009
|
Junior Member |
|
|
here is all my code, i know i have many errors but i'm trying to make the first part correct so the rest will be esaier.
DECLARE
V_Eqp varchar(40);
tel varchar(40);
V_Sec varchar(40);
V_Third varchar(40);
a varchar(40);
Begin
Cursor Telcursor is
SELECT tel_no
FROM adsltest;
OPEN Telcursor;
loop
FETCH Telcursor INTO a;
EXIT WHEN Telcursor%NOTFOUND;
SELECT DOWN_EQPT_ID
INTO V_Eqp
FROM ccbs.pairs_status_view
WHERE TELNO= a.tel_no AND cable_name_down is null;
DBMS_OUTPUT.PUT_LINE(V_Eqp) ;
Select GIS_EQUIPMENT_NAME
Into V_Sec
From ccbs.equipment_conversion
Where CCBS_EQPT_ID = V_EQP;
DBMS_OUTPUT.PUT_LINE(V_Sec) ;
select ADSL_SERVICE
Into V_Third
From ne.equipment
where EQUIPMENT_name = V_Sec;
DBMS_OUTPUT.PUT_LINE(V_Third) ;
update testadsl
set adsl_value
where a.tel_no = ccbs.pairs_status_view.telno
End;
end loop
CLOSE Telcursor;
end;
|
|
|
|
Re: how to use multi select ? (merged 2) [message #408401 is a reply to message #408299] |
Tue, 16 June 2009 02:17   |
|
Faten ,
We really dint understand the things what you gonna supposed to do.
Please make sure that your test case is proper before asking us for help.
See in your code you have following mistakes..
1. you are declaring telcursor in executable section
2. we dont understand why you are using cursor here
3. in the select query you are referencing a variable by a.tel_no,'a' is the name of the variable ,instead of variable you can reference the cursor.
code you have written
SELECT DOWN_EQPT_ID
INTO V_Eqp
FROM ccbs.pairs_status_view
WHERE TELNO= a.tel_no AND cable_name_down is null;
DBMS_OUTPUT.PUT_LINE(V_Eqp) ;
You have to rewrite as
SELECT DOWN_EQPT_ID
INTO V_Eqp
FROM ccbs.pairs_status_view
WHERE TELNO= Telcursor.a AND cable_name_down is null;
DBMS_OUTPUT.PUT_LINE(V_Eqp) ;
4.in the next 2 select statements you have not assigned any value to v_seq and v_eqp
|
|
|
Re: how to use multi select ? (merged 2) [message #408929 is a reply to message #408401] |
Thu, 18 June 2009 06:13   |
pure_colors74
Messages: 9 Registered: June 2009
|
Junior Member |
|
|
sorry for bothering,
i'm new and must done many mistakes, i keep trying and now my code is working but with wrong result.
i'll try to explain what i want to do again in more detils, please be pataint with me 
i have four tables:
1- is adsl_test( this table contains two feilds
one is thetelephone number and the other is empty, i have to
fill it with ADSL_SERVICE value.
2- the second table is (ccbs.pairs_status_view)
its feilds:DOWN_EQPT_ID,TELNO
I want to select DOWN_EQPT_ID where tel_no in first table
= telno in ccbs.pairs_status_view.
3-third table is (ccbs.equipment_conversion)and the
feild for this table is GIS_EQUIPMENT_NAME,CCBS_EQPT_ID.
i want to find the GIS_EQUIPMENT_NAME where
the DOWN_EQPT_ID i selected previously = CCBS_EQPT_ID.
(note:selection from this table might be null)
4- the forth table is (ne.equipment)and
has ADSL_SERVICE,EQUIPMENT_NAME
I want to find ADSL_SERVICE where EQUIPMENT_NAME=GIS_EQUIPMENT_NAME i selected previously.
finally i want to update the ADSL_TEST table to fill adsl_service for telno found within.
i'll listed my code then the result i get
Declare
reqTelNo varchar(40);
Eqpt_id varchar(40);
ccbcEqpt_id varchar(40);
GisEqpt varchar(40);
GisEqptTwo varchar(40);
Adsl varchar(40);
gisTelNumber varchar(40);
cursor c5 is
select tel_no,ADSL_SPEED
from adsl_test;
cursor c6 is
select DOWN_EQPT_ID,telno
from ccbs.pairs_status_view
where telno = reqTelNo and cable_name_down is null;
cursor c7 is
Select GIS_EQUIPMENT_NAME , CCBS_EQPT_ID
From ccbs.equipment_conversion
Where Eqpt_id = CCBS_EQPT_ID;
cursor c8 is
select ADSL_SERVICE, EQUIPMENT_name
From ne.mv_equipment
Where EQUIPMENT_name= GisEqpt ;
begin
open c5;
loop
fetch c5 into reqTelNo, Adsl;
exit when c5%notfound;
dbms_output.put_line(chr(10)|| reqTelNo);
begin
open c6;
loop
fetch c6 into Eqpt_id, gisTelNumber;
exit when c6%notfound;
--dbms_output.put_line (Eqpt_id || gisTelNumber);
end loop;
close c6;
end;
begin
open c7;
loop
fetch c7 into GisEqpt, ccbcEqpt_id;
exit when c7%notfound;
--dbms_output.put_line (GisEqpt || ccbcEqpt_id);
End loop;
Close c7;
End;
begin
open c8;
loop
fetch c8 into Adsl, GisEqptTwo;
exit when c8%notfound;
--dbms_output.put_line (GisEqpt || ccbcEqpt_id);
if Adsl = null then
dbms_output.put_line (GisEqpt || GisEqptTwo);
Else
Update adsl_test
Set ADSL_SPEED= Adsl
Where GisEqpt = GisEqptTwo;
End if;
End loop;
Close c8;
End;
end loop;
close c5;
end;
[Updated on: Thu, 18 June 2009 06:19] Report message to a moderator
|
|
|
|
Re: how to use multi select ? (merged 2) [message #408936 is a reply to message #408299] |
Thu, 18 June 2009 06:52   |
cookiemonster
Messages: 13965 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Some thoughts:
1) Cursor For Loops are wonderful things - you should use them
2) Calling cursors c<number> just makes it harder to follow the code, make the names descriptive of what they do.
3) Variables should be typed to the column that's being used to populate them i.e.
reqTelNo adsl_test.tel_no%TYPE;
4) Variables should be named with a prefix so you can tell at glance they're not column names.
5) The Adsl variable is being populated by two different cursors - that's only going to confuse matters. In fact I'm not sure why you're selecting ADSL_SPEED in c5 you don't need it.
6) Most of those loops almost certainly shouldn't be loops. Either those cursors only find one row each time, in which case you should use select into, or they find multiple rows and the variables you're fetching them into are being constantly overwritten.
7) This can almost certainly be done in a single update statement with no cursors needed.
|
|
|
Re: how to use multi select ? (merged 2) [message #408937 is a reply to message #408935] |
Thu, 18 June 2009 06:56   |
cookiemonster
Messages: 13965 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Littlefoot wrote on Thu, 18 June 2009 12:47 |
I don't think so; thisis invalid and prevents your PL/SQL block to compile.
|
Since when has that been a compile error?
It'll never equate to true for sure and needs fixing but it will compile.
|
|
|
Re: how to use multi select ? (merged 2) [message #408940 is a reply to message #408935] |
Thu, 18 June 2009 07:14   |
pure_colors74
Messages: 9 Registered: June 2009
|
Junior Member |
|
|
i didn't have any error about this statement !! i don't think that i can use select statement only.... look for this
Select adsl_test.tel_no,adsl_test.adsl_speed,ccbs.pairs_status_view.down_eqpt_id, ccbs.pairs_status_view.telno, ccbs.equipment_conversion. GIS_EQUIPMENT_NAME, ccbs.equipment_conversion. CCBS_EQPT_ID, ne.mv_equipment. ADSL_SERVICE, ne.mv_equipment. EQUIPMENT_name
From adsl_test, ccbs.pairs_status_view, ccbs.equipment_conversion, ne.mv_equipment
Where adsl_test.tel_no = ccbs.pairs_status_view.telno and and cable_name_down is null and ____!!!;
it is about where condition, i don't have a primary or forign keys so i can't say (Where adsl_test.tel_no = ccbs.pairs_status_view.telno).
so i need to get value each time and use it with the next statement .
what is wrong with using this cursor? i thought it is what i have to do?!!
|
|
|
|
Re: how to use multi select ? (merged 2) [message #408948 is a reply to message #408940] |
Thu, 18 June 2009 07:42   |
cookiemonster
Messages: 13965 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
pure_colors74 wrote on Thu, 18 June 2009 13:14 |
Select adsl_test.tel_no,adsl_test.adsl_speed,ccbs.pairs_status_view.down_eqpt_id, ccbs.pairs_status_view.telno, ccbs.equipment_conversion. GIS_EQUIPMENT_NAME, ccbs.equipment_conversion. CCBS_EQPT_ID, ne.mv_equipment. ADSL_SERVICE, ne.mv_equipment. EQUIPMENT_name
From adsl_test, ccbs.pairs_status_view, ccbs.equipment_conversion, ne.mv_equipment
Where adsl_test.tel_no = ccbs.pairs_status_view.telno and and cable_name_down is null and ____!!!;
it is about where condition, i don't have a primary or forign keys so i can't say (Where adsl_test.tel_no = ccbs.pairs_status_view.telno).
|
Are you saying there are multiple rows in ccbs.pairs_status_view for each row in adsl_test?
Because if there are what you are doing isn't going to work.
This bit of code:
begin
open c6;
loop
fetch c6 into Eqpt_id, gisTelNumber;
exit when c6%notfound;
--dbms_output.put_line (Eqpt_id || gisTelNumber);
end loop;
close c6;
Loops over all the rows in the cursor and overwrites the variables with the new values each time.
Since your cursor doesn't have an order by, the values you end up with when the loop has finished - to be used by the next cursor - are random
(out of the possible values returned by the cursor of course).
That being the case you have no way of telling what values the adsl_test table is eventually going to be updated with.
Using cursors isn't necessary. Knowing exactly which value from which row you're going to use for your update is.
|
|
|
Re: how to use multi select ? (merged 2) [message #408951 is a reply to message #408945] |
Thu, 18 June 2009 07:51   |
 |
Littlefoot
Messages: 21823 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Hm, OK, I'm talking nonsense but here you are: first, small test case based on one of your previous messages:SQL> create table adsl_test
2 (telno varchar2(20),
3 adsl_service varchar2(20)
4 );
Table created.
SQL> insert into adsl_test (telno, adsl_service) values ('123-456', null);
1 row created.
SQL> create table pairs_status_View
2 (down_eqpt_id number,
3 telno varchar2(20)
4 );
Table created.
SQL> insert into pairs_status_view (down_eqpt_id, telno) values (123, '123-456');
1 row created.
SQL> create table equipment_conversion
2 (gis_equipment_name varchar2(20),
3 ccbs_eqpt_id number
4 );
Table created.
SQL> insert into equipment_conversion (gis_equipment_name, ccbs_eqpt_id) values ('ES name', 123);
1 row created.
SQL> create table equipment
2 (adsl_service varchar2(20),
3 equipment_name varchar2(20)
4 );
Table created.
SQL> insert into equipment (adsl_service, equipment_name) values ('I want this AS', 'ES name');
1 row created.
SQL>
A single update statement:SQL> update adsl_test at set
2 at.adsl_service = (select e.adsl_service
3 from equipment e
4 where e.equipment_name = (select ec.gis_equipment_name
5 from equipment_conversion ec
6 where ec.ccbs_eqpt_id = (select psv.down_eqpt_id
7 from pairs_status_view psv
8 where psv.telno = at.telno
9 )
10 )
11 );
1 row updated.
SQL>
It results withSQL> select * from adsl_test;
TELNO ADSL_SERVICE
-------------------- --------------------
123-456 I want this AS
SQL>
Is this something you'd want to do?
|
|
|
Re: how to use multi select ? (merged 2) [message #408960 is a reply to message #408299] |
Thu, 18 June 2009 08:05   |
cookiemonster
Messages: 13965 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Hmmm.....
Update adsl_test
Set ADSL_SPEED= Adsl
Where GisEqpt = GisEqptTwo;
Aren't GisEqpt and GisEqptTwo local variables?
If so this update will either update every row in adsl_test or none.
Shouldn't that be:
IF GisEqpt = GisEqptTwo THEN
UPDATE adsl_test
SET adsl_speed = adsl
WHERE tel_no = reqTelNo;
END IF;
You really should prefix those variables.
|
|
|
|
|
|
Re: how to use multi select ? (merged 2) [message #410728 is a reply to message #410567] |
Tue, 30 June 2009 00:25   |
pure_colors74
Messages: 9 Registered: June 2009
|
Junior Member |
|
|
pure_colors74 wrote on Mon, 29 June 2009 00:33 | Littelfoot the code you sent is what i'm looking for, but i still have problems with the code. see the code i used first but i don't get any result, looks like the code hang up..
update adsl_test at
set at.adsl_speed = (select e.adsl_service
from ne.mv_equipment e, adsl_test at
where e.equipment_name = (select ec.gis_equipment_name
from equipment_conversion ec
where ec.ccbs_eqpt_id in (select distinct psv.down_eqpt_id
from ccbs.pairs_status_view psv
where at.tel_no in psv.telno and psv.cable_name_down is null
)
)
)
Where at.tel_no in (select telno from ccbs.pairs_status_view);
i tried to test my code by just execute the select statment but i got (NO ROWS SELECTED)
select e.adsl_service, b.tel_no
from ne.mv_equipment e, adsl_test b
where e.equipment_name = (select ec.gis_equipment_name
from equipment_conversion ec
where ec.ccbs_eqpt_id in (select distinct psv.down_eqpt_id
from ccbs.pairs_status_view psv
where psv.cable_name_down is null and b.tel_no = '064616351'
)
);
finally i tried to not use adsl_test table and i got the result i want !!
select e.adsl_service
from ne.mv_equipment e
where e.equipment_name = (select ec.gis_equipment_name
from equipment_conversion ec
where ec.ccbs_eqpt_id in (select distinct psv.down_eqpt_id
from ccbs.pairs_status_view psv
where psv.cable_name_down is null and telno = '064616351'
)
);
|
this is the discription of tables
1-ADSL_TEST;
TEL_NO,ADSL_SPEED
2-ccbs.pairs_status_view;
DOWN_EQPT_ID,TELNO, CABLE_NAME_DOWN
3-ccbs.equipment_conversion;
GIS_EQUIPMENT_NAME,CCBS_EQPT_ID.
4- ne.equipment;
ADSL_SERVICE,EQUIPMENT_NAME
|
|
|
|
|
Re: how to use multi select ? (merged 2) [message #410909 is a reply to message #410775] |
Wed, 01 July 2009 00:55   |
pure_colors74
Messages: 9 Registered: June 2009
|
Junior Member |
|
|
this is my queastion, update dose not work, i left the code runing for hours but i don't get any result, seems like it hang.
update adsl_test at
set at.adsl_speed = (select e.adsl_service
from ne.mv_equipment e
where e.equipment_name = (select ec.gis_equipment_name
from equipment_conversion ec
where ec.ccbs_eqpt_id in (select distinct psv.down_eqpt_id
from ccbs.pairs_status_view psv
where at.tel_no in psv.telno and psv.cable_name_down is null
)
)
)
Where at.tel_no in (select psv.telno from ccbs.pairs_status_view psv where at.tel_no =psv.telno );
by the way some select statment might not return any values, and some might have more than one by mistake !!
is this what cause this ?
|
|
|
Re: how to use multi select ? (merged 2) [message #411000 is a reply to message #410909] |
Wed, 01 July 2009 08:17   |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
pure_colors74 wrote on Wed, 01 July 2009 01:55 |
by the way some select statment might not return any values, and some might have more than one by mistake !!
is this what cause this ?
|
No, for example if you SELECT returns more than one row, it will not run long, it will return ORA-1422 error.
|
|
|
|