Column name as a variable !!!!!!!!!!!!!!!!!!!!

hi All,
please I would like to write a query such that the column names is to be variables.
"select &C1, &C2 from T1 ;" it's working in sql developer but it's not working on apex region.
thanks a lot.

Actually, the ADF Faces demo site from Oracle looks (potentially) really cool. I have no idea how it works in the background though so it could be a nightmare. I'd appreciate it if you would post back here once you've given it a try. I'd be genuinely interested to know what it's like to work with.
Its a bit funny because APEX seems to be coming on leaps and bounds but Oracle don't advertise it much (probably because it's free). That could well be a view that is tainted by being an active member of the community though so it would be intriguing to see what people 'from the outside, looking in' feel about it. You got many people there that have opinions about APEX? The java boys at the place I'm contracting at seem to look down on us with disdain!
Cheers
Ben

Similar Messages

  • Having a column name as a variable within a query,Problem!!!!

    I have a problem with a simple query shown below
    SELECT * FROM DisneyWHERE Upper(COLNAME) LIKE UPPER('%' || SEARCHSTRING || '%');SELECT * FROM Disney
    WHERE Upper(COLNAME) LIKE UPPER('%' || SEARCHSTRING || '%');
    My problem, The colname variable is not being recognised as a column name for example
    A user can select to view a set of characters from the DB by username, movies, etc etc (they select this from a combobox) They then enter a search string(textbox)
    colname = username
    Searchstring = pluto
    SELECT * FROM Disney
    WHERE Upper(COLNAME) LIKE UPPER('%' || SEARCHSTRING || '%');
    The problem is orac;e does not seem to be picking up that colname is a column name and seems to be doing a simple comparison. To make this clearer
    it' seems to be trying to match username = pluto
    rather than finding pluto in the username column.
    Has anyone got any ideas how i can get around this. I have a strange feeling it is something to do with dynamic pl/sql but i am new to oracle so i have no idea how to write dynamic queries. Any help would be muchly appreciated
    I am using oracle 11g and visual studio .net 2005

    user10372910 wrote:
    If you can refer me to any material you think i would find helpful to read or experiment with pls let me knowThe online documentation is a good place to look...
    http://www.oracle.com/pls/db102/homepage?remark=tahiti
    Start with the concepts guide which gives a good background to the Oracle database.
    Look up "bind variables", "hard parsing"/"soft parsing" of queries
    In light of what boneist highlighted, you may also want to do a search for "function based indexes".
    Those topics should give you some insight into the performance issues around queries and why it is better to write SQL that uses bind variables and get's soft parsed (re-usable SQL) rather than writing dynamic queries that become hard parsed (non-reusable SQL).

  • PL/SQL column names in cursor variable

    Hi,
    I try to put all column names of a table in a cursor variable?? without the values, just the column names.
    Can somebody help me, because I really don't know how to do this.
    Thanks,
    Moni

    Hi Jes,
    my table looks like this: T1
    columnNames: id | col1 | col2 | col3 | col4 | col5
    Values: a | b | c| d| e| f
    the number of the columns depend on a how many rows in another table (T2) are entered. If a new row is entered in T2, I have to add a column in table T1.
    and then I have to look what is the new row, compare it with all my existing columnnames in T1 and add the right one.
    my vector should look like this:
    c1:
    id
    col1
    col2
    col3
    col4
    col5
    I hope you know what I mean, it is not very easy to describe!
    Thanks for helping me!
    Moni

  • Column name as a variable in a database trigger

    I am trying to code a pre-insert database trigger to format all varchar2 columns entered into the triggering table. I am using a cursor to get all the relevant column names from all_tab_columns, but do not know how to refer to these values in combination with the ":new" construct within my code. The example below is a very simplified version of what I am trying to do ( it obviously will not work as it is):
    declare
    cursor column_cur is
    select column_name cn
    from all_tab_columns
    where table_name = 'TEMP_ASSESSMENT'
    and data_type = 'VARCHAR2';
    v_columnname varchar2(30);
    begin
    for column_rec in column_cur loop
    v_columnname := column_rec.cn;
    :new.v_columnname := upper(:new.v_columnname); declare
    cursor column_cur is
    select column_name cn
    from all_tab_columns
    where table_name = 'TABLE_X'
    and data_type = 'VARCHAR2';
    v_columnname varchar2(30);
    begin
    for column_rec in column_cur loop
    v_columnname := column_rec.cn;
    :new.v_columnname := upper(:new.v_columnname);
    end loop;
    end;

    Seems to me like the problem in this thread:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:59412348055
    C.

  • Variable and column name

    Hi all,
    In an interface, I've got a column which has the same name as a variable.
    When I execute it in ODI 10g all is fine.
    With ODI 11g, it replace the column name by the variable one. If the variable has no value, I've got an SQLException.
    I believe it comes from the colon.
    In the execution code, I have
    INSERT ... VALUES PROJECT.NAME instead of NAME.
    I assume that ODI interprets the NAME with the variable name instead of the column name.
    Has anyone experienced it ?
    Thanks.

    Hi,
    I just tried to reproduce your issue, I didn't experience the case where ODI takes variable name instead of column name. ODI should be able to differentiate between column name and variable name with '#' Symbol prefixed to it. Any name prefixed with #, ODI thinks thats a variable and, it need to be declared in a package.
    In your case, looks like issue is not with the NAME, its with variable having no value. May be that ODI 10g has a default value. However, if a variable has no value, its definitely going to fail the process.
    Regards,
    K

  • How to concatenate column name in PL/SQL

    Here is ex.
    I want to execute like this
    for i in 1 .. 18 loop
    EXECUTE IMMEDIATE 'select qty_sz'||i||' from table1';
    end loop;
    I mean Can I cancatenate column name with a variable?
    your advice is gretly Appeciated.
    Ram.

    It would be a lot easier to diagnose your problem if you posted the error message. Oracle has an awful lot of error messages.
    I thing I have noticed is this:
    EXECUTE IMMEDIATE mysql into var1 using 'qty_sz'||i ;
    You have an implicit datatype conversion there, i being a number and you can only concatenate varchar variables. There is a little wrinkle in this, in that to_char seems to put a space in front of any number. So try this:
    EXECUTE IMMEDIATE mysql into var1 using 'qty_sz'||ltrim(to_char(i));
    and see if it makes any difference.
    Another problem will be if your columns are like this: qty_sz01, qty_sz02,...qty_sz8. because the default mask doesn't incude the leading zero so in that case you should specify the mask in the to_char(i, '09')
    If neither of those fixes your problem post the your error message.
    rgds, APC

  • Can I use bind variable instaed of writing static COLUMN Name

    Hi , I am having a table containing id and column names, the data is stored against that id in other tables. Now I wish to update data into another table so that it goes into apppropriate column without using decode function.
    I am trying to do this:
    EXECUTE IMMEDIATE 'update TEST set :1 = :2
    where PROJECT_ID= :3 and UNIQUE_ID= :4' using P_DEST_COLUMN, P_TEXT_VALUE, P_PROJ_ID, P_TASK_UID;
    the values P_DEST_COLUMN, P_TEXT_VALUE, P_PROJ_ID, P_TASK_UID are populated using a cursor in PL/SQl
    Is this statement valid? If not can you tell me how to do it as I am getting some error I am unable to comprehend.
    thanks
    Rishabh

    Column names cannot be substituted at run-time as bind variables. If you need to specify the column name at run-time, you'd need to construct a new string and execute that string dynamically, i.e.
    EXECUTE IMMEDIATE 'UPDATE test SET ' || p_dest_column || ' = :1 ' || ...From a data model standpoint, storing column names as data elements in another table is generally a rather poor idea. It's likely to make ad-hoc reporting nearly impossible and to cause a lot more parsing than would otherwise be required.
    Justin

  • How to use variable in :old. variable instead of :old.column name.

    My requirement is to update the audit_update table with the old value and new value when an update statement gets executed. If i use :old.columnname the purpose is getting resolved.
    My requirement is to loop through the columns taken dynamically and update the audit_update_details table. The updation is taking place in a function that called.
    The first cursor holds the column name of a given table.
    I have to use a statement like :old.variablename . where the variable holds the column name .
    Is their an alternative?
    CREATE OR REPLACE TRIGGER INSERT_EMP1
    AFTER INSERT OR UPDATE OR DELETE
    ON EMP FOR EACH ROW
    declare
    v_seq number(5);
    v_result boolean;
    v_col_name varchar2(20);
    CURSOR FIRSTCURSOR IS
    select COLUMN_NAME from user_tab_columns where table_name like 'EMP';
    begin
    if inserting then
    INSERT INTO aud_details VALUES(seq_aud_log_id.nextval,'Emp','I','eno',:NEW.ENO);
    end if;
    if DELETING then
    INSERT INTO aud_details VALUES(seq_aud_log_id.nextval,'Emp','D','eno',:OLD.ENO);
    end if;
    IF UPDATING THEN
    INSERT INTO aud_details VALUES(seq_aud_log_id.nextval,'Emp','U','eno',:OLD.ENO);
    select seq_aud_log_id.currval into v_seq from dual;
    open FIRSTCURSOR;
    LOOP
    FETCH FIRSTCURSOR INTO v_col_name;
    EXIT WHEN FIRSTCURSOR%NOTFOUND;
    BEGIN
    -- How to make the code to work successfully.
    if (:old.v_col_name != :new.v_col_name) then
    v_result := insert_fn('emp',:old.eno, v_col_name , :old.v_col_name, :new.v_col_name, v_seq );
    end if;
    end;
    end loop;
    THe below is an example that uses the column name .Instead of column name i need to use the variable. So that the trigger functions for any given table.
    /* IF (:old.ename != :new.ename) THEN
    v_result := insert_fn('emp',:old.eno, 'ename' , :old.ename, :new.ename, v_seq );
    END IF;
    IF (:old.dno != :new.dno) THEN
    v_result := insert_fn('emp',:old.eno, 'dno' , :old.dno, :new.dno,v_seq );
    END IF;
    IF (:old.salary != :new.salary) THEN
    v_result := insert_fn('emp',:old.eno, 'salary' , :old.salary, :new.salary, v_seq );
    END IF;
    END IF;
    END INSERT_EMP1;
    thanks,
    vinodh

    As Solomon said, it's not possible, but you could write a procedure that would generate the code to do it for each individual column. You could then execute the code generator procedure any time the tables DDL changed in order to update the triggers code.

  • Saving result from sp_executesql into a variable and using dynamic column name - getting error "Error converting data type varchar to numeric"

    Im getting an error when running a procedure that includes this code.
    I need to select from a dynamic column name and save the result in a variable, but seem to be having trouble with the values being fed to sp_executesql
    DECLARE @retval AS DECIMAL(12,2)
    DECLARE @MonthVal VARCHAR(20), @SpreadKeyVal INT
    DECLARE @sqlcmd AS NVARCHAR(150)
    DECLARE @paramdef NVARCHAR(150)
    SET @MonthVal = 'Month' + CAST(@MonthNumber AS VARCHAR(2) );
    SET @SpreadKeyVal = @SpreadKey; --CAST(@SpreadKey AS VARCHAR(10) );
    SET @sqlcmd = N' SELECT @retvalout = @MonthVal FROM dbo.CourseSpread WHERE CourseSpreadId = @SpreadKeyVal';
    SET @paramdef = N'@MonthVal VARCHAR(20), @SpreadKeyVal INT, @retvalout DECIMAL(12,2) OUTPUT'
    --default
    SET @retval = 0.0;
    EXECUTE sys.sp_executesql @sqlcmd,@paramdef, @MonthVal = 'Month4',@SpreadKeyVal = 1, @retvalout = @retval OUTPUT;
    SELECT @retval
    DECLARE @return_value DECIMAL(12,2)
    EXEC @return_value = [dbo].[GetSpreadValueByMonthNumber]
    @SpreadKey = 1,
    @MonthNumber = 4
    SELECT 'Return Value' = @return_value
    Msg 8114, Level 16, State 5, Line 1
    Error converting data type varchar to numeric.

    Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need
    to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. And you need to read and download the PDF for: 
    https://www.simple-talk.com/books/sql-books/119-sql-code-smells/
    >> I need to select from a dynamic column name and save the result in a variable, but seem to be having trouble with the values being fed to sp_executesql <<
    This is so very, very wrong! A column is an attribute of an entity. The idea that you are so screwed up that you have no idea if you want
    the shoe size, the phone number or something else at run time of this entity. 
    In Software Engineering we have a principle called cohesion that says a model should do one and only one task, have one and only one entry point, and one and only one exit point. 
    Hey, on a scale from 1 to 10, what color is your favorite letter of the alphabet? Yes, your mindset is that level of sillyity and absurdity. 
    Do you know that SQL is a declarative language? This family of languages does not use local variables! 
    Now think about “month_val” and what it means. A month is a temporal unit of measurement, so this is as silly as saying “liter_val” in your code. Why did you use “sp_” on a procedure? It has special meaning in T-SQL.  
    Think about how silly this is: 
     SET @month_val = 'Month' + CAST(@month_nbr AS VARCHAR(2));
    We do not do display formatting in a query. This is a violation of at the tiered architecture principle. We have a presentation layer. But more than that, the INTERVAL temporal data type is a {year-month} and never just a month. This is fundamental. 
    We need to see the DDL so we can re-write this mess. Want to fix it or not?
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • How to use column name as variable in select statement

    hi,
    i want to make a sql query where in select statement using variable as a column name. but its not working plz guide me how can i do this.
    select :m1 from table1;
    regards

    Hi,
    Is this what you want..
    SQL> select &m1 from dept;
    Enter value for m1: deptno
    old   1: select &m1 from dept
    new   1: select deptno from dept
        DEPTNO
            10
            20
            30
            40
    SQL> select &m1 from dept;
    Enter value for m1: dname
    old   1: select &m1 from dept
    new   1: select dname from dept
    DNAME
    ACCOUNTING
    RESEARCH
    SALES
    OPERATIONS
    SQL> select &&m1 from dept;
    Enter value for m1: loc
    old   1: select &&m1 from dept
    new   1: select loc from dept
    LOC
    NEW YORK
    DALLAS
    CHICAGO
    BOSTON
    SQL> select &&m1 from dept;
    old   1: select &&m1 from dept
    new   1: select loc from dept
    LOC
    NEW YORK
    DALLAS
    CHICAGO
    BOSTONIf you use single '&' then each time you fire the query, It will ask for the new value..
    But if you will use double '&&' the value of m1 will be persistent across the session..
    Twinkle

  • How to put the column name and variable value in the alert message.

    Dear,
    how can i put the column name and variable value in the alert message text. i want to display an alert which tell the user about the empty textboxes. that these textboxes must be filled.
    Regards:
    Muhammad Nadeem
    CHIMERA PVT. LTD.
    LAHORE
    [email protected]

    Hello,
    The name of the item that fires the current trigger is stored in the :SYSTEM.TRIGGER_ITEM system variable.
    The value contained in this item can be retrived with the Name_In() built-in
    value := Name_In( 'SYSTEM.TRIGGER_ITEM') ;
    LC$Msg := 'The item ' || :SYSTEM.TRIGGER_ITEM || ' must be entered' ;
    Set_Alert_Property('my_alert_box', ALERT_MESSAGE_TEXT, LC$Msg ) ;
    Ok := Show_Alert( 'my_alert_box' ) ;
    ...Francois

  • Use variable in SQL for column name

    Hi All,
    We want to use a user input as a column name in APEX.
    For e.g user will enter "ALLOWABLE_AMOUNT" then the query will be as follows  :
    select Rule,rule_name,rule_desc,"User Input" from rule_dim
    where "User_input" > 100
    So here the User_input will be substitued with Allowable_amount. Is this doable using any bind/substitution variables ? I tried ":P2_USER_VARIABLE" and "&P2_USER_VARIABLE." but did not work.
    Please advice.

    Hi Andy,
    You do that with an Interactive Report and a Dynamic Action.
    I'll assume that you're using APEX 4.2
    Here's how:
    Create Page 2 with an Interactive Report
    Create New Page > Report > Interactive Report > Next > Next
    Enter a SQL Select statement: select Rule,rule_name,rule_desc from rule_dim
    Next > Create > Edit Page
    Create the item P2_USER_VARIABLE
    Add Item > Number Field > Next
    Item Name: P2_USER_VARIABLE
    Next > Next > Next
    Source Used: Always, replacing any existing value in session state
    Source Type: Static Assignment (value equals source attribute)
    Create Item
    Create a Dynamic Action to refresh the Interactive Report when P2_USER_VARIABLE is changed
    Add Dynamic Action
    Name: Refresh IRR
    Next >
    Event: Lose Focus
    Selection Type: Item(s)
    Item(s): P2_USER_VARIABLE
    Next >
    Action: Refresh
    Next >
    Selection Type: Region
    Region: Report 1 (10)
    Create Dynamic Action
    Add the ALLOWABLE_AMOUNT to the Interactive Report
    Report 1
    Region Source: SELECT * FROM (select Rule,rule_name,rule_desc, :P2_USER_VARIABLE AS ALLOWABLE_AMOUNT from rule_dim) WHERE ALLOWABLE_AMOUNT > 100
    Apply Changes > Apply Changes
    Get the Interactive Report to submit P2_USER_VARIABLE
    Report 1
    Page Items to Submit: P2_USER_VARIABLE
    Apply Changes
    Change the Heading for ALLOWABLE_AMOUNT in the Interactive Report
    Interactive Report
    Change the Heading of ALLOWABLE_AMOUNT to &P2_USER_VARIABLE.
    Apply Changes
    Run
    Enter something into the USER VARIABLE field and select something else on the page. Watch the last column update to that value.
    Tim.

  • Variable column name in a trigger

    I have a table with many columns, and would like to make a log table of all the updates in the first one. So my idea was to bulid a "before update" trigger which would first create a cursor to get the column names from the user_tab_columns table. In a cursor loop it would check if the :new.column_data is somehow different from :old.column_data. If this was the case, trigger would build a sql statement for inserting old and new values into the log table, and then execute this statement using the "execute immediate".
    Here i run into a problem, because i shuld pass the old and new values of a column instead of :new and :old variables, which are not bound in the execute immediate. Any suggestion besides writing a long trigger for checking each and every column name in a table?

    Or don't create it like this at all.
    This way you'll get:
    - complex trigger code
    - a hard parse each and every insert into that log table
    - a trashed shared pool, because it gets populated with all "almost the same" insert statements
    Querying a log table probably is not something you do everyday, so it likely doesn't pay to store it in a complex way to make sure the retrieval goes smoothly.
    If you want to log, you could do it in a single insert statement with all the information you could possibly need. This makes the code easy and the SQL sharable. It could look like below, and just skip the columns you don't need:
    insert into log_table
    ( action
    , action_date
    , action_user
    , column1_old_value
    , column1_new_value
    , did_column1_change
    , column2_old_value
    , column2_new_value
    , did_column2_change
    , columnN_old_value
    , columnN_new_value
    , did_columnN_change
    values
    ( case when inserting then 'I' when updating then 'U' when deleting then 'D' end
    , sysdate
    , user
    , :old.column1
    , :new.column1
    , case when updating('column1') then 'Y' else 'N' end
    , :old.column2
    , :new.column2
    , case when updating('column1') then 'Y' else 'N' end
    , :old.columnN
    , :new.columnN
    , case when updating('columnN') then 'Y' else 'N' end
    );Or you can use a flashback archive instead of a log table in 11g, but I don't have enough experience with it yet to fully recommend that one.
    Regards,
    Rob.

  • If variable name is the same as the column name in procedure ?

    If variable name is the same as the column name in procedure , What should i do?
    For Example :
    CREATE OR REPLACE PROCEDURE "TEST_PROC" (MIN_SALARY in UMBER)
    as
    begin
    INSERT INTO TEST SELECT JOB_ID,MIN_SALARY FROM JOBS WHERE MIN_SALARY = MIN_SALARY;
    end;

    You could follow a better naming convention and have the parameters to the procedures named in a way so as not to be confused with column names.
    You could prefix the variable names with the name of the procedure they appear in but then what if your have procedure names same as table names?
    SQL> create or replace procedure test_proc(sal in number) is
      2    cnt number ;
      3  begin
      4    select count(*) into cnt from scott.emp where scott.emp.sal = test_proc.sal ;
      5    dbms_output.put_line('cnt='||cnt) ;
      6  end ;
      7  /
    Procedure created.
    SQL> set serveroutput on
    SQL> exec test_proc(800) ;
    cnt=1
    PL/SQL procedure successfully completed.
    SQL>If you search you can find several posts here itself on naming convention to avoid such issues in first place.

  • Passing column names in as a variable

    I know in SQL server there is a way you can pass a variable in to the procedure and put that variable as a column name, Oracle doesn't seem to pick the variable up as a column name. Can anyone show me how with a simple select statement

    bgulcu wrote:
    I'm having a similar problem. I didn't want to open up another thread since it's related.
    Problem:
    I have 107 columns on a table. The number of columns increases almost every month.
    I have an array of columns, which I have gathered from the db. I need to pivot the whole table, but due to indefinite number of columns, it had to be dynamic sql.
    Premature Solution:
    Right now, the solution that we have runs through the array of columns and uses dynamic sql to query for every single cell to get the value. Considering that we have 1000+ rows on this table, in order to do the transformation, we send 107000+ queries which is just ugly. It works, but I don't feel comfortable about it.
    Solution:
    Since we are able to get a single row of 107 columns, why don't we gather the whole row, and get the information out from it instead of tiring up the server? The indefinite number of columns is a problem, but we have the whole list in an array, lets say feature_array. So what I want to do is to get the date from the row, ie myRow.feature_array(1), myRow.feature_array(2), ..., myRow.feature_array(i).
    I just learned that it's not possible to do it (from this thread).You just misunderstood the contents of this thread. What we said is that you can't do such things with static SQL, so you need to use dynamic sql.
    What has also been told was that using dynamic sql for such a purpose is just the result of a bad design. Use a relational table as a bi-dimensional array is not right, it is possible but it's not right for the simple reason that you need a procedural language to process the dynamic SQL that is needed to retrieve data.
    Probably on database programming there's only a rule of thumb and it says: "Maximize SQL minimize PL/SQL" (or any procedural language like java). The only exceptions to keep code in procedural language must depend on a complexity reduction determined by its use. In your case there is no complexity reduction determined by the use of a procedural language.
    In the thread on this link(Schema integrity I discussed about how to implement a solution you can use as alternative for submitting queries with variable column names.
    If you introduce parameter management in your model probably you could have your problem solved with the use of only a single SQL statement. And that may be a good designed model.
    Bye Alessandro

Maybe you are looking for

  • Photoshop saved a PSD file with security permissions, and now I can't access it.  Please help!

    Using Photoshop CC, I saved a PSD file after several hours of editing.  I also exported a TIF file from the PSD.  After closing Photoshop, I cannot access either file in any way.  I cannot even copy them. going to properties --> security, I get a mes

  • Help I Im stupid

    I Need help deciphering crash reports on my iMacG5, after someone was using it , I am trying to figure out if he was doing something wrong that led to a huge crash of all my programs and a terrifying ten minutes with my desk top nowhere to be found ,

  • Is MBP 13" 2.66 strong enough for FCE?

    I am making the switch from PC to Mac. I need a laptop for basic editing with both audio & video. I am more concerned with video editing, as my work has tons of audio PC's decked out. I should preface I work in radio, so my audio edits are mainly :10

  • How do I email an entire file?

    How do I email an entire folder, not just an individual file?  I want to eamil the entire file and not have to email each file individually.  Does anyone know?

  • Runtime error in Forms9iAS

    Hope someone can help: I'm trying to run a report(Forms Builder 9.0.2.7.0) but it fails with a error message: Microsoft Visual C++ Runtime Library Program: C:<orahome>\bin\ifweb90.exe abnormal program termination What causes this error message? Regar