Home » SQL & PL/SQL » SQL & PL/SQL » How to get value of table (oracle 11g)
How to get value of table [message #640863] |
Thu, 06 August 2015 03:25 |
realbassist
Messages: 9 Registered: March 2008
|
Junior Member |
|
|
Hi all,
I have some problem.
i have this table
table abc
(internalid varchar2(20),
vCol1 varchar2(20),
nCol2 Number,
dCol3 date,
vCol4 varchar2(20)
)
Data of table abc
internalid vCol1 nCol2 dCol3 vCol4
----------------------------------------------------------------------------
11111 aaaa 101 20-may-2015 xxxxx
11112 bbbb 102 02-may-2015 zzzzz
11113 cccc 103 10-may-2015 yyyyy
Input parameter : 11112
So i want to store into array variable like this :
i(vCol1) ==> value aaaa -- data type varchar2
i(ncol2) ==> value 101 -- data type number
i(dcol3) ==> value 101 -- data type date
i have this parameter table
PARAM_TABLE
ID Source_Col
----------------------------
A1 vCol1
A2 nCol2
A3 dCol3
now i use this parameter table to get value
Parameter internalid = 11111
so i lookup table abc to get those value
vCol1 = aaaa
nCol2 = 101
dCol3 = 20-may-2015
Can anybody help me how to write in plsql procedure ??
Thanks for your help.
|
|
|
|
Re: How to get value of table [message #640872 is a reply to message #640867] |
Thu, 06 August 2015 04:11 |
realbassist
Messages: 9 Registered: March 2008
|
Junior Member |
|
|
Hi this is the code
create table abc
(internalid varchar2(20),
vCol1 varchar2(20),
nCol2 Number,
dCol3 date,
vCol4 varchar2(20)
);
Insert into ABC
(INTERNALID, VCOL1, NCOL2, DCOL3, VCOL4)
Values
('11112', 'bbbb', 102, TO_DATE('05/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
'yyyyyy');
Insert into ABC
(INTERNALID, VCOL1, NCOL2, DCOL3, VCOL4)
Values
('11111', 'aaaa', 101, TO_DATE('05/20/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
'xxxxxx');
COMMIT;
create table abc_param
(id varchar2(20),
source_col varchar2(200)
);
Insert into ABC_PARAM
(ID, SOURCE_COL)
Values
('111', 'VCOL1');
Insert into ABC_PARAM
(ID, SOURCE_COL)
Values
('222', 'NCOL2');
Insert into ABC_PARAM
(ID, SOURCE_COL)
Values
('333', 'DNCOL3');
COMMIT;
[code]
DECLARE
TYPE EmpCurTyp IS REF CURSOR;
c EmpCurTyp;
emp_rec abc%ROWTYPE;
stmt_str VARCHAR2 (2000);
--e_job emp.job%TYPE;
dml_str VARCHAR2 (200);
z VARCHAR2 (200);
vParamID VARCHAR2 (200);
BEGIN
vParamID := '11111';
stmt_str := 'SELECT * FROM abc WHERE internalid = :1';
-- in a multi-row query
OPEN c FOR stmt_str USING vParamID;
LOOP
FETCH c INTO emp_rec;
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE c;
DBMS_OUTPUT.
put_line (
'aaaa = '
|| emp_rec.internalid
|| ' --- '
|| emp_rec.vCol1
|| ' -- '
|| emp_rec.nCol2);
FOR i IN (SELECT * FROM ABC_PARAM)
LOOP
--z := emp_rec.[i.SOURCE_COL]; --This part i want to ask the code ....
DBMS_OUTPUT.put_line (i.source_col || ' = ' || z);
END LOOP;
END;
/
[code]
My expected result is
VCOL1 = aaaa
NCOL2 = 101
DCOL3 = 20-MAY-2015
Thanks
|
|
|
|
Re: How to get value of table [message #640879 is a reply to message #640872] |
Thu, 06 August 2015 04:42 |
realbassist
Messages: 9 Registered: March 2008
|
Junior Member |
|
|
Hi this is the code
create table abc
(internalid varchar2(20),
vCol1 varchar2(20),
nCol2 Number,
dCol3 date,
vCol4 varchar2(20)
);
Insert into ABC
(INTERNALID, VCOL1, NCOL2, DCOL3, VCOL4)
Values
('11112', 'bbbb', 102, TO_DATE('05/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
'yyyyyy');
Insert into ABC
(INTERNALID, VCOL1, NCOL2, DCOL3, VCOL4)
Values
('11111', 'aaaa', 101, TO_DATE('05/20/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
'xxxxxx');
COMMIT;
create table abc_param
(id varchar2(20),
source_col varchar2(200)
);
Insert into ABC_PARAM
(ID, SOURCE_COL)
Values
('111', 'VCOL1');
Insert into ABC_PARAM
(ID, SOURCE_COL)
Values
('222', 'NCOL2');
Insert into ABC_PARAM
(ID, SOURCE_COL)
Values
('333', 'DNCOL3');
COMMIT;
DECLARE
TYPE empcurtyp IS ref CURSOR;
c EMPCURTYP;
emp_rec abc%ROWTYPE;
stmt_str VARCHAR2 (2000);
--e_job emp.job%TYPE;
dml_str VARCHAR2 (200);
z VARCHAR2 (200);
vparamid VARCHAR2 (200);
BEGIN
vparamid := '11111';
stmt_str := 'SELECT * FROM abc WHERE internalid = :1';
-- in a multi-row query
OPEN c FOR stmt_str USING vparamid;
LOOP
FETCH c INTO emp_rec;
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE c;
dbms_output. Put_line ('aaaa = '
|| emp_rec.internalid
|| ' --- '
|| emp_rec.vcol1
|| ' -- '
|| emp_rec.ncol2);
FOR i IN (SELECT *
FROM abc_param) LOOP
--z := emp_rec.[i.SOURCE_COL]; --This part i want to ask the code ....
dbms_output.Put_line (i.source_col
|| ' = '
|| z);
END LOOP;
END;
My expected result is
VCOL1 = aaaa
NCOL2 = 101
DCOL3 = 20-MAY-2015
Thanks
CM: changed quote to code tags.
[Updated on: Thu, 06 August 2015 04:50] by Moderator Report message to a moderator
|
|
|
Re: How to get value of table [message #640881 is a reply to message #640863] |
Thu, 06 August 2015 05:06 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
If you use SQL*Plus you can use something like that.
C:\>type t.sql
col cols new_value cols noprint
set feedback off
select listagg(source_col,',') within group (order by id) cols from abc_param;
set feedback on
select &cols from abc where INTERNALID = &id;
SQL> @c:\t
Enter value for id: 11111
VCOL1 NCOL2 DCOL3
-------------------- ---------- -----------
aaaa 101 20-MAY-2015
1 row selected.
SQL> @c:\t
Enter value for id: 11112
VCOL1 NCOL2 DCOL3
-------------------- ---------- -----------
bbbb 102 02-MAY-2015
1 row selected.
[Updated on: Thu, 06 August 2015 05:06] Report message to a moderator
|
|
|
Re: How to get value of table [message #640883 is a reply to message #640881] |
Thu, 06 August 2015 05:20 |
realbassist
Messages: 9 Registered: March 2008
|
Junior Member |
|
|
Hi Michel,
Thanks for reply.
I am sorry, my expected result is not 1 row value.
In this code
FOR i IN (SELECT *
FROM abc_param) LOOP
--z := emp_rec.[i.SOURCE_COL]; --This part i want to ask the code ....
dbms_output.Put_line (i.source_col
|| ' = '
|| z);
END LOOP;
I want to print as below using dbms_output.put_line using plsql procedure :
VCOL1 = aaaa
NCOL2 = 101
DCOL3 = 20-MAY-2015
So i want to get the value or column from table ABC_PARAM.
Thanks
|
|
|
|
|
Re: How to get value of table [message #640938 is a reply to message #640889] |
Fri, 07 August 2015 02:43 |
realbassist
Messages: 9 Registered: March 2008
|
Junior Member |
|
|
When i compile the procedure, it show this error "PL/SQL: ORA-00923: FROM keyword not found where expected" in line 8.
Actually, i have stored the value in variable emp_rec which contain all the column of table abc;
OPEN c FOR stmt_str USING vparamid;
LOOP
FETCH c INTO emp_rec;
EXIT WHEN c%NOTFOUND;
END LOOP;
Now i want to get the value of that column based on abc_param without select 1 by 1,
so i the app_param, column Source_col = VCOL1
then i will get this value 'aaaa'
FOR i IN (SELECT *
FROM abc_param) LOOP
--z := emp_rec.[i.SOURCE_COL]; --This part i want to ask the code ....
dbms_output.Put_line (i.source_col
|| ' = '
|| z);
END LOOP;
on the above script :
in abc_param , the value are :
ID SOURCE_COL
333 DCOL3
111 VCOL1
222 NCOL2
so
for the 1st loop
i get i.source_col = 'DCOL3'
and i need to assign the value the variable z= i.source_col = '20-may-2015'
later i will use variable z to other process.
example if z < trunc(sysdate) then 'Yesterday' else 'Today'
for the 2nd loop
i get i.source_col = 'VCOL1'
and i need to assign the value the variable z= i.source_col = aaaa'
later i will use
later i will use variable z to other process.
example if z='aaaa' then 'same name' else 'different Name'
for the 3rd loop
i get i.source_col = 'NCOL2'
and i need to assign the value the variable z= i.source_col = 101
later i will use
later i will use variable z to other process.
example if z >100 then 'Fat' else 'Thin'
so if i have 100 table, itu wiil loop 100 times.
that is my excpected result
that's why i remark the part that i do not know the code.( The underline code).
FOR i IN (SELECT *
FROM abc_param) LOOP
--z := emp_rec.[i.SOURCE_COL]; --This part i want to ask the code ....
dbms_output.Put_line (i.source_col
|| ' = '
|| z);
END LOOP;
Thanks
|
|
|
|
Re: How to get value of table [message #640941 is a reply to message #640939] |
Fri, 07 August 2015 03:05 |
realbassist
Messages: 9 Registered: March 2008
|
Junior Member |
|
|
Hi Michel,
as i replied, i have tried your solution, but it was facing the error :
"PL/SQL: ORA-00923: FROM keyword not found where expected" in line 8.
so i still can not find the solution yet.
Thanks anyway for your time.
|
|
|
Re: How to get value of table [message #640944 is a reply to message #640941] |
Fri, 07 August 2015 03:15 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Well the error message speaks for itself really.
Did you run Michels code as written or did you modify it.
Cause if you modified it you have to actually show us the code you're currently running if you expect us to have any chance to spot the mistake you made.
|
|
|
Re: How to get value of table [message #640945 is a reply to message #640944] |
Fri, 07 August 2015 03:21 |
realbassist
Messages: 9 Registered: March 2008
|
Junior Member |
|
|
Hi Cookiemonster,
I have copy paste from the code above, i run this code
CREATE OR replace PROCEDURE P (p_val IN NUMBER)
IS
cols VARCHAR2(1000);
nb PLS_INTEGER;
res sys.odcivarchar2list;
BEGIN
SELECT 'case '
||Listagg('when line = '
||ROWNUM
||' then '''
||source_col
||' = ''||to_char('
||source_col, ') ')
within GROUP (ORDER BY id)
||') end',
Count(*)
INTO cols, nb
FROM abc_param;
EXECUTE IMMEDIATE 'select '||cols||' from abc, (select level line from dual connect by level <= :nb) where INTERNALID = :id' bulk
collect
INTO res
USING nb, p_val;
FOR i IN res.first..res.last LOOP
dbms_output.Put_line (Res(i));
END LOOP;
END;
/
after i compile that code, it show error.
"ERROR line 8, col 14, ending_line 8, ending_col 20, Found 'Listagg', Only CUME_DIST, DENSE_RANK, PERCENT_RANK, RANK, PERCENTILE_CONT and PERCENTILE_DISC are valid with WITHIN GROUP"
Thanks
[Updated on: Fri, 07 August 2015 03:26] Report message to a moderator
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 19 23:58:33 CDT 2024
|