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 Go to next message
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 #640867 is a reply to message #640863] Thu, 06 August 2015 03:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

If you post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data.

Re: How to get value of table [message #640872 is a reply to message #640867] Thu, 06 August 2015 04:11 Go to previous messageGo to next message
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 #640874 is a reply to message #640872] Thu, 06 August 2015 04:22 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
See how to use the code tags in the link Michel posted.
Re: How to get value of table [message #640879 is a reply to message #640872] Thu, 06 August 2015 04:42 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #640886 is a reply to message #640883] Thu, 06 August 2015 06:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
So i want to get the value or column from table ABC_PARAM.


And this is what my script does.

Quote:
I am sorry, my expected result is not 1 row value.


This is just a display issue.
You can
1/ Fold one row into several lines
2/ Unpivot to get several lines

For case 2, after some modifications in the script:
C:\>type t.sql
col cols new_value cols noprint
col nb new_value nb noprint
set feedback off head off
select 'case
'||listagg('when line = '||rownum||' then '''||source_col||' = ''||to_char('||source_col,')
') within group  (order by id)||')
 end' cols,
        count(*) nb
from abc_param;
set feedback on
select &cols
from abc,
      (select level line from dual connect by level <= &nb)
where INTERNALID = &id;

SQL> @c:\t

Enter value for id: 11111
VCOL1 = aaaa
NCOL2 = 101
DCOL3 = 20-MAY-2015

3 rows selected.

SQL> @c:\t

Enter value for id: 11112
VCOL1 = bbbb
NCOL2 = 102
DCOL3 = 02-MAY-2015

3 rows selected.

Re: How to get value of table [message #640889 is a reply to message #640886] Thu, 06 August 2015 08:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You can do exactly the same thing in PL/SQL if you don't want to SQL*Plus features:
SQL> create or replace procedure p (p_val in number) is
  2    cols varchar2(1000);
  3    nb   pls_integer;
  4    res  sys.odcivarchar2list;
  5  begin
  6    select 'case
  7  '||listagg('when line = '||rownum||' then '''||source_col||' = ''||to_char('||source_col,')
  8  ') within group  (order by id)||')
  9   end', count(*)
 10    into cols, nb
 11    from abc_param;
 12    execute immediate
 13      'select '||cols||'
 14       from abc,
 15            (select level line from dual connect by level <= :nb)
 16       where INTERNALID = :id'
 17    bulk collect into res
 18    using nb, p_val;
 19    for i in res.first..res.last loop
 20      dbms_output.put_line (res(i));
 21    end loop;
 22  end;
 23  /

Procedure created.

SQL> exec p(11111)
VCOL1 = aaaa
NCOL2 = 101
DCOL3 = 20-MAY-2015

PL/SQL procedure successfully completed.

SQL> exec p(11112)
VCOL1 = bbbb
NCOL2 = 102
DCOL3 = 02-MAY-2015

PL/SQL procedure successfully completed.

Re: How to get value of table [message #640938 is a reply to message #640889] Fri, 07 August 2015 02:43 Go to previous messageGo to next message
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 #640939 is a reply to message #640938] Fri, 07 August 2015 02:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Michel Cadot wrote on Thu, 06 August 2015 10:46

Welcome to the forum.
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

If you post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data.


I gave you 2 solutions for your problem, you can do as you want but I will surely not spend more time on this when you refuse to follow our guidelines and do not even try to understand what I provided.

Good luck.

Re: How to get value of table [message #640941 is a reply to message #640939] Fri, 07 August 2015 03:05 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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

Re: How to get value of table [message #640947 is a reply to message #640945] Fri, 07 August 2015 03:28 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
What exact version of oracle are you running?
Re: How to get value of table [message #640948 is a reply to message #640947] Fri, 07 August 2015 03:32 Go to previous messageGo to next message
realbassist
Messages: 9
Registered: March 2008
Junior Member
my oracle version is Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Re: How to get value of table [message #640950 is a reply to message #640948] Fri, 07 August 2015 03:51 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So why have you put 11g at the top of the thread?
listagg doesn't exist in 10g.
Re: How to get value of table [message #640952 is a reply to message #640950] Fri, 07 August 2015 03:54 Go to previous messageGo to next message
realbassist
Messages: 9
Registered: March 2008
Junior Member
Hi Cookiemonster,

I am sorry i put it wrongly, should be 10g version,
Is there any way for database 10 g ?

Thanks
Re: How to get value of table [message #640953 is a reply to message #640952] Fri, 07 August 2015 04:02 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
You could use ROW_NUMBER() and SYS_CONNECT_BY_PATH. See few examples here https://oracle-base.com/articles/misc/string-aggregation-techniques. Just avoid WM_CONCAT.
Previous Topic: Filtering data by aggregated columns calculated in a subquery.
Next Topic: calulating target and actual hrs
Goto Forum:
  


Current Time: Fri Apr 19 23:58:33 CDT 2024