Using a variable as a SELECT field

I try to get shipping to a different weight and postal codes. I query a table to get a zip code provided by the user area. This works. Then, I want to use this area to retrieve the shipping cost for a specific weight of another table. I put the box as a variable and use it as follows: SELECT #Zone # FROM Ground WHERE Wt = weight # #. If I use the name of the actual area instead of the variable, everything works fine. When I use the variable, however and try out of her results with the variable #Zone #, I get the original name in the field name box, not the rate that is listed under this domain name

It is good to use a variable of the way I have? If so, how to get the result? If not, is there another way to get a field name for a query from another query? Thank you.

You can use a variable to dynamically change the select field, but it is rarely a good idea.
Here's how you would do in this case:


SELECT
#Zone # AS ShippingEstimate
Of
On the ground
WHERE
WT = #weight #.


#weight # kilograms, to the shipping area: #Zone #, cost #MaintenanceNightmare.ShippingEstimate # Euros.



The suggestion to normalize your database, was a good thing.

Tags: ColdFusion

Similar Questions

  • Using Bind Variable in a SELECT statement

    Hello

    I am trying to build my SQL query running using bind variables and in Oracle® Fusion Middleware Fusion developer Guide for Oracle Application Development Framework 11 g Release 1 (11.1.1) it is said that ' after you define bind variables, the next step is to reference them in the SQL statement. While SQL syntax allows you to bind variables to appear in the SELECT list and in the WHERE clause, you'll generally use them in this context, as part of your WHERE clause. ».

    However, when I try to use bind variables in my SELECT list because I had set a type to the string of the variable variable is inserted with quotes each side for example SELECT TestTable FROM 'Service '. Is it possible to use bind variables to insert a value in my list of selection without the quotes around it?

    Thanks in advance,
    Tom

    Hi Robinst,

    I think you want to set up column name of the table that is not possible using bind variables. With the help of the bind variables you can send a value to the SQL. The String value is therefore always with inverted commas.

    Kind regards

    Branislav

  • using variable in the select statement (php)

    I have trouble using a variable in a select statement.

    The following query to manually (using a static date) works very well:

    $query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > ' 2006-06-01' ';"

    However, if I use the following:

    $dateVar = date('Y-m-d');

    @mysql_select_db ($database_mw, $mw);
    $query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > $dateVar";

    All records are returned, which means that the $dateVar variable is not recognized. I compared the values of $dateVar <? PHP echo $datetime;? > against the real value of my date field <? PHP echo $row_Recordset1 ["classDate"];? > in my results to the table and it seems that the values are indeed accurate regarding a date 2006-06-04 for example.

    My date field 'classDate' is of type 'date' in the mysql database. I use Dreamweaver MX 2004 with Mac OSX 10.3.9.

    I'm sure it's just a syntax problem, at least, I hope it is.

    Any help is greatly appreciated.



    It worked:

    $query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > = CURRENT_DATE;

    Thank you
    MW

  • Can I use a variable in step of TS Message for field 'waiting time '?

    Is it possible to use a variable (inhabitants, parameters, etc.) for the field "Wait time" in the stage of message TestStand?

    TestStand 4.1

    Thank you

    Rafi

    Go to the step settings > properties > Expressions > Expression Pre and enter something like:

    Step.TimeToWait = Locals.MyTimeToWait

  • Using a variable in an Insert / Select

    Oracle 11 g 2

    It is a block of the size of the code. I want to use a variable in the clause values an Insert / Select using the execute immediate statement.

    g_sysdate date := sysdate
    

    I can't ' use sysdate directly because it is a long-term process. We attribute sysdate to g_sysdate at the beginning of the procedure and we stored in several tables. Therefore, it has the same value everywhere.

    I'm getting ORA-00904: "G_SYSDATE": invalid identifier

    create table usa.work_time
    ( id number ,
      work_status_id number ,
      work_task_id number ,
      txt_msg varchar2(255)
    )
    /
    
    create table usa.work_time_arc
    ( id number ,
      work_status_id number ,
      work_task_id number ,
      txt_msg varchar2(255),
      archived_dt date
    )
    /
    
    insert into usa.work_time values(1,2,3,'hello');
    
    commit ;
    
    DECLARE
       v_query varchar2(2000);
       g_sysdate date := sysdate ;
       v_tbl_arc varchar2(61) := 'usa.work_time_arc';
       v_tbl_src varchar2(61) := 'usa.work_time';
       v_tab_cols varchar2(2000) := 'id,work_status_id,work_task_id,txt_msg';
       v_id number := 9521;
    BEGIN
       v_query := 'insert /*+ append */ into '||v_tbl_arc || ' (' || v_tab_cols || ',archived_dt)' || 
                                  'select ' || v_tab_cols || ',g_sysdate from ' || v_tbl_src || 
                               ' where id = :id';
       dbms_output.put_line(v_query);
       
       execute immediate v_query using v_id ;
    END ;
    /
    
    ERROR at line 1:
    ORA-00904: "G_SYSDATE": invalid identifier
    ORA-06512: at line 14
    
    
    
    

    That should do it.

    DECLARE
       v_query varchar2(2000);
       g_sysdate date := sysdate ;
       v_tbl_arc varchar2(61) := 'usa.work_time_arc';
       v_tbl_src varchar2(61) := 'usa.work_time';
       v_tab_cols varchar2(2000) := 'id,work_status_id,work_task_id,txt_msg';
       v_id number := 9521;
    BEGIN
       v_query := 'insert /*+ append */ into '||v_tbl_arc || ' (' || v_tab_cols || ',archived_dt)' ||
                                  'select ' || v_tab_cols || ',:g_sysdate from ' || v_tbl_src ||
                               ' where id = :id';
       dbms_output.put_line(v_query);  
    
       execute immediate v_query using g_sysdate, v_id ;
    END ;
    /
    
  • Treatment options for form (use the value of a form field to select a shared list or a web redirect page)

    Hello

    I tried something with the processing of the forms which doesn't seem to work. I wonder if the function really works or if I'm doing something wrong.

    Here is an example of what I do: I have a form that I want to use. I add "Web Page redirection" processing step to my form. When I click on the processing step to set up, under "General settings" there is an option that says "Choose how the launch of destination page is selected." In this drop-down menu, there are three options: always redirect to the same landing page, use a drop-down list to select the landing page and the value of a form field allows you to select the destination page. I have a hidden field on the form that contains the URL of the web page to which I would like to redirect. When I select 'Use the value of a form field to select the landing page', I select the hidden field to the field that contains the URL of the web page to which I want to redirect. It's my setup. However, when I submit the form, nothing happens, I do not redirect to the URL contained in the hidden field... This function still works? I'm doing something wrong? I can't seem to get the function "Use a drop-down list to select the destination page" to work either. The same thing happens when I try to use the "Add Contact to shared the list" processing step. I add the name of the list that is shared in the field hidden, but the contact does not appear in the shared list... These functions still work? If yes how do I configure them?

    Thank you!

    Well, I understood the problem of the shared list... You must add the ID of the list shared in the hidden field. I had to use Firebug to see the real ID of the shared list. Once you have the ID, use you it as a static value in the field. If the HTML code of the form will look something like this:

    If you do not use the lookup data ID. You use the ID real asset on the shared list. You must use something like Firebug to find.

    I'm glad everything is working.

  • 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;

  • Capture selected vales of Date dates invite you using the variable of presentation

    We have an obligation to display the date on the text of the report / whatever the user selects the date from the command prompt.
    We have a date called created and used operator is between.
    Can someone help me in this area, the use of variable presentation to capture the values of a single Date column of creation?

    In the criteria to add more than 2 columns for Date of creation
    and use these expressions foreach and hide them
    MAX (RMAX (creation Date))
    MIN (RMIN (creation Date))

    In your story view we will call @1 and @2. assuming that these 2 columns are first from left to right.

    No need to use variables of the presentation.

    Mark as correct

    Thank you

    Published by: Srini VIEREN on April 22, 2013 16:47

  • Can I use a variable, all referring to a text field?

    Hello

    I have to settle for some fields readOnly property DYNAMICALLY (I mean, until the moment of execution, I do not know which fields are read-only and which are not?  .

    So I want to replace the TextField1 dynamically with all the fields in the syntax below. So, can we put a variable in the position of TextField1? If so, how can I do that?

    xfa.resolveNode ("VISITOR. Page3.SubFrom1.TextField1') .access = "readOnly";

    xfa.resolveNode ("VISITOR. Page3.SubFrom1. MyVariableComesHere') .access = 'readOnly ';

    How can I put the variable in the Red position?

    In details:

    I have 5 fields, then I want to readOnly, then I hv to write as below,

    xfa.resolveNode ("VISITOR. Page3.SubFrom1.TextField1') .access = "readOnly";

    xfa.resolveNode ("VISITOR. Page3.SubFrom1.TextField2') .access = "readOnly";

    xfa.resolveNode ("VISITOR. Page3.SubFrom1.TextField3') .access = "readOnly";

    xfa.resolveNode ("VISITOR. Page3.SubFrom1.TextField4') .access = "readOnly";

    xfa.resolveNode ("VISITOR. Page3.SubFrom1.TextField5') .access = "readOnly";

    In fact, my requirement is differnent (I know at run time only... Wht wht fields I hv to set readOnly and wht wht fields should allow very good for entry), for this reason I want to use a VARIABLE in the 'position' of TextField1, TextField2... as bleow

    $myvariable var

    $myvariable = TextField1

    Closure of the TABLE

    xfa.resolveNode ("VISITOR. Page3.SubFrom1. myVariable') .access = 'readOnly ';

    Pls. Let me know I can sue a variable like this? If so, what is the correct syntax (pls.  Suggest me a free code to SYNTAX error, bcz am very very beginner to this)

    Thank you

    Thank you

    You can use the following...

    $myvariable var

    $myvariable = TextField1

    xfa.resolveNode ("VISITOR. (Page3.SubFrom1.' + myVariable + "") .access = "readOnly";

    Thank you

    Srini

  • Text added to the text field using the variable does not appear

    Hello

    I have a test field I created dynamically.  If I add some text to it by writing: myField.text = 'text', the text if poster, but if I add text using a variable is nothing.  If I trace trace text in the text field ("text =" myField.text) get good value.

    Someone at - it ideas?

    Best regards

    Chris McLaughlin

    Try to comment out the two lines below and see if it helps...

    ["textField" + guestCueCnt 'c'] .defaultTextFormat plus myFormat;
    ["textField" + guestCueCnt + 'c'] .embedFonts = true;

  • using the variable as a field name in a query

    Oracle 10G


    Hi all

    I have a table like:

    TEST_MSL
    CID NUMBER (10),
    TEST_1 VARCHAR2,
    TEST_2 VARCHAR2

    Now, I want to use a procedure such as:

    create or replace procedure TEST
    (VARIABLE_1 IN VARCHAR2) Possible values (TEST_1, TEST_2)
    as
    BEGIN

    declare
    cursor c1 is
    Select the CID
    where VARIABLE_1 = 'BLAH '.

    My problem is:

    How can I use this variable as a domain name?

    Kind regards

    Menk Slot

    Published by: Menk slot on June 3, 2010 10:46

    If you know that there are only two possibilities so preferable to use as static SQL statements

    procedure test(v_cloumn_name varchar2) IS
    IF v_column_name='TEST_1' THEN
    SELECT CID INTO var where TEST_1='BLABLA';
    ELSE
    SELECT CID INTO var where TEST_2='BLABLA';
    END;
    

    Otherwise, use Ref cursour as

    declare
    v_sql_stm varchar2(200);
    refcur sys_refcursor;
    begin
    v_sql_stm:='SELECT CID FROM TEST_MSL WHERE '||v_cloumn_name||' = ''BLABLA''';
    open refcur for v_sql_stm;
    
  • Problem using bind variables in shared components report queries

    Greetings,

    I use APEX 4.0.1.00.03 and BI Publisher 10.1.3.4.1.

    In the APEX, I try to create a report query that uses bind variables in the where clause. I have a page that contains a button and a text element (P1_ID). The button is used to call the report. I went the shared components and created a new report query. Here is the text of the code.
    select violation_date
    , violation_type
    , nvl(:P1_ID,'zzz')
    from edd_procard_violations
    where upper(card_holder_id) = upper(:P1_ID)
    I also have Session State to enabled and have added the P1_ID in the area of 'State of Session' under the query.

    When I change the query, and click on the 'Set Bind Variables' button, I get a field where I can enter a value for: P1_ID I enter a value that should return lines, but when I click on the button "Test query", I get no rows returned. If I comment on where clause and test the query, even once, I get all rows in the table. You'll notice in my query I have included nvl(:P1_ID,'zzz'). When I run the query, without where clause, the returned value is always "zzz" indicating that: P1_ID is null.

    No idea what I am doing wrong?

    Thank you very much.
    Larry

    Hello Larry,.

    Looks like the wizard to create a report query in the shared components has a bug related to bind variables. I was able to reproduce your problem. Just create the query using the link variable, check the box to include session state, add the item and create the query. When you use the report query to download the report, where the your clause will work fine.

    Thank you
    Machaan

  • Using global variables in a device custom

    Hello world

    I have problems with the passing of data through my device customized using global variables. I want to allow a user to select a RIO device address when you set up the system definition file. This is recorded in a global variable using the hand Page VI. I would later use this RIO device address in RT driver to deploy a bitfile. I can hard-code, but it is much more convenient to use a global.

    When I try to store the address of the global variable, it updates the value temporarily (I confirmed that the structure of the event recognizes the change of value). When I click the node of the tree to define system linked to the Main Page VI and then click on it again, the hand Page VI runs again. I wrote the code to repopulate the address of device of RIO with the last value stored in the global variable, but it is empty. I've included the VI global variables in my build, so I can't imagine why it can reset this value.

    Any help would be appreciated!

    Thank you

    Mitch

    Have you tried to use the custom device properties to store this value instead of GVs?

    Configuration VI, you can set this property, and if I'm not mistaken, you can read these properties on the RT driver.

  • Limit set using the variable

    IAM having the following query to load the data of my DataModel

    DataSource {
                id: firstSource
                source: "db/duDatabase.db"
                query: "SELECT title FROM how_to ORDER BY howToId ASC limit 10"
                onDataLoaded:
                    {
                        firstModel.insertList(data);
                    }
               }
    

    My Question is:

    How can I use a variable to set the limit in the query

    That is to say, I want something like that

     query: "SELECT title FROM how_to ORDER BY howToId ASC limit variableName"
    

    Please please help me. IAM stuck here.

    query: "SELECT title OF how_to ORDER BY howToId limit of ASC"+ NomDeLaVariable"

  • Estimate of poor cardinality using Bind Variables

    Hi I'm using the 11.2.0.4.0 Oracle version. I have a query that is underway for the plan of the poor execution by the estimate of poor cardinality for two tables (I've extracted and published this part only) as I mentioned below, the individual conditions for which the estimate goes bad and moving entire query execution path.

    These are for two tables and currently we use BIND variable for them in our code, and I notice, its best estimate gives with literals. I need to know how to handle this scenario that I need this query to execute for all types of volumes. Is there something I can do without changing the code, as it works well for most of the execution? In the current scenario of the main query that uses those below tables providing a plan (index + nested loop) that works very well for small volume, but running for 10 hr + for large volume as ideally its going to the same regime.
    And Yes, most time that this request will be hit for small volume, but killing some appearance of large volume presents the performance of the queries.


    Here are the values of the variable binding.

    B1 VARIABLE VARCHAR2 (32);
    B2 VARIABLE VARCHAR2 (32);
    B3 VARIABLE NUMBER;
    B4 VARIABLE VARCHAR2 (32);
    B7 VARIABLE VARCHAR2 (32);
    B5 VARIABLE NUMBER;
    B6 VARIABLE NUMBER;

    EXEC: B1: = 'NONE ';
    EXEC: B2: = NULL;
    EXEC: B3: = 0;
    EXEC: B4: = NULL;
    EXEC: B7: = NULL;
    EXEC: B5: = 0;
    EXEC: B6: = 0;

    ---- For  TABLE1-------
     -- Published Actual VS Etimated cardinality
     
     
    -- With bind values
    select * from TABLE1 SF
    WHERE (   (SF.C1_IDCODE = :B4) OR (NVL (:B4, 'NONE') = 'NONE'))
        AND ( (SF.C2_ID = :B3) OR (NVL (:B3, 0) = 0));
    Plan hash value: 2590266031
    -----------------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation                 | Name                | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
    -----------------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT          |                     |      1 |        |  28835 |00:00:00.08 |    2748 |     46 |       |       |          |
    |*  1 |  TABLE ACCESS STORAGE FULL| TABLE1              |      1 |     11 |  28835 |00:00:00.08 |    2748 |     46 |  1025K|  1025K|          |
    -----------------------------------------------------------------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       1 - storage((("SF"."C1_IDCODE"=:B4 OR NVL(:B4,'NONE')='NONE') AND ("SF"."C2_ID"=:B3 OR NVL(:B3,0)=0)))
           filter((("SF"."C1_IDCODE"=:B4 OR NVL(:B4,'NONE')='NONE') AND ("SF"."C2_ID"=:B3 OR NVL(:B3,0)=0))) 
     
    -- With literals 
    select * from TABLE1 SF
     WHERE  (   (SF.C1_IDCODE = null) OR (NVL (null, 'NONE') = 'NONE'))
          AND ( (SF.C2_ID = 0) OR (NVL (0, 0) = 0));
       Plan hash value: 2590266031
    --------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation                 | Name                | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
    --------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT          |                     |      1 |        |  28835 |00:00:00.03 |    2748 |       |       |          |
    |   1 |  TABLE ACCESS STORAGE FULL| TABLE1              |      1 |  28835 |  28835 |00:00:00.03 |    2748 |  1025K|  1025K|          |
    --------------------------------------------------------------------------------------------------------------------------------------
    
    --------For TABLE2 ----------------------- 
    -- Published Autotrace plan, as it was taking long time for completion, and actual cardinality is 45M, but its estimating 49 With bind value---
    
    --withbind value
    select * from TABLE2 MTF
    WHERE (   (MTF.C6_CODE = TRIM (:B2)) OR (NVL (:B2, 'NONE') = 'NONE'))
      AND (   (MTF.C3_CODE = :B1)  OR (NVL (:B1, 'NONE') = 'NONE'))
      AND (   (MTF.C4_CODE = :B7)  OR (:B7 IS NULL))
      AND (   (MTF.C5_AMT <= :B6)  OR (NVL (:B6, 0) = 0))
      AND (   (MTF.C5_AMT >= :B5)  OR (NVL (:B5, 0) = 0));
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1536592532
    -----------------------------------------------------------------------------------------------------------
    | Id  | Operation                  | Name         | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    -----------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT           |              |    49 | 10437 |   358K  (1)| 01:11:43 |       |    |
    |   1 |  PARTITION RANGE ALL       |              |    49 | 10437 |   358K  (1)| 01:11:43 |     1 |  2 |
    |*  2 |   TABLE ACCESS STORAGE FULL| TABLE2       |    49 | 10437 |   358K  (1)| 01:11:43 |     1 |  2 |
    -----------------------------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - storage(("MTF"."C4_CODE"=:B7 OR :B7 IS NULL) AND ("MTF"."C3_CODE"=:B1 OR
                  NVL(:B1,'NONE')='NONE') AND ("MTF"."C5_AMT"<=TO_NUMBER(:B6) OR NVL(:B6,0)=0) AND
                  ("MTF"."C5_AMT">=TO_NUMBER(:B5) OR NVL(:B5,0)=0) AND ("MTF"."C6_CODE"=TRIM(:B2) OR
                  NVL(:B2,'NONE')='NONE'))
           filter(("MTF"."C4_CODE"=:B7 OR :B7 IS NULL) AND ("MTF"."C3_CODE"=:B1 OR
                  NVL(:B1,'NONE')='NONE') AND ("MTF"."C5_AMT"<=TO_NUMBER(:B6) OR NVL(:B6,0)=0) AND
                  ("MTF"."C5_AMT">=TO_NUMBER(:B5) OR NVL(:B5,0)=0) AND ("MTF"."C6_CODE"=TRIM(:B2) OR
                  NVL(:B2,'NONE')='NONE'))
      
    -- with literal
    select * from TABLE2 MTF
    WHERE (   (MTF.C6_CODE = TRIM (null)) OR (NVL (null, 'NONE') = 'NONE'))
     AND (   (MTF.C3_CODE = 'NONE') OR (NVL ('NONE', 'NONE') = 'NONE'))
      AND (   (MTF.C4_CODE = null)  OR (null IS NULL))
       AND (   (MTF.C5_AMT <= 0)  OR (NVL (0, 0) = 0))
      AND (   (MTF.C5_AMT >= 0)  OR (NVL (0, 0) = 0));
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1536592532
    -----------------------------------------------------------------------------------------------------------
    | Id  | Operation                  | Name         | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    -----------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT           |              |    45M|  9151M|   358K  (1)| 01:11:41 |       |    |
    |   1 |  PARTITION RANGE ALL       |              |    45M|  9151M|   358K  (1)| 01:11:41 |     1 |  2 |
    |   2 |   TABLE ACCESS STORAGE FULL| TABLE2 |    45M|  9151M|   358K  (1)| 01:11:41 |     1 |  2 |
    -----------------------------------------------------------------------------------------------------------
    
    select column_name,num_nulls,num_distinct,density
    from dba_tab_col_statistics where table_name='TABLE2'
    and column_name in ('C3_CODE','C4_CODE','C5_AMT','C6_CODE');
    C3_CODE 0 65 0.0153846153846154
    C4_CODE 0 2 0.5
    C5_AMT 0 21544 4.64166357222429E-5
    C6_CODE 1889955 71 0.0140845070422535
    
    

    933257 wrote:

    ((SF. C1_IDCODE =: B4) OR (NVL (: B4, 'NONE') = 'NONE'))

    In fact for literals, I did not find any section of the predicate after running the sql code with activation "set autotrace traceonly explain."

    The main problem is with another large query whose cardinality is underestimated due to the presence of these table (table1, table2) with the above mentioned clause, and the query is for the analysis of index + nested with values of Bind loops and take 10 hr +, whereas with literals, its completion in ~ 8minutes with FTS + Hash Join.

    Your real problem is that you try to have just a single SQL query handle all POSSIBLE thanks to the use of embedded FILTERS ' either / or ' filters in the WHERE clause.  You want only a select this OPTION to run whatever filters have been selected at run time by the user or the application using it.  And it would never work.  You really need to SELECT different queries for different combinations of filter conditions.

    Why?  Think for a minute.  How Oracle works internally?  A SQL SELECT query gets analyzed and an execution plan is produced which is stored in the library cache and gets REUSED on all subsequent executions of this query - except in certain cases where there may exist several plans run through several cursors of the child.  So with only SELECT a query you only AN execution plan in the library cache, to be used by all THE executions of this query, regardless of the value of your run-time binding variables.

    Lets put another way - each library cache execution plan is associated with a SQL statement.  If you want a DIFFERENT execution plan then you need run a DIFFERENT SQL statement.  That's how you get a different execution plan - by running a different SQL statement.  Running the SAME SQL query generally you will get the SAME execution plan every time.

    In addition, because of the "either / or" filters that you use you will end up generally with a full Table Scan on each of the referenced tables.  Why?  Given that the optimizer must produce an implementation plan that manages all possible contingencies for all values of possible bind variables in the SELECT.  If the optimizer should choose to use any index based on one of these "either / or" filters then it would only help performance when real value was provided, but it would be really bad if a NULL value was supplied.  If the optimizer ends up ignoring the index because they are not always optimal for all possible input values and instead chose a plan that is "good enough" for all input values possible.  That means that it will use a scanning Table full.

    I hope you can see that it is precisely what is happening for you with your query.  You select this OPTION to manage the different combinations of filter, which leads to the execution plan only one, which leads to scans full Table on the referenced tables in these ' either / or ' filters.

    The solution?  Build queries SELECT DIFFERENT when input values are NULL.  How you do that?  Read this article to ask Tom that tells you:

    http://www.Oracle.com/technetwork/issue-archive/2009/09-Jul/o49asktom-090487.html

    To sum up - when you have real value for a bind variable 'bind_var1' add the following filter to your CHOICE:

    AND column_name1 =: bind_var1

    When the binding variable is NULL, add the filter according to your CHOICE:

    AND (1 = 1 OR: bind_var1 IS NULL)

    Now, you'll have 2 queries SELECT must be performed, which have exactly the same number of variables in the same order bind, which is important.  When you then run one of these variations, Oracle can analyze and optimize each one SEPARATELY, with a single execution by the SELECT query plan.

    When you provide a real value, the filter is a normal 'column = value' that the optimizer can use all indexes on this column, because NULL values are not referenced.

    When there is no real value, the optimizer will analyze the '1 = 1 GOLD' and realize that "1 = 1" is set to TRUE and GOLD, it is quite TRUE regardless because the binding variable is null or not.  This means that the optimizer will actually REMOVE this filter, because it filters nothing because it is always TRUE.  You will end up with an operating plan based on the other filters in the query, which is what you want because you have no filter on this column.

    What is it - producing distinct SELECT queries to determine if you have a real value to filter or not you end up with DIFFERENT execution plans for each of them, and each of them is OPTIMAL for this particular set of filters.  Now you get good performance for each variation of the performance of the SELECTION, rather than sometimes good and sometimes very bad when using SELECT only one.  It is impossible to try to get multiple shots of execution 'optimal' out of a SELECT query.  That's why you get mediocre performance under different bound the values of the variables.

    John Brady

Maybe you are looking for