Question PLSQL with variable compound column names

Hello gentlemen,


I'm new to PLSQL and I would like to display some values of columns, which are listed in the defined variable in dbms.output. I created under code, which I think it could work, but I guess that it is far from correct.


DECLARE
v_TEMP_QUERY VARCHAR2
(1000 BYTE);
v_TEMP_VALUE VARCHAR2
(1000 BYTE) := 'FILTER_01, FILTER_02';

BEGIN  
FOR v_TEMP_LOOP IN
(v_TEMP_QUERY := 'SELECT DISTINCT ' || v_TEMP_VALUE || ' FROM CW_PAGE_COMPONENT_TEMPL WHERE COMPONENT_TEMPL_ID = 10000034642';
EXECUTE IMMEDIATE v_TEMP_QUERY; )
LOOP
DBMS_OUTPUT
.PUT_LINE (v_TEMP_LOOP.v_TEMP_VALUE);
END LOOP;
END;


Thanks in advance for your help and advice on this issue and sincerely,

Sebastian

OK, first of all, you're confused concepts here.

cursor loop FOR waiting for a slider must be provided, and yet you offer an assignment statement and a command immediately execute for it.  Syntax is not valid.

Secondly, you force the way of the use of dynamic SQL, which is generally bad practice.  This use of the SQL is rarely necessary, because it often indicates that functional requirements, database design, or design/code enforcement has not examined properly... the first question is "why don't you know the names of the columns to select.  With dynamic columns in your query, you need some kind of dynamic code to manage the results of the query, you will not be able to reference the columns directly by name.

Thirdly, the DBMS_OUTPUT package doesn't make "blank".  This may sound pedantic, but it's a common mistake that people make.  There is no fill data dbms_output buffer.  It relies on call code/interface to read the contents of the output buffer and display it in fact (for example, in SQL * more you use SQL * Plus the command "set serveroutput we ' to achieve)

In general, in a well designed where application you know your columns you would only make...

Start
I'm in (select distinct filter_01, filter_02 from vw_page_component_templ where component_templ_id = 10000034642)
loop
dbms_output.put_line (i.filter_01 | ',' | i.filter_o2);
end loop;
end;

If the columns are dynamic, then you have three options:

(1) create a ref and pass cursor who back, extracting the content for example

sys_refcursor getRc function returns (in numbers, in colnames templ_id varchar2) is
RC sys_refcursor;
Start
Open rc for ' select distinct ' | colnames |' from vw_page_component_templ where component_teml_id =: 1' using templ_id;
return rc;
end;

In SQL * more:

var r refcursor; -It creates a variable in SQL * Plus ref cursor type
Start
: r: = getRc (10000034642, ' filter_01, filter_02');
end;
/
print r;      -It's SQL * command to retrieve the cursor open and display the results

(2) create an object and collection of the right structure to receive the results and use execute immediate to select IN this structure.

(I will not show in this way, because it is absolute rubbish, and if you know the columns that you want to create the object and the collection, then you should not use dynamic SQL in the first place)

(3) use the DBMS_SQL package to run the query and retrieve the data from the column in position rather than example:

create or replace procedure run_query (p_sql IN VARCHAR2) is
v_v_val varchar2 (4000);
number of v_n_val;
date of v_d_val;
number of v_ret;
c number;
number d;
whole col_cnt;
Boolean f;


rec_tab dbms_sql.desc_tab;
number of col_num;
v_rowcount number: = 0;
Start
-create a slider
c: = dbms_sql.open_cursor;
-analyze the SQL statement in the cursor
DBMS_SQL. Parse (c, p_sql, dbms_sql.native);
-run the cursor
d: = dbms_sql.execute (c);
--
-Describe the columns that are returned by the SQL statement
DBMS_SQL. DESCRIBE_COLUMNS (c, col_cnt, rec_tab);
--
-Local variables Bind to return to the different columns according to their types
 
dbms_output.put_line (' number of columns in the query: ' | col_cnt);
1.col_cnt j
loop
case rec_tab (j) .col_type
When 1 then dbms_sql.define_column (c, j, v_v_val, 2000); -Varchar2
When 2 then dbms_sql.define_column (c, j, v_n_val);      -Number
12. When can dbms_sql.define_column (c, j, v_d_val);     -Date
on the other
DBMS_SQL.define_column (c, j, v_v_val, 2000);  -Any other type of return as varchar2
end case;
end loop;
--
-Display columns are returned...
dbms_output.put_line ('- Columns-');
1.col_cnt j
loop
dbms_output.put_line (rec_tab (j) .col_name |') -' || case rec_tab (j) .col_type when 1 then 'VARCHAR2 '.
When 2 then 'NUMBER '.
When 12 can "DATE".
"Otherwise, 'Other' end);
end loop;
dbms_output.put_line('---');
--
-This part generates the DATA
loop
-Retrieves a row of data using the cursor
v_ret: = dbms_sql.fetch_rows (c);
-Output when no more line
When the output v_ret = 0;
v_rowcount: = v_rowcount + 1;
dbms_output.put_line (' line: ' | v_rowcount);
dbms_output.put_line('---');
-Extract the value of each column of the row
1.col_cnt j
loop
-Fetch each column to the correct data type according to the description of the column
case rec_tab (j) .col_type
When 1 then dbms_sql.column_value (c, j, v_v_val);
dbms_output.put_line (rec_tab (j) .col_name |': ' | v_v_val);
When 2 then dbms_sql.column_value (c, j, v_n_val);
dbms_output.put_line (rec_tab (j) .col_name |': ' | v_n_val);
12. When can dbms_sql.column_value (c, j, v_d_val);
dbms_output.put_line (rec_tab (j) .col_name |': ' | to_char (v_d_val,' DD/MM/YYYY HH24:MI:SS'));))
on the other
DBMS_SQL.column_value (c, j, v_v_val);
dbms_output.put_line (rec_tab (j) .col_name |': ' | v_v_val);
end case;
end loop;
dbms_output.put_line('---');
end loop;
--
-Close the cursor, now we're done with it
DBMS_SQL.close_cursor (c);
END;
/

SQL > run_query exec ('select ename, empno, sal, deptno from emp where deptno = 10');
Number of columns in the query: 4
-Columns-
EMPNO - NUMBER
ENAME - VARCHAR2
DEPTNO - NUMBER
SAL - NUMBER
-------------
Line: 1
--------------
EMPNO: 7782
ENAME: CLARK
DEPTNO: 10
SAL: 2450
--------------
Row: 2
--------------
EMPNO: 7839
ENAME: KING
DEPTNO: 10
SAL: 5000
--------------
Row: 3
--------------
EMPNO: 7934
ENAME: MILLER
DEPTNO: 10
SAL: 1300
--------------

PL/SQL procedure successfully completed.

As you can see with this method, you can extract the column details, including the name and data etc. types and then extract each line and extract the data from the column position.

Of course, this is quite complex, so it would be a last resort.

Tags: Database

Similar Questions

  • How to create Table View with even a column name but another Table?

    Hi all

    I have the problem to create a tableview with the same column name, but in the other table.

    Table I: -.

    Table - PAC051MPROFORMA

    Column - MNR, visitid

    Table - PAC051TPROFORMA
    Column - MNR, visitid

    Table - PAC052MTRANSBILL
    Column - MNR, visitid

    Then, I want to create a table for this table. It comes to my SQL

    Pacviewproforma CREATE VIEW (MNR, visitid, MNR, visitid, MNR, visitid)

    Like some PAC051MPROFORMA.mrn, PAC051MPROFORMA.visitid, PAC051TPROFORMA.mrn, PAC051TPROFORMA.visitid, PAC052MTRANSBILL.mrn, PAC052MTRANSBILL.visitid

    where

    * (a.PAC051MPROFORMA.mrn = PAC051TPROFORMA.mrn) *.
    and
    * (a.PAC051TPROFORMA.mrn = PAC052TRANSBILL.mrn) *.

    SQL return this error ORA-00957 =: duplicate column name

    Can I change this SQL for

    Pacviewproforma CREATE VIEW (MNR, visitid)

    Like some PAC051MPROFORMA.mrn, PAC051MPROFORMA.visitid, PAC051TPROFORMA.mrn, PAC051TPROFORMA.visitid, PAC052MTRANSBILL.mrn, PAC052MTRANSBILL.visitid

    where

    * (a.PAC051MPROFORMA.mrn = PAC051TPROFORMA.mrn) *.
    and
    * (a.PAC051TPROFORMA.mrn = PAC052TRANSBILL.mrn) *.
    This time this error return = ORA-01730: number of column names specified invalid

    What should I do?

    Thank you...

    Hello

    SQL> CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
      2  As Select
      3  PAC051MPROFORMA.mrn,
      4  PAC051MPROFORMA.visitid,
      5  PAC051TPROFORMA.mrn,
      6  PAC051TPROFORMA.visitid,
      7  PAC052MTRANSBILL.mrn,
      8  PAC052MTRANSBILL.visitid
      9  from PAC051MPROFORMA,PAC051TPROFORMA,PAC052MTRANSBILL
     10  where
     11  (PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)
     12  and
     13  (PAC051TPROFORMA.mrn=PAC052MTRANSBILL.mrn);
    CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
                                             *
    ERROR at line 1:
    ORA-00957: duplicate column name
    

    Please give different names to each column.

    Something like that...

    SQL> CREATE OR REPLACE VIEW pacviewproforma (MPROFORMA_mrn,MPROFORMA_visitid,TPROFORMA_mrn,TPROFORMA
    _visitid,MTRANSBILL_mrn,MTRANSBILL_visitid)
      2  As Select
      3  PAC051MPROFORMA.mrn,
      4  PAC051MPROFORMA.visitid,
      5  PAC051TPROFORMA.mrn,
      6  PAC051TPROFORMA.visitid,
      7  PAC052MTRANSBILL.mrn,
      8  PAC052MTRANSBILL.visitid
      9  from PAC051MPROFORMA,PAC051TPROFORMA,PAC052MTRANSBILL
     10  where
     11  (PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)
     12  and
     13  (PAC051TPROFORMA.mrn=PAC052MTRANSBILL.mrn);
    
    View created.
    
    SQL> DESC  pacviewproforma;
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     MPROFORMA_MRN                                      NUMBER
     MPROFORMA_VISITID                                  NUMBER
     TPROFORMA_MRN                                      NUMBER
     TPROFORMA_VISITID                                  NUMBER
     MTRANSBILL_MRN                                     NUMBER
     MTRANSBILL_VISITID                                 NUMBER
    

    ORA-01730: number of column names specified invalid

    The list of the nmae column you specified for the CREATE VIEW must correspond with the list of SELECTION in the view.

    Twinkle

  • Generate CSV with pl.sql column names

    Hi guys,.

    Well, I am back after a long hiatus in this form, was occupied by SAP BI project.

    Now do some oralce - sap project, well here, I needed to generate a CSV of many many queries,

    I can do via sql, but I tot it would be better to write a package or a common procedure to generate csv files

    ex, if I pass the query to the proceudre it should generate csv on the server directory file, later I send these file to the users concerned.

    The e-mail part is later, but first of all is to have a package of generic to launch and run the query n generate the csv file.

    I guess this should be possible using native dynamic sql and utl_ package, but I don't know how to get the columns name,

    If one has implemented this please explain me the way,.

    ex:
    Select emp,mgr, empno from emp;
    
    emp mgr empno
     1    1    123
      2   1    234
    
    something like that
    hope my question is clear 

    Maybe try this

    http://asktom.Oracle.com/pls/asktom/asktom.download_file?p_file=6551135514501758779

  • How to get the column headers into an Excel report with variable width columns?

    Hello:

    I read this question before but I can´t open the link when I try to download the solution I get the following: status HTTP 404 the requested resource is not available.

    Can someone help me with the solution? I want to just put a heading for each column (1 thermocouple, thermocouple 2, anemometer, etc.).

    In advance, thank you for your help!

    You may need to take some tutorials.

    LabVIEW Introduction course - 3 hours
    LabVIEW Introduction course - 6 hours

  • apex5 pre-release - url filter applies to the two IRs with the same column name

    I've implemented 2 interactive reports on the same page, one on EMP (static id Throne) and the other on DEPT (deptir static id). Both the DEPTNO column. If I create the following filter via the URL: IR [throne] EQ_DEPTNO:20, the filter is applied to the two interactive reports.

    Screenshot from 2015-03-29 08:30:33.png

    Hello

    Please let us know. It has the same root cause as your other reported problem. It's bug #20764255, which has already been identified and corrected on our development environment. A fix will be released in our production release.

    Concerning

    Patrick

    Member of the APEX development team

    My Blog: http://www.inside-oracle-apex.com

    APEX Plug-Ins: http://apex.oracle.com/plugins

    Twitter: http://www.twitter.com/patrickwolf

  • Need help query view with the 'Level' column name

    Hello world

    I use Oracle DB 10 g

    I have this request
    SELECT  count(per_id) 
    FROM    XXITS_EMP_MSTR_V
    WHERE   per_id = 213214 
    AND     Level='Technical Staff / Officer'
    XXITS_EMP_MSTR_V is a view created by someone else.

    I get the error "ORA-01788: CONNECT BY clause in this query block" for the the above statement.
    Is it possible that I can always get the result without changing the view?

    Any help is appreciated...

    Thank you
    Elmer

    Elmer says:
    Hello world

    I use Oracle DB 10 g

    I have this request

    SELECT  count(per_id)
    FROM    XXITS_EMP_MSTR_V
    WHERE   per_id = 213214
    AND     Level='Technical Staff / Officer'
    

    Place the level with quotes

    AND "Level" = 'Technical Staff / Officer'
    

    Make sure that the case is used correctly when using double quotes.

  • Having a column name as a variable in a query, problem!

    I have a problem with a simple query below


    SELECT * FROM DisneyWHERE Upper (COLNAME) AS SUPERIOR ('%' |) SEARCHSTRING. '%'); SELECT * FROM Disney
    WHERE Upper (COLNAME) AS SUPERIOR ('%' |) SEARCHSTRING. '%');

    My problem, the variable of column name is not recognized as a column for example name
    A user can select to display a set of characters from the database by user name, movies, etc (they selecting this function since a combobox), they then enter a search string (textbox)
    column name = user name
    SearchString = pluto

    SELECT * FROM Disney
    WHERE Upper (COLNAME) AS SUPERIOR ('%' |) SEARCHSTRING. '%');

    The problem is orac; e does not seem to be picking up this column name is a column name and seems to be a simple comparison. To make this more clear

    It "seems to match the username = pluto
    instead of find pluto in the username column.

    Someone at - it ideas how I can get around this. I have a strange feeling, it's something to do with pl/sql dynamic, but I am new to oracle, so I have no idea of how to write dynamic queries. Any help would be appreciated muchly

    I'm using oracle 11g and visual studio .net 2005

    user10372910 wrote:
    But it wouldn't just look for each column I'm sorry if I seem stupid that I have only just introduced to oracle and I used to use sql

    Sort of Yes and no.

    If colname is 'film' and the search string 'star wars' is the place where actually takes the clause...

    WHERE username as username
    AND the movie like 'star wars % % '.

    If username is not limited in any way and it limits just on film.

    Conversely if the column name is "username" and the search string is 'Bob Smith' then where clause evaluates actually to...

    WHERE username like ' %% of Bob Smith.
    AND the film as film

    then move is not limited in any way and simply limits on the user name.

  • Oracle: Nested joins/sub-requetes: odd column names and an unexplained error

    The following query works fine. It is a series of nested joins to give me a kind of main table:

    SELECT *

    FROM proj_trainer k

    JOIN

    (

      SELECT *

      FROM proj_breeder i

      JOIN

      (

      SELECT *

      FROM proj_jockey g

      JOIN

      (

      SELECT *

      FROM proj_horses e

      JOIN

      (

      SELECT *

      FROM proj_results c

      JOIN

      (

      SELECT *

      FROM proj_race_details a

      JOIN proj_meet b

      ON a.meet_id = b.meet_id

      ) d

      ON c.race_id = d.race_id

      ) f

      ON e.horse_id = f.horse_id

      ) h

      ON g.jockey_id = h.jockey_id

      )j

      ON i.breeder_id = j.breeder_id

    ) l

    ON k.trainer_id = l.trainer_id;

    It works very well with a strange feature, that is not my main problem. Some columns are back with weird codes such as "QCSJ_C000000001300001". Don't know why, or if it comes to my real problem.

    The real problem is that when I add just a subquery join more I get:

    ORA-00904: "N"."RACE_ID": invalid identifier

    Here is the same code with the additional nested block (the one on the very outside)

    SELECT *

    FROM proj_entry m

    JOIN

    (

      SELECT *

      FROM proj_trainer k

      JOIN

      (

      SELECT *

      FROM proj_breeder i

      JOIN

      (

      SELECT *

      FROM proj_jockey g

      JOIN

      (

      SELECT *

      FROM proj_horses e

      JOIN

      (

      SELECT *

      FROM proj_results c

      JOIN

      (

      SELECT *

      FROM proj_race_details a

      JOIN proj_meet b

      ON a.meet_id = b.meet_id

      ) d

      ON c.race_id = d.race_id

      ) f

      ON e.horse_id = f.horse_id

      ) h

      ON g.jockey_id = h.jockey_id

      )j

      ON i.breeder_id = j.breeder_id

      ) l

      ON k.trainer_id = l.trainer_id

    ) n

    ON n.race_id = m.race_id AND n.horse_id = m.horse_id;

    I felt as if I was on the hill with it and then the last line was wrong somehow, despite having virtually the same structure as all previous blocks. I also used the race_id and the horse_id above in the code so that they work.

    I also tried this on the last line:

    ON n.race_id = m.race_id AND n.horse_id = m.horse_id;

    and a few other variations, with supports etc...

    Hello

    I can't reproduce the problem, so I can't say with certainty. It might have to do with the same column names which occur in several tables.

    Why do you need nested views online?  Why not simply join all tables directly, like this?

    SELECT *- or display the columns that you really need

    OF proj_race_details one

    JOIN proj_meet b ON b.meet_id = a.meet_id

    JOIN proj_resuls c ON c.race_id = b.race_id - or a.race_id

    ...

    JOIN proj_trainer k WE k.trainer_id = i.trainer_id - or else

    JOIN proj_entry m ON m.race_id = k.race_id - or else

    AND m.horse_id = k.horse_id - or else

    ;

    I guess just the tables that contain columns; for example, I can't tell what you posted if proj_race_details or proj_meet contains race_id.  If I guessed wrong, use the correct table alias.  Are eligible all the column names with the correct alias.

  • Change the column names in the Table of snapshot?

    Now when you create a snapshot - a table is created using names of columns like C_PRODUCT_MEMBER_CAPTION or C_MEASURES_MEASURE_NAME

    Is it possible to format these column names when creating the table?
    As a measure named "Expéditions" would create a name of column 'Deliveries' and not 'C_MEASURES_SHIPMENTS '.

    I am aware that MDX can Member alias names such as:

    WITH MEMBER [Measures]. [New name] [MEASURES]. [Really long name that I want to change]

    But it would not always remove the prefix "C_MEASURES_".

    Thank you
    John

    John,

    Don't believe it's possible - tables snapshot always end with the crazy column names. I my experience, the raw snapshot tables (almost) always need to be post-processed, which is where you'd of beautiful readable columns.

  • Create table works, create materialized view only - long column names?

    Hello.

    I have no probs creating a table as well: -.
    CREATE TABLE blah
    (
    DEVICE_ID
    )
    in select
    "Device_ID" AS DEVICE_ID
    "of"sum" Device"@ed_link_3
    where "Device_ID" < 5;


    But when I try to create a materialized view:
    Blah1 CREATE MATERIALIZED VIEW
    (
    DEVICE_ID
    )
    < various materialized view parms >
    in select
    "Device_ID" AS DEVICE_ID
    "of"sum" Device"@ed_link_3
    where "Device_ID" < 5;

    It fails with errors: -.
    ORA-04052: error occurred when searching to the top of the remote object Aggregate.Device@ED_LINK_3
    ORA-01948: length of the name of the identifier (31) exceeds maximum (30)

    Is there a way to get around this?
    Is the problem with the columns of the remote table of device, which I do NOT need to import to have column names that are longer than 30 characters?

    For now, I want only the Device_ID column which is a simple 9 characters long.

    Oh, and the remote database is MySQL.
    I'm uncomfortable with the < parms of materialized view > as they work fine when I choose a different remote table with only short column names.

    Thank you.

    To my knowledge, you have the option
    (a) create view (with shortened column names or only with desired columns if they are already less than 30 char limit) side of mysql
    (b) use dbms_passthrough to force the analysis to be done on mysql (as in the example provided by SY here use dbms_passthrough to create a view )
    However, I prefer to stick to one), because with dbms_passtrhough, you retrieve row by row.

    Best regards

    Maxim

  • ERROR DUPLICATE COLUMN NAME

    I get an error of duplicate column name to submit using automatic processing of line.

    I have a few items on the page with the same column name, but they appear under certain conditions. Even if I put in them NEVER occurs again.

    I would understand that you still can't duplicate names even if the elements are conditional?

    If this is the case, then Ill have to do with javascript or something. Pain cos apart from that, my form is quite clean

    Thanks for the help of anyones :-)

    Colin

    Since your sql does not deal with conditional columns, you don't CAN NOT use the same column name several times... Unless you write your own DML to manage updates/inserts & removes...

    Thank you

    Tony Miller
    Webster, TX

  • Column name

    Hi guys,.

    I have a very trivial question? Is * 30_DAYS * a column name valid?

    Thank you

    Published by: user10696492 on July 1st, 2009 15:19

    Ok... I'll try for him...

    SQL> create table shame_on_you (30_days number);
    create table shame_on_you (30_days number)
                               *
    ERROR at line 1:
    ORA-00904: : invalid identifier
    
    SQL> create table shame_on_you ("30_days" number);
    
    Table created.
    
    SQL> desc shame_on_you;
     Name                 Null?    Type
     --------------------- -------- --------------------
     30_days                        NUMBER
    

    User10696492 Yes, what is the answer?

    See you soon

    Legatti

  • As a dynamic values column names

    I have a WEEK_TAB table

    Week_num Bkg1 Bkg2
    1 10 20
    2 40 10
    3 30 20
    2 20 60
    1-10-50
    1-20-80
    3 10 30

    When I run this package, I get the below output...

    CREATE OR REPLACE
    PACKAGE GET_TOTAL AS

    TYPE ref_cursor IS REF CURSOR;

    PROCEDURE get_week_total (p_refCursor to ref_cursor);

    PROCEDURE get_total_val;

    END GET_TOTAL;
    /

    CREATE OR REPLACE
    PACKAGE BODY GET_TOTAL AS

    PROCEDURE get_week_total (p_refCursor to ref_cursor) AS
    BEGIN
    OPEN FOR P_refcursor
    SELECT SUM (bkg1), week_num
    OF week_tab
    GROUP BY week_num
    ORDER BY week_num;
    END get_week_total;

    PROCEDURE get_total_val AS
    v_refCursor ref_cursor;
    v_sum NUMBER;
    v_week NUMBER;
    v_total NUMBER: = 0;

    BEGIN

    get_week_total (v_refcursor);

    LOOP
    EXTRACTION v_refCursor
    IN v_sum, v_week;
    EXIT WHEN v_refcursor % NOTFOUND;
    v_total: = v_total + v_sum;
    DBMS_OUTPUT. Put_line (' week ' | v_week |': ' | v_sum);
    END LOOP;
    DBMS_OUTPUT. Put_line (' total sales reservations :'|| v_total);
    END get_total_val;

    END GET_TOTAL;
    /

    O/P
    Week 01:40
    Week
    Week 03:40
    Total sales reservations: 140

    --------------------------------------------------------------------------------

    Here, I'm moving the column name as dynamic value.
    I modified the package as below...

    CREATE OR REPLACE
    PACKAGE BODY GET_TOTAL AS

    CREATE OR REPLACE
    PACKAGE GET_TOTAL AS

    TYPE ref_cursor IS REF CURSOR;

    Strarray ARRAY TYPE IS VARCHAR2 (100);

    PROCEDURE get_week_total)
    p_colName VARCHAR2,
    p_refCursor to ref_cursor);

    PROCEDURE get_total_val;

    END GET_TOTAL;
    /

    CREATE OR REPLACE
    PACKAGE BODY GET_TOTAL AS

    PROCEDURE get_week_total)
    p_colName IN VARCHAR2,
    p_refCursor OUT ref_cursor) AS

    v_query VARCHAR2 (4000);
    BEGIN
    v_query: = ' SELECT SUM (: p), week_num FROM week_tab GROUP BY week_num ORDER BY week_num';

    P_refcursor OPEN FOR v_query
    Using p_colName;

    END get_week_total;

    PROCEDURE get_total_val AS
    v_refCursor ref_cursor;
    v_sum NUMBER;
    v_week NUMBER;
    v_total NUMBER: = 0;
    STRARRAY v_colName: = STRARRAY ('BKG1', 'BKG2');

    BEGIN

    I'm IN v_colName.FIRST... v_colName.Last LOOP
    get_week_total (v_colName (i), v_refcursor);

    LOOP
    EXTRACTION v_refCursor
    IN v_sum, v_week;
    EXIT WHEN v_refcursor % NOTFOUND;
    v_total: = v_total + v_sum;
    DBMS_OUTPUT. Put_line (' week ' | v_week |': ' | v_sum);
    END LOOP;
    DBMS_OUTPUT. Put_line (' total sales reservations :'|| v_total);

    END LOOP;
    END get_total_val;

    END GET_TOTAL; GET_TOTAL;
    /


    But I get this error message
    ORA-01722: invalid number
    ORA-06512: at "SCOTT. GET_TOTAL', line 29
    ORA-06512: at line 2


    Please let me know how this can be resolved... I'm trying to get the result as below

    Week 01:40
    Week
    Week 03:40
    Total sales reservations: 140

    Week 1:150
    Week 2:70
    Week 03:50
    Total sales reservations: 270

    Variable bind bind values of the object, not the object names. You cannot use bind variable as column name. Change:

    v_query := 'SELECT SUM (:p), week_num FROM week_tab GROUP BY week_num ORDER BY week_num'; 
    

    TO

    v_query := 'SELECT SUM (' || p_colName || '), week_num FROM week_tab GROUP BY week_num ORDER BY week_num'; 
    

    and

    OPEN p_refcursor FOR v_query
    USING p_colName; 
    

    TO

    OPEN p_refcursor FOR v_query; 
    

    SY.

  • How to use a variable as a column name in a select statement

    Hello, I am using

    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    PL/SQL Release 11.2.0.3.0 - Production

    I have a procedure looking for groups that have not been updated for the current month. The columns are month_01, month_02, month_03 etc. I'm becoming if I can automate every month, but I need to be able to use a variable as the name of the column for the month. Can someone help me with this?

    Example of Table

    CREATE TABLE TEST_TABLE
    (
      IDENTITY_CODE      NUMBER(10),
      YEAR_S             NUMBER(5),
      MONTH_01           NUMBER(18,2),
      MONTH_02           NUMBER(18,2),
      MONTH_03           NUMBER(18,2),
      MONTH_04           NUMBER(18,2),
      MONTH_05           NUMBER(18,2),
      MONTH_06           NUMBER(18,2),
      MONTH_07           NUMBER(18,2),
      MONTH_08           NUMBER(18,2),
      MONTH_09           NUMBER(18,2),
      MONTH_10           NUMBER(18,2),
      MONTH_11           NUMBER(18,2),
      MONTH_12           NUMBER(18,2) 
    );
    INSERT ALL
    INTO TEST_TABLE VALUES(640,2015,124,123,125,126,127,128,547,888,987,567,145,685)
    INTO TEST_TABLE VALUES(098,2015,587,874,0,587,652,222,444,777,885,999,657,547)
    INTO TEST_TABLE VALUES(608,2015,587,874,687,587,652,222,444,787,885,999,657,547)
    select 1 from dual;
     

    Here's my current procedure

    CREATE OR REPLACE PROCEDURE RRM_EMAIL_NOTIFICATION IS
    
    v_output VARCHAR2(10000);
    v_message VARCHAR2(1000);
    v_date date;
    cursor v_check is 
    SELECT *
      FROM (SELECT '640' AS ds FROM DUAL
            UNION ALL
            SELECT '098' AS ds FROM DUAL
            UNION ALL
            SELECT '608' AS ds FROM DUAL
            UNION ALL
            SELECT '618' AS ds FROM DUAL
            UNION ALL
            SELECT '617' AS ds FROM DUAL
            UNION ALL
            SELECT '614' AS ds FROM DUAL
            UNION ALL
            SELECT '610' AS ds FROM DUAL
            UNION ALL
            SELECT '616' AS ds FROM DUAL
            UNION ALL
            SELECT '643' AS ds FROM DUAL)
    WHERE LPAD (ds, 3, '0') NOT IN
              (SELECT DISTINCT (SUBSTR (IDENTITY_CODE, 2, 3)) AS Blah_Blah_Blah
                 FROM TEST_TABLE
                WHERE     month_03 <> 0
                      AND year_s = 2015
                      AND (   SUBSTR (IDENTITY_CODE, 2, 3) = '640'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '098'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '608'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '618'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '617'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '614'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '610'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '616'
                           OR SUBSTR (IDENTITY_CODE, 2, 3) = '643'));
    
    BEGIN
    v_date := SYSDATE;
    open v_check;
    loop 
    fetch v_check into v_output;
    exit when v_check%NOTFOUND;
    v_message := v_message || chr(13) || v_output;
    END LOOP;
    close v_check;
       
    send_mail('The RRM one-time initiatives that have not been processed for '||v_date|| ' are ' || chr(13) ||v_message,'[email protected]','RRM ONETIMES NOT PROCESSED FOR ' || v_date);
       
    END RRM_EMAIL_NOTIFICATION;
     

    I need to be able to substitute the name e.g. MONTH_01 with a variable column so that it will be MONTH_ | "" some months "

    A variable can be created as v_month: = 'month_ | TO_CHAR (T_DATE, 'MM')

    If I'm not mistaken that should come out as month_03 for March

    The output should be in an email as follows:

    Capture.JPG

    Something like:

    CREATE OR REPLACE PROCEDURE RRM_EMAIL_NOTIFICATION IS

    v_output VARCHAR2 (10000);

    v_message VARCHAR2 (1000);

    date of T_DATE;

    v_check SYS_REFCURSOR;

    BEGIN

    T_DATE: = SYSDATE;

    Open the v_check FOR

    Q' {}

    SELECT *.

    FROM (SELECT '640' DS DUAL FROM

    UNION ALL

    SELECT '098' LIKE ds FROM DUAL

    UNION ALL

    SELECT '608' LIKE ds FROM DUAL

    UNION ALL

    SELECT '618' LIKE ds FROM DUAL

    UNION ALL

    SELECT '617' LIKE ds FROM DUAL

    UNION ALL

    SELECT '614' LIKE ds FROM DUAL

    UNION ALL

    SELECT '610' LIKE ds FROM DUAL

    UNION ALL

    SELECT '616' LIKE ds FROM DUAL

    UNION ALL

    SELECT '643' DS DUAL FROM)

    WHERE LPAD (ds, 3, '0') NOT IN

    (SELECT DISTINCT (SUBSTR (IDENTITY_CODE, 2, 3)) AS Blah_Blah_Blah

    FROM TEST_TABLE

    {WHERE month_}' | TO_CHAR (T_DATE, 'MM') | Q'{ <> 0

    AND year_s = 2015

    AND SUBSTR (IDENTITY_CODE, 2, 3))

    '640'

    '098'

    , ' 608 "

    '618'

    '617'

    '614'

    '610'

    '616'

    '643')

    )

    }';

    loop

    extract the v_check in v_output;

    When the output v_check % NOTFOUND;

    v_message: = v_message | Chr (13) | v_output;

    END LOOP;

    close v_check;

    send_mail ("one-time initiatives the RRM that haven't been treated for ' |") T_DATE | «are» | Chr (13) | v_message,' [email protected]',' ONETIMES UNPROCESSED for RRM ' | T_DATE);

    END RRM_EMAIL_NOTIFICATION;

  • questions to move a column name as parameter. Please advice.

    Hi all

    I have some difficulties with oracle reports. I am a novice with the beginner 2 days.
    I am not able to use the lexical settings in reports. my query is as easy as

    Select & column-name abc xyz;

    I tried to make a setting before creating the SQL but I always get the error saying that ORA-00922: lack of expression.
    also I tried with bind variables like

    Select: col_name abc xyz;

    but it gives a: inplace Column of records the data.

    some could help me with step by step procedure to create a lexical parameter.

    I am using reports 6i.

    Please let me know for details.

    Thanks in advance!

    Hello

    I suggest you create a sing an example 'Basic' of the EMP table: (see the demobld.sql script provided with Developer Suite to create this table)

    Create a new report
    Create a COLUMN name parameter:
    Name = COLNAME
    DataType = character
    Width = 20
    Initial value = ENAME

    Create a SQL query: select & emp colname

    Create a page layout manually or with the wizard

    Click on the button «Run paper Layout»

    Enter a valid value for the 'column' name (ENAME / JOB / SAL / COMM /...)

    Concerning

Maybe you are looking for

  • What apps work best with the pencil

    What apps work best with the pencil. Particularly interested in commenting, highlighting and rating photos and graphics.

  • Re: Satellite C660-2R3 CPU limitation to 978 Mhz?

    Hi all 5 days ago, I bought a Toshiba Satellite C660 2R3. The laptop has core i5 2.4 GHz and 6 GB of DDR3 memory.However, I am very disappointed by its performance. I run CPU ID/CPU-Z and it turns out that the laptop is limitation up to 978 MHz Core

  • Update video P6 card - 2220t

    Hi I need to update my video card to one that has a HDMI out on it, not for the games just to watch movies any suggestions would be great just need one that is compatible

  • Dell inspiron 17r helmet does not connect

    Pretty much what title says. I plug in my headset on the laptop, and they do not work. I reinstalled the drivers from the dell site. Problem remains. Anyone know what else to do? It will help if I uninstall the drivers from dell (thing audio dell) an

  • I do not have a folder AppData\Roaming\Adobe\Lightroom.

    I have a problem. A software, I uninstalled some time ago, Adware, quarantined my AppData folder any Lightroom was not installing in the AppData\Roaming\Adobe\Lightroom. Everything is in Roaming is Microsoft.The problem is that I want to install the