How to bind local variables in complex query in PL/SQL

Hi
I have long complex query with local varibales; when I run in Dynamic SQL I get an error. Not sure which is the easiest way to bind local variables.
L_QUERY := q'[select m.segment1 col1,
      '1' col2,
      '13' col3,
      l.operand col4,
      0 col5,
       'Y' col8,
      'N' col9,
      'N' col10,
   decode(h.name,'09_UKOR_*','EOS Credit','10_UKOR_*','TopUp','11_UKOR_*','Main') col18
from qp_list_headers_v h,
      qp_list_lines_v l,
      mtl_system_items m
where h.name in ('09_UKOR_*','10_UKOR_*','11_UKOR_*')
and h.list_header_id = l.list_header_id
and m.inventory_item_id = l.product_attr_value
and m.organization_id  = 84
UNION all
SELECT qs.PRODUCT_ATTR_VALUE col1,
       hca.account_number col2,
       decode (su.site_use_code,'BILL_TO','21','SHIP_TO','18') col3,
       0 col4,
       qs.operand col5,
       null col6,
       null col7,
       'N' col8,
       'Y' col9,
       'N' col10,     
--Local variables
      (case when (trunc(nvl(qq.start_date_active,sysdate)) between l_cur_year_from  and l_cur_year_to) and  
                  ( trunc(nvl(qq.end_date_active,l_cur_year_to)) >= trunc(l_cur_year_to) )  then    'TopUp'
            when (trunc(qq.start_date_active) between l_next_year_from  and l_next_year_to) and  
                  ( trunc(nvl(qq.end_date_active,l_next_year_to)) >= l_next_year_to)  then   'Main'              
            when (trunc(qq.start_date_active) between l_last_year_from  and l_last_year_to) and  
                  ( trunc(nvl(qq.end_date_active,l_last_year_to)) >= l_last_year_to )  then  'EOS Return'
       end )col18,
--Local variables
       (case   when (trunc(qq.start_date_active) between l_next_year_from  and l_next_year_to) and  
                  ( trunc(nvl(qq.end_date_active,l_next_year_to)) >= l_cur_year_to )  then   'N'
          else
                'Y'  end ) col21
   FROM qp.qp_list_headers_b qlhb,
        qp.qp_list_headers_tl qlht,
        qp.qp_qualifiers qq,
        qp_modifier_summary_v qs,
        hz_cust_site_uses_all su,
        hz_cust_acct_sites_all  sa,
        hz_cust_accounts hca
WHERE 1=1
      and qlhb.LIST_TYPE_CODE       = 'DLT'
      and nvl(qlhb.active_flag,'Y') = 'Y'
      and qlhb.list_header_id       = qlht.list_header_id
      AND qq.list_header_id         = qlhb.list_header_id
      and qq.list_type_code         = qlhb.LIST_TYPE_CODE
      and qq.QUALIFIER_CONTEXT      = 'CUSTOMER'
      and qs.list_header_id         = qlhb.list_header_id
      and qs.LIST_LINE_TYPE_CODE    = 'DIS'
      and su.site_use_id            = qq.qualifier_attr_value --1064
      AND  su.cust_acct_site_id     = sa.cust_acct_site_id
      AND  sa.cust_account_id       = hca.cust_account_id ]';
--call Dynamic SQL function
l_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(l_cursor, p_QUERY , DBMS_SQL.NATIVE);
l_rows := DBMS_SQL.EXECUTE(l_cursor);
Any help will be much appreciated.
iI guess I should use DBMS_SQL.BIND_VARIABLE to bind all variables seperately so not to mess with query

1) What is the error you get?
2) If you use bind variables inside a query, you should prefix them with colons, so use :l_cur_year_from etc.
3) Why are you using dbms_sql instead of native dynamic sql and/or ref-cursors?
Toon

Similar Messages

  • How can i imrove peroformance of complex Query

    How can i improve performance of complex Query
    which used CASE WHEN , GROUP By Rollup
    for example
    SELECT CASE WHEN null then a
    ELSE b END
    FROM (
    SELECT a,b,c,..........
    FROM table1
    UNION ALL
    SELECT a,b,c,..........
    FROM table2
    GROUP BY ROLLUP a,b,c ,............
    this query take 18 min to return 180000 record
    is there is any way to improve the performance of this query

    Hello
    Have you considered using
    ALTER SYSTEM SET _fast=TRUE;-)
    Alternatively you could provide some more information such as an execution plan for your query:
    Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.4.0 - Production
    SQL> set linesize 200
    SQL> set pages 50000
    SQL> EXPLAIN PLAN FOR
      2  select * from dual;
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id  | Operation            |  Name       | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT     |             |       |       |       |
    |   1 |  TABLE ACCESS FULL   | DUAL        |       |       |       |
    Note: rule based optimization
    9 rows selected.If you want general info on tuning, I suggest you read the Perfomance tuning guide and the Data Warehousing guide. These are the best place to find information about concepts and techniques. The forums are better suited to specific questions.
    HTH
    David

  • Log the local variables (string type) to the database (SQL Server)

    i have a customized PreUUT callback so that my own VI gets the information from barcode. it contains serial number as well as other information. i am collecting the information into local variables of the PreUUT callback.
    Now i want to log the local variables (string type) to the database (SQL Server).
    i have a successful connection to the database and i am using a generic recordset schema.
    can anyone help me how to do it?
    also shall i have to create the corrosponding fields (columns) in the database? or is there any option in TestStand4.0 to do it?

    Hello i like original,
    After re-reading your original message, I think I might have a better understanding of what you would like to do.  I have included a few links to Knowledge Base and Developer Zone articles that should be very useful for you.  I have included these links below:
    Logging a New UUT Property to a Database in TestStand
    Logging a New Step Property to a Database in TestStand
    Creating a TestStand Database Schema from Scratch
    Thanks,
    Jonathan C
    Staff Application Engineering Specialist | CTD | CLA
    National Instruments

  • How to change Global variable in the query

    Hi,
    I have 6 reports which is using 0PDT as a global variable and has single option as a property. Now I want to change this property and have multiple selection but I am not able to change its property.
    I am able to see all the filter variables from the query property and from there I am getting edit option to change but the "single selection" option is grey/disable which is preventing me to make change in this variable. Is there any way that I delete this variable and create a local variable which has date range option.
    This is affecting all my reports and all the report needs date ranges instead of single select option.
    Thanks,
    Shivani

    Shivani,
        You can change the restrictions with new variable(right click on required infoobject and choose restrict -->> choose variables -->> move existing variable from right to left and move  new variable. Then you can see only one variable.
    You can not delete any variable if is using in some queries.
    how to restrict:
    Srini<a href="http://help.sap.com/saphelp_nw04/helpdata/en/f1/0a563fe09411d2acb90000e829fbfe/content.htm">Restricting Characteristics</a>

  • How to store data from a complex query and only fresh hourly or daily?

    We have a report which runs quite slow (1-2 minutes) because the query is quite complicate, so we would like to run this query daily only and store in a table so for those procedures that need to use this complex query as a subquery, can just join to this table directly to get results.
    However, I am not sure what kind of object I should use to store data for this complex query. Is data in global temp table only persist within transaction? I need something that can persist the data and be access by procedures.
    Any suggestions are welcome,
    Cheers

    Thank you for your reply. I looked at the materialized view earlier on, but have some difficulties to use it. So I have some questions here:
    1.The complex query is not a sum or aggregate functions, it just need to get data from different tables based on different conditions, in this case is it still appropriate to use meterialized view?
    2.If it is, I created one, but how to use it in my procedure? From the articles I read, it seems I can't just query from this view directly. So do I need to keep the complex query in my procedure and how the procedure will use the meterialized view instead?
    3. I also put the complex query in a normal view, then create a materialized view for this normal view (I expect the data from the complex query will be cache here), then in the procedure I just select * from my_NormalView, but it takes the same time to run even when I set the QUERY_REWRITE_ENABLED to true in the alter session. So I am not sure what else I need to do to make sure the procedure use the materialized view instead of the normal view. Can I query from the Materialized View directly?
    Below in the code I copied from one of the article to create the materialized view based on my normal view:
    CREATE MATERIALIZED VIEW HK3ControlDB.MW_RIRating
    PCTFREE 5 PCTUSED 60
    TABLESPACE "USERS"
    STORAGE (INITIAL 50K NEXT 50K)
    USING INDEX STORAGE (INITIAL 25K NEXT 25K)
    REFRESH START WITH ROUND(SYSDATE + 1) + 11/24
    NEXT NEXT_DAY(TRUNC(SYSDATE), 'MONDAY') + 15/24
    enable query rewrite
    AS SELECT * FROM HK3ControlDB.VW_RIRating;
    Cheers

  • How to use local variables to pass an image mask correctly

    I'm kind of new to labview but i'll try to explain the problem as best as i can: I'm trying to pass an image mask (basically an image) to the next iteration of a while loop using local variables. I think the image passes through the loop with the local variable but i can't read from it correctly for some reason. And I don't think the problem has to do with local variables because i've tried using shift registers and that didnt work either. I think the problem is that you need to do something to read the image correctly again, like using IMAQ copy or something (that didnt work tho), but i can't figure out what the problem is. Does anyone know what the problem is? I know this isnt a great explanation and if its too confusing i could send some snapshots of the program or something. Any help would be greatly appreciated though.
    Thanks,
    Will

    So i attached 2 snapshots of the program to give a better idea of the problem. The first snapshot shows an image getting written to the local variable SavedMask. The second snapshot, which is run on the next iteration of a while loop, shows the local variable SavedMask being read to other image operations. When i run the program, the SavedMask image is always displayed correctly on the front panel, but i can't read from it for whatever reason. I think the problem could be like you said, that im only passing an imaq reference, and i think theres a certain way to extract the image data. Do you know the correct way to extract the image data or how to pass the image data and not just a reference to the data.
    Attachments:
    first.jpg ‏81 KB
    second.jpg ‏68 KB

  • How to Populate  local variable values to tables like likp .

    Hi..I have taken 3 local variables date1 date2 and date3 .How to populate them in table (likp-lfdat).In date1 i have dates of lfdat between 14/05 to 21/05 and date2 from 22/05 to 28/05 and date3 one more week.In o/p how can see data between these dates range only?

    Use Where clause as:
    WHERE
    LFDAT IN DATE1
    OR LFDAT IN DATE2
    OR LFDAT IN DATE3.
    Regards,
    Anil

  • How to use local variable in member formula?

    Hi Expert,
    We would like to use a {local variable} in a member formula, so that it can pick up the prompt from webform upon save. Would like to know if it is possible to do? If so, what is the correct syntax?
    We attempted to use brace {scenario}, blacket [scenario], blacket with quote ["scenario"]. None of these work.
    We are using 11.1.2.1 version, EPMA, Hyperion Planning.
    Thanks in advance for your help!

    One other pointer. Where did you define your variable? Hopefully you went into Calc Manager and selected <Tools>, <Variables>. From there, navigate to your application or database. Right-click on your application or database and select "New". In the bottom panel, you'll need to select your Variable Type. Make sure you select "Member", and not "Members". You cannot pass "Members" (plural) to a web input form. Also, if you want to define a variable for a custom dimension, you'll need to do it at the database level. Standard dimension member variables can be defined at the app level. (Not sure why this is the case, especially when my custom dimension exists in all plan types . . . . )
    Anyway, hope this helps,
    - Jake

  • How come my local variable is not updating it's value with respect to what's happening in the while loop?

    Hello,
    I am trying to extract data out of a while loop as my declarations update with respect to the iteration number. I have attempted to use both local variables and shift registers, but with no luck.
    I have also done the following example: http://www.ni.com/white-paper/7585/en and it works like a charm.
    I attached the PNG file with local variable declaration circled in red. Will attach a VI in the next respnose.
    Thanks
    Solved!
    Go to Solution.
    Attachments:
    Local Variable.png ‏366 KB

    OK, looking at the code...
    Can you explain what it is supposed to do? What's the purpose of the value property node read which only seems to update an indicator.
    The inner while loop should proably be a FOR loop, because the number of iterations is known before the loop starts.
    Your use of formula nodes seems overly complicated.
    LabVIEW Champion . Do more with less code and in less time .

  • How to declare local variables in PL/SQL stored programs

    Where do I declare local variables in a PL/SQL stored program?
    I get compiler errors with either of the options below:
    FUNCTION GET_PRINCIPAL_BALANCE (CUT_OFF_DATE IN DATE, TITLE_SN IN DATE, TOTAL_BALANCE OUT NUMBER) RETURN NUMBER IS
    TOTAL_BALANCE NUMBER;
    BEGIN
    RETURN TOTAL_BALANCE;
    END;
    FUNCTION GET_PRINCIPAL_BALANCE (CUT_OFF_DATE IN DATE, TITLE_SN IN DATE, TOTAL_BALANCE OUT NUMBER) RETURN NUMBER IS
    BEGIN
    TOTAL_BALANCE NUMBER;
    RETURN TOTAL_BALANCE;
    END;

    Your local variable cannot have the same name as the formal out parameter. This is a procedure example, but since functions should not have out parameters anyway ...
    session2> CREATE PROCEDURE p (p_id IN NUMBER, p_did OUT NUMBER) AS
      2     p_did NUMBER;
      3  BEGIN
      4     p_did := p_id * 2;
      5  END;
      6  /
    Warning: Procedure created with compilation errors.
    session2> show error
    Errors for PROCEDURE P:
    LINE/COL ERROR
    0/0      PL/SQL: Compilation unit analysis terminated
    1/1      PLS-00410: duplicate fields in RECORD,TABLE or argument list are
             not permittedThe proper way to create a function would be something like:
    CREATE FUNCTION f (p_id IN NUMBER) RETURN NUMBER AS
       l_did NUMBER;
    BEGIN
      l_did := p_id * 2;
      RETURN l_did;
    END;You should really assign a value to the variable before you return it.
    John

  • How to pass local variables between different sequence files?

    Actually i want to pass the data (local variable) from my process model file to a client file. The client file only has the callbacks and those callbacks require some parameters as input which is available in local variables of my process model sequence file. I do not want to use Station Globals.
    Please tell me any other way by which i can pass that data.

    Which Locals do you want from the model.  Every sequence has it's own Locals and there are a bunch of sequences in every process model.  Do you want the entry point's Locals?  A callback's Locals?  Just one of the sequence's Locals?  Some Models have way more entry points than others.  What if your sequence file is ran with a different entry point?
    One option is to create your own callback in the model and pass the data to it as parameters.  Then add that callback to your client sequence file and get it out of the parameters.
    If you simply want the entry point's locals and you are in your Main Sequence then you could use RunState.Caller.Locals.VariableName.  This assumes you will use the same entry point every time you run that sequence file.  Or assumes that every entry point you run has the same Local variable name.
    Maybe there is an API method that exposes what you are looking for.  What is your goal?
    Regards,
    jigg
    CTA, CLA
    teststandhelp.com
    ~Will work for kudos and/or BBQ~

  • How to use local variables declared in b/t the methods in other views

    Hi
    i need to use the value of the local variables declared in b/w the views in one other view,as i have manipulated the value based on some condition & want to use it in 1 more view for my req.,but that variable is not global one just for once i need to use it,and i can't again write the whole for its conditions,as its there in initialization view,which if coded again,can hit the functionality there,so is it possible to use the value of such local variables thereafter or not,if yes pls let me know,or i need to declare the global variable for this.

    hi,
    1.Declare an Attribute in Component controller.
    2. Pass the value to this Attribute in View V1.
             using :
      wd_comp_controller->Att = 'Saurav'.
    3. Now in view2 , you can access this Attribute using:
      data lv type string.
         lv = wd_comp_controller->Att .
    Here Att is my attribute of type string declared in component controller.

  • How to find the variable in the Query?

    Hi Expert,
    I test the BEx query by the tfcode RSRT1, after exectued,  Go to "Info" tab and we can see the "Variable Values" colum, however, the variable values dont show all variable defined in Query, do you know which type of varible will show in this colum? Thanks very much.
    Best Regards,
    Andrew

    Hi,
    The reason you are not able to see all the variables in query designer while they are there in RSRT1 is that:
    1. Their might be some condition used in your report which need user input
    2. Their might be a restricted Key Figure with restriction based on user input
    In order to see all the variables in query designer available in RSRT1 report. Go to properties tab on the rightmost side in BI7 and select the Query. Now click on variable screen tab here you will see list of all the variables used in the report.
    Regards
    Shubh
    Edited by: Shubh Karan Rathore on Nov 16, 2010 10:57 AM

  • How to create a variable in Bex Query Designer

    Hi guys,
    I am writing a query for a travel management report using the SAP Standard cube. I would like to restrict the query so it only shows me the last two months excluding the current month. I have checked BI content in RSA1, but I canu2019t find a relevant SAP standard variable that I can use.
    Can anyone please tell me if I can create my own? And if so, how I can do this.
    Many Thanks
    Forhad.

    lets say u want to display following picture:
    sept$-----august$
    City
    -chicago123$--
    44$
    so in Query designer go to Default values section.
    Bring in Calendar month
    right click calendar month
    and hit Restrict
    now select variable
    use standard sap variable Current Month.
    hit okey
    now select Variable and hit Red button
    Result u restricted current month.
    in columns bring in keyfigure $
    right click and hit edit
    bring in time field calendar month
    right click and hit restrict
    bring in Current month
    right click and select - SET OFFSET FOR VARIABLE
    select -1
    hit okey
    repeat the above process
    and this time select offset = -2
    this way u restricted current month
    but displayed last 2 months for keyfigure values.

  • How to bind DATE variables in SQL*Plus

    I have a stored procedure in a package that has a DATE OUT parameter
    myPackage.myProcedure( outDate OUT DATE )
    I am trying to test this from SQL*Plus
    How do I specify a Bind variable for the outDate? The VARIABLE command does not allow DATE types?
    If this was a CHAR type I could have done
    VARIABLE myChar CHAR
    BEGIN
    myPackage.myProcedure( :myChar )
    END;
    PRINT myChar
    How do I do this with DATE data types instead? I am using Oracle 8.1.7.

    you should use VARCHR2 type with the minimum length that can store a value defined by your NLS_DATE_FORMAT
    string. The returned date will be converted to a VARCAHR2 type and the size and format will depend on
    your NLS_DATE_FORMAT setting.
    =============================================================================
    SQL> variable dt VARCHAR2(20)
    SQL> create or replace procedure ret_date(dt OUT DATE) is
    2 begin
    3 dt := SYSDATE ;
    4 end ;
    5 /
    Procedure created.
    SQL> exec ret_date(:dt) ;
    PL/SQL procedure successfully completed.
    SQL> print dt
    DT
    13-OCT-02
    SQL> select sysdate from dual ;
    SYSDATE
    13-OCT-02
    SQL> alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS' ;
    Session altered.
    SQL> exec ret_date(:dt) ;
    PL/SQL procedure successfully completed.
    SQL> print dt
    DT
    13-OCT-2002 10:43:23
    SQL>
    ================================================================

Maybe you are looking for