Can we use Case in Where Clause along with Exists

Hi Everybody,
Can we use Case in the where clause with exists? As i have a requirement in which i have to check whether value exists in 6 views, now depending on some value(gns_type )of select clause i have to attach a paticular exists else the performance dies.
Please go through the query any suggestion appreciated.
Thanks
SELECT count(*)
FROM
(SELECT eah.changed_date,
decode(eua.is_deleted, 'N', decode(eah.alert_type, NULL, 'GN', 'R', 'GAR', 'G', 'GAG', 'Y', 'GAY'), 'Y', decode(eah.alert_type, 'R', 'GDR', 'G', 'GDG', 'Y', 'GDY', NULL, 'GN'), NULL, 'GN') AS
alert_type,
decode(eac.pta_line, 'N', '') ptaline,
eac.exp_type_desc,
eac.supplier_name,
eac.transaction_id,
eah.gns_type,
eac.po_amount,
eac.po_end_date,
eah.notes,
eua.is_deleted,
eac.expenditure_type,
eua.gns_alert_summary_id,
eah.changed_date alert_date,
eua.user_alert_id,
eah.reference_number,
decode(eac.cms_pta_line,'N','',eac.cms_pta_line) cms_pta_line,
cms_po_amount,
cms_po_end_date,
mgns.is_decommitted,
eac.gns_alert_id,
eah.gns_type source_name
FROM xxdl.xxdl_sc_gns_alerts_summary eah,
xxdl.xxdl_sc_gns_detail_alerts eac,
xxdl.xxdl_sc_gns_user_alerts eua,
xxdl.xxdl_sc_manage_gns_master mgns
WHERE eah.gns_alert_summary_id = eac.gns_alert_summary_id
AND eah.gns_alert_summary_id = eua.gns_alert_summary_id
AND eah.transaction_id = eac.transaction_id
AND eah.transaction_id = mgns.transaction_id)
a
WHERE(EXISTS
(SELECT 1
FROM xxdl_sc_mng_gns_pta_req_hc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS( SELECT 1
FROM xxdl_sc_mng_gns_pta_inv_hc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS(SELECT 1
FROM xxdl_sc_mng_gns_pta_req_sc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS(SELECT 1
FROM xxdl_sc_mng_gns_pta_inv_sc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS( SELECT 1
FROM xxdl_sc_mng_gns_pta_po_sc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS (SELECT 1
FROM xxdl_sc_mng_gns_pta_po_hc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
AND TRUNC(alert_date) >= TRUNC(add_months(sysdate, -1))
AND TRUNC(alert_date) <= TRUNC(sysdate)
AND is_deleted = 'N'
ORDER BY changed_date DESC

you can do
WHERE
CASE WHEN (something) THEN
  CASE WHEN EXISTS (SELECT * from ...) THEN 1 ELSE 0 END
           WHEN (something else) THEN
     CASE WHEN EXISTS (SELECT * from ...) THEN 1 ELSE 0 END      
END = 1Looking at your current query, it looks like all those exist statements could be a lot neater, maybe like:
WHERE (69309,242528,34694) IN
(SELECT project_id,task_id,award_id FROM
  (Select project_id,task_id,award_id,transaction_id,source_name
  FROM
  xxdl_sc_mng_gns_pta_req_hc_v
  UNION ALL
  Select project_id,task_id,award_id
  xxdl_sc_mng_gns_pta_inv_hc_v
  ...) x
where a.transaction_id = x.transaction_id
and a.source_name = x.source_name
)or put the tuple in the where clause at the bottom

Similar Messages

  • Can i use Decode in Where clause

    Hi,
    Can i use Decode in Where clause Please Do the need full on the same.
    Thanks,
    Sanjeev.

    set serveroutput on
    DECLARE
    posn  PLS_INTEGER := 0;
    empid PLS_INTEGER := 178;
    x     NUMBER;
    BEGIN
      SELECT NVL(SUM(ah.quantity * ah.saleprice * ap.payoutpct), 0)
      INTO x
      FROM accessoryhistory ah, payoutpercentage ap,
      sku s, store st
      WHERE empid = DECODE(posn,
                              0, st.areadir,
                              1, st.areamgr,
                              2, NVL(st.storemgr1, st.storemgr2),
                              3, NVL(st.asstmgr1, NVL(st.asstmgr2,
                           st.asstmgr3)))
      AND ah.statustype IN ('ACT', 'DEA')
      AND ah.store = st.store
      AND s.dbid = ah.dbid
      AND s.sku = ah.sku
      AND ap.productgroup = s.productgroup
      AND ap.position = posn;
      dbms_output.put_line(x);
    END;
    /http://psoug.org/reference/decode_case.html

  • Using CASE on WHERE clause?

    Is it posible to use a CASE on the WHERE clause?
    I always used the following syntax on MSSQL
    but Oracle aparently doesn't accept it:
    SELECT...
    WHERE
         CASE WHEN keyword IS NOT NULL THEN
              CONTAINS(field, keyword) > 0
         END AND
         CASE WHEN varcode IS NOT NULL THEN
              code = varcode
         END AND
         OTHER STATEMENTS

    SQL> SELECT
    2 COUNT(*) OVER() total,
    3 ed_fotos.id AS COD_FOTO,
    4 ed_fotos.foto.getwidth()/100 AS VLR_LARGURA,
    5 ed_fotos.foto.getheight()/100 AS VLR_ALTURA,
    6 ed_fotos.arquivo AS NOM_ARQUIVO,
    7 ed_areas.nome AS NOM_REDACAO
    8 FROM edt.ed_fotos ed_fotos
    9 INNER JOIN edt.ed_areas ed_areas ON
    10 ed_areas.id = ed_fotos.redacao_id AND
    11 (ed_areas.ind_licenciada <> 'S' OR
    12 ed_areas.ind_licenciada IS NULL
    13 OR (ed_areas.id IN (SELECT * FROM THE (SELECT CAST(fda_
    adm.in_list('') AS fda_adm.mytableType) FROM dual)))
    14 )
    15 LEFT JOIN edt.ed_creditos_fotos ed_creditos_fotos ON
    16 ed_creditos_fotos.foto_id = ed_fotos.id
    17 LEFT JOIN edt.ed_itens ed_itens ON
    18 ed_itens.foto_id = ed_fotos.id
    19 LEFT JOIN edt.ed_edicoes ed_edicoes ON
    20 ed_edicoes.id = ed_itens.edicao_id
    21 LEFT JOIN edt.vw_dedoc_ccda vw_dedoc_ccda ON
    22 vw_dedoc_ccda.num_ccda = ed_fotos.num_ccda_sct AND
    23 vw_dedoc_ccda.cod_tipo_documento IN ('B','C','C1','C2')
    24 LEFT JOIN fda_adm.vw_lui vw_lui ON
    25 vw_lui.foto_id = ed_fotos.id AND
    26 vw_lui.cod_tipo_documento IN ('B','C')
    27 WHERE
    28 CASE WHEN 'BUSH' IS NOT NULL THEN
    29 CONTAINS(ed_fotos.detalhe, '{BUSH} within termo
    OR {BUSH} within headline OR {BUSH} within desc') > 0
    30 END) AND
    31 1 = 1
    32 GROUP BY
    33 ed_fotos.id,
    34 ed_fotos.foto.getwidth(),
    35 ed_fotos.foto.getheight(),
    36 ed_fotos.registro,
    37 ed_fotos.arquivo,
    38 ed_areas.nome,
    39 ed_areas.id
    40 ORDER BY
    41 ed_fotos.id DESC
    42 /
    CONTAINS(ed_fotos.detalhe, '{BUSH} within termo
    OR {BUSH} within headline OR {BUSH} within desc') > 0
    ERRO on line 29:
    ORA-00905: missing keyword

  • Can we use concatenate in where clause of a select query

    I have a select query as follows....
        SELECT ebeln ebelp belnr vgabe bwart DMBTR
                                 FROM ekbe
                                 INTO TABLE i_ekbe
                                 FOR ALL ENTRIES IN i_blck
                                 WHERE ebeln =  i_blck-ebeln AND ebelp =                                                                               
    i_blck-ebelp.
    Now i want to retrieve data from BKPF ..can i write a select query something like....
    select * from BKPF into itab
                FOR ALL ENTRIES IN i_ekbe
                where awkey = ( conatenate i_ekbe-belnr i_ekbe-gjahr )
    OR ELSE...is there any other way to link table ekbe and bseg ???

    Hi Poonam,
    SELECT ebeln ebelp belnr vgabe bwart DMBTR
    FROM ekbe
    INTO TABLE i_ekbe
    FOR ALL ENTRIES IN i_blck
    WHERE ebeln = i_blck-ebeln
    AND ebelp = i_blck-ebelp.
    select *
    from BKPF
    into  table itab
    FOR ALL ENTRIES IN i_ekbe
    where belnr = i_ekbe-belnr.
    If u want to check with AWKEY concatenate into seperate field and then use to check in where condition.
    data: lw_awkey(20) type c.
    CONCATENATE  i_ekbe-belnr  i_ekbe-gjahr  INTO lw_awkey.
    select *
    from BKPF
    into  table itab
    FOR ALL ENTRIES IN i_ekbe
    where awkey = lw_awkey
    Best regards,
    raam

  • Can both in-row and off-row LOBs be used in a where clause?

    We are preparing to upgrade ASE from version 15.5 to 16.0.  One of the features that is very appealing has to do with the changes made concerning the handling of LOBs.  In particular, the ability to use a LOB column in search arguments (where clause) is something our developers have longed for since the retirement of the full-text search option.  So, in version 16.0, can any column with a datatype of text be used as a search argument? Are there any restrictions.
    Thanks in advance.

    Hi Kevin,
    The in-row LOB feature is mainly designed to save space when a text column has many short values, which is a particularly bad problem when the server uses a large page size (like 16K) as ASE would use a full 16K to store even a single bye "Y".  I don't think the feature made any change in how text can be used in search arguments.  i.e. you can still use LIKE and IS [NOT] NULL, but not others.
    Example:
    1> create table t (x text in row (8))
    2> go
    1> insert t values ("abc")
    2> insert t values ("abcdefghijkl")
    3> go
    (2 rows affected)
    1> select * from t where x like "%c%"
    2> go
    x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    abc                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    abcdefghijkl                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    (2 rows affected)
    1> select * from t where x !> "c"
    2> go
    Msg 257, Level 16, State 1:
    Server 'redhead', Line 1:
    Implicit conversion from datatype 'TEXT' to 'VARCHAR' is not allowed.  Use the
    CONVERT function to run this query.
    Msg 306, Level 16, State 5:
    Server 'redhead', Line 1:
    TEXT, IMAGE and UNITEXT datatypes may not be used in a WHERE clause, except with
    the LIKE expression and IS [NOT] NULL predicate on columns.

  • Using a dummy where-clause to force the use of an index

    I notice that an index only gets used when I use the index key in the where clause. Should I use a dummy where clause to force the index to be used?
    SQL> create table emp (
    2 empno NUMBER(5),
    3 empname VARCHAR2(15) );
    Table created.
    SQL> create index idx_emp_no on emp (empno);
    Index created.
    SQL> insert into emp (empno, empname) values (1, 'Peter');
    1 row created.
    SQL> set autotrace traceonly;
    SQL> select empno from emp;
    Execution Plan
    Plan hash value: 3956160932
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 13 | 3 (0)| 00:00:01 |
    | 1 | TABLE ACCESS FULL| EMP | 1 | 13 | 3 (0)| 00:00:01 |
    SQL> select empno from emp where empno > 0;
    Execution Plan
    Plan hash value: 434430053
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)| 00:00:01 |
    |* 1 | INDEX RANGE SCAN| IDX_EMP_NO | 1 | 13 | 1 (0)| 00:00:01 |
    As you can see, using a dummy where clause (empno > 0), I manage to reduce the cost from 3 to 1.

    Again. Be careful with comparisons. But I like the discussion. It is usefull to carefully look at what happens "behind the scenes" and find a sensible explaination for that.
    Your new scenario now is different. By selecting one specific row instead of selection all you already made an assumption. That only one or few of the rows are returned. Andre's statement still is correct. Now the CBO will prefere the index scan.
    The CBO now has to decide between two options.
    Option a) use the index.
    This means =>
    Step 1: Access the index to find the value
    Step 2: Access the table using the rowid that was found in the index leaf entry.
    Option b) scan the full table
    This means =>
    Step 1: Iterate over all rows of the table
    Step 2: Apply a filter condition (empno = :x)
    We can see this execution plan by adding a hint to the query.
    select /*+ FULL(emp) */ * from emp where empno = 1;This hint will force the CBO to access the table emp using a full table scan.
    The cost in my environment for option B is 3.
    Option A has a cost with 2. Therefore the CBO prefers Option A.
    However this can easily change.
    Consider the following
    begin
      for i in 1..1000 loop
        insert into emp (empno, empname) values (1, 'Employee');
      end loop;
    end;
    commit;
    execute dbms_stats.gather_table_stats(user,'EMP');The run the select again.
    SQL> set autotrace traceonly
    SQL> select * from emp where empno = 1;
    1001 Zeilen ausgewõhlt.
    Ausf³hrungsplan
    Plan hash value: 3956160932
    | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT  |      |  1001 | 12012 |     3   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| EMP  |  1001 | 12012 |     3   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter("EMPNO"=1)
    Statistiken
              1  recursive calls
              0  db block gets
             74  consistent gets
              0  physical reads
              0  redo size
          18356  bytes sent via SQL*Net to client
           1250  bytes received via SQL*Net from client
             68  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
           1001  rows processedThe CBO now prefers the full table scan and not the index access.
    If we force it to use an index then the cost increases.
    SQL> set linesize 100
    SQL> select /*+INDEX(emp) */ * from emp where empno = 1
    1001 Zeilen ausgewõhlt.
    Ausf³hrungsplan
    Plan hash value: 2426388914
    | Id  | Operation                   | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT            |           |  1001 | 12012 |     5   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| EMP       |  1001 | 12012 |     5   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | IDX_EMPNO |  1001 |       |     2   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - access("EMPNO"=1)
    Statistiken
              0  recursive calls
              0  db block gets
            139  consistent gets
              0  physical reads
              0  redo size
          30550  bytes sent via SQL*Net to client
           1250  bytes received via SQL*Net from client
             67  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
           1001  rows processed
    SQL>The cost using the index range scan is now 5. WHile the FTS is still 3. Therefore the CBO prefers to use the FTS.

  • Using Date in where clause

    Hello all,
    I am new to Oracle, currently using 10G + aspvbscript.
    I've been trying to query data using date in where clause but nothing seems to work.
    The column is in date format.
    It gets printed out like this: 5/1/2010 11:21:19 AM
    I tried using this query:
    SELECT * from table where TRUNC(user_date) > to_date('FEB-01-2010:00:00:00','mm-dd-yyyy:HH24:MI:SS') order by user_date asc.
    It does return an output but it returns everything in table and does not take WHERE clause into consideration however, it does sort the date in ascending order.
    I've tried getting rid of TRUNC tried to format date in a different way but no such luck.
    Please point me to the right direction.
    Thanks.

    Welcome to the forums!
    In cases like this it is helpful if you can provide the following information:
    1. Oracle version (SELECT * FROM V$VERSION)
    2. Sample data in the form of CREATE / INSERT statements.
    3. Expected output
    4. Explanation of expected output (A.K.A. "business logic")
    5. Use \ tags for #2 and #3. See FAQ (Link on top right side) for details.
    I'll try and take a stab at your request based on the data given. What your query says is that it will return all rows that have a date greater then 2/1/2010 (MM/DD/YYYY). If your query is returning all rows then maybe the possibility exists that all the dates in the table are greater then 2/1/2010. Have you checked all dates to see if this is the case?
    Also, one note about your TO_DATE() function.to_date('FEB-01-2010:00:00:00','mm-dd-yyyy:HH24:MI:SS')The date format does not match the string you are using with respect to month. Your string has 'FEB' but the format is 'MM' which is the numeric representation of the month. Although Oracle was able to convert it to the proper date on my system you should try and maintain consistency between the string and the date format used.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Case in where clause

    Hello to everyone
    I have a select like as
    select
              where   (KPMERCE IS NULL AND KSMERCE IS NULL)
         UNION
              where   (KPMERCE IS NOT NULL AND KSMERCE IS NULL AND K_PMERCE = KPMERCE)
         UNION
              where   (KSMERCE IS NOT NULL AND K_PMERCE = KPMERCE AND K_SMERCE = KSMERCE)is possibile create a CASE in where clause, something as, So, i can write a single query,      
              CASE  (in where-clause)
              WHEN   (KPMERCE IS NULL AND KSMERCE IS NULL)                            
              WHEN   (KPMERCE IS NOT NULL AND KSMERCE IS NULL AND K_PMERCE = KPMERCE)
          WHEN   (KPMERCE IS NOT NULL AND KSMERCE IS NOT NULL AND K_PMERCE = KPMERCE AND K_SMERCE = KSMERCE)  so
    IF CONDITION IS SATISFYED RETURNs ROWS OTHERWISE NO ROWS
    Thanks in advance

    If your select lists are equal, then you can do it like this:
    where (  (KPMERCE IS NULL AND KSMERCE IS NULL)
           or (KPMERCE IS NOT NULL AND KSMERCE IS NULL AND K_PMERCE = KPMERCE)
           or (KSMERCE IS NOT NULL AND K_PMERCE = KPMERCE AND K_SMERCE = KSMERCE)
           )Regards,
    Rob.

  • Error while using REMAP_TABLE and WHERE clause  together in IMPDP

    I am trying to move some records from a very large table to another small table.
    I am facing trouble while using REMAP_TABLE and WHERE clause together in IMPDP.
    Problem is data filter is not getting applied and all records are getting imported.
    here is how I have simulated this. please advice.
    CREATE TABLE TSHARRHB.TMP1
      A  NUMBER,
      B  NUMBER
    begin
    Insert into TSHARRHB.TMP1
       (A, B)
    Values
       (1, 1);
    Insert into TSHARRHB.TMP1
       (A, B)
    Values
       (2, 2);
    COMMIT;
    end;
    expdp system/password TABLES=tsharrhb.TMP1 DIRECTORY=GRDP_EXP_DIR DUMPFILE=TMP1.dmp REUSE_DUMPFILES=YES LOGFILE=EXP.log PARALLEL=8
    impdp system/password DIRECTORY=GRDP_EXP_DIR DUMPFILE=TMP1.dmp LOGFILE=imp.log PARALLEL=8 QUERY='TSHARRHB.TMP1:"WHERE TMP1.A = 2"'  REMAP_TABLE=TSHARRHB.TMP1:TMP3 CONTENT=DATA_ONLY
    Import: Release 11.2.0.1.0 - Production on Fri Dec 13 05:13:30 2013
    Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    With the Partitioning, Automatic Storage Management, OLAP, Data Mining
    and Real Application Testing options
    Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
    Starting "SYSTEM"."SYS_IMPORT_FULL_01":  system/********@GRD6.RBSG DIRECTORY=GRDP_EXP_DIR DUMPFILE=TMP1.dmp LOGFILE=SSD_93_TABLES_FULL_EXP.log PARALLEL=8 QUERY=TSHARRHB.TMP1:"WHERE TMP1.A = 2" REMAP_TABLE=TSHARRHB.TMP1:TMP3 CONTENT=DATA_ONLY
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    . . imported "TSHARRHB"."TMP3"                           5.421 KB       2 rows
    Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at 05:13:33
    here I am expecting only 1 record to get imported but both the records are getting imported. please advice.

    The strange thing compared to your output is that I get an error when I have table prefix in the query block:
    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
    Starting "SYSTEM"."SYS_IMPORT_FULL_01":  system/******** DUMPFILE=TMP1.dmp LOGFILE=imp.log PARALLEL=8 QUERY=SYSADM.TMP1:"WHERE TMP1.A = 2" REMAP_TABLE=SYSADM.TMP1:TMP3 CONTENT=DATA_ONLY
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    ORA-31693: Table data object "SYSADM"."TMP3" failed to load/unload and is being skipped due to error:
    ORA-38500: Unsupported operation: Oracle XML DB not present
    Job "SYSTEM"."SYS_IMPORT_FULL_01" completed with 1 error(s) at Fri Dec 13 10:39:11 2013 elapsed 0 00:00:03
    And if I remove it, it works:
    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
    Starting "SYSTEM"."SYS_IMPORT_FULL_01":  system/******** DUMPFILE=TMP1.dmp LOGFILE=imp.log PARALLEL=8 QUERY=SYSADM.TMP1:"WHERE A = 2" REMAP_TABLE=SYSADM.TMP1:TMP3 CONTENT=DATA_ONLY
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    . . imported "SYSADM"."TMP3"                             5.406 KB       1 out of 2 rows
    Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at Fri Dec 13 10:36:50 2013 elapsed 0 00:00:01
    Nicolas.
    PS: as you can see, I'm on 11.2.0.4, I do not have 11.2.0.1 that you seem to use.

  • Using function in where clause

    I have created a function as follows
    create or replace FUNCTION get_codes RETURN varchar2 IS
    scodes varchar2(50) := 'A1,A2';
    BEGIN
    scodes := '('''||REPLACE(scodes,',',''',''')||''')';
    return scodes;
    END;
    this function returns ('A1','A2')
    now i want to use this in where clause, both below statements fetches no rows
    select * from tablea where code in (select get_codes from dual);
    select * from tablea where code in get_codes;
    but the following will fetch rows
    select * from tablea where code in ('A1','A2')
    how to use function in where clause
    Thanks

    Hi,
    The code that works:
    where code in ('A1','A2')is testing whther code is either of two 2-character strings, A1 or A2.
    The ones that don't work, such as:
    where code in get_codes;are comparing code to one 9-character string: 'A1','A2', where characters 1, 4, 6 and 9 are single-quotes. (The function can only return one value.)
    You can use dynamic SQL to embed that 9-character string in part of your query, where it will be interpreted as two 2-character strrings.
    [Oracle Base|http://www.oracle-base.com/articles/misc/DynamicInLists.php] and AskTom have good pages on the subject of dynamic IN-lists.
    Of you can use INSTR or LIKE:
    where INSTR ( get_codes
                , '''' || code || ''''
                ) > 0

  • Using distinct in where clause

    Greedings,
    I am trying to execute some kind of distinct using rowid in where clause but for some reason im not getting the appropriate result. Any guidance is welcome
    SELECT acnt_code,cat,cat_desc,buc FROM so_budgets_cat where
    Trim(buc)='S03'
    and
    NOT exists (SELECT  1
                       from   so_budgets_cat tab2
                       where  (  tab2.cat     =  so_budgets_cat.cat )
                       and    tab2.ROWID  > so_budgets_cat.ROWID
    ORDER BY cat,so_budgets_cat.rowidThanks in advance

    Additionally to the fact that we don't have an idea on the expected output, I can't understand the comparison of ROWIDs in your query. These are the logical addresses where the data reside in the disks, comparing them doesn't make sense to me; Oracle DB defines where to store, based on space availability in the disk allocation to the database occupation - and NOT in a logical sequence.
    Maybe the existing row is stored in an address that is not a higher number than other, and so it is not being retrieved in your sub-query - hence your test for distinction is failing.

  • I find it annoying that you can't use aliases in where statements

    I find it annoying that you can't use aliases in where
    statements
    for example:
    SELECT TOP 1
    CAST(amount
    / ( CASE WHEN ISNULL(rate, 0) <> 0 THEN rate
    ELSE 1
    END ) AS NUMERIC(10, 2)) AS num
    FROM table
    WHERE num > 1
    ORDER BY num
    Are there any workarounds?

    > I find it annoying that you can't use aliases in where
    statements
    This isn't really (or... "at all") a CF question. You're
    better off asking
    this sort of thing on a SQL Server forum.
    You could use a derived table, I guess:
    select top 1 num
    from (
    SELECT
    CAST(amount
    / ( CASE WHEN ISNULL(rate, 0) <> 0 THEN
    rate
    ELSE 1
    END ) AS NUMERIC(10, 2)) AS num
    FROM table
    where num > 1
    order by num
    (that might not preserve the intent of you SQL (sorry: it's
    late, I'm
    tired), but you get the idea.
    Adam

  • Can I use case statements in triggers?

    I created this trigger, it works BUT I don't like those parentheses at the begining, I would like
    to change those parentheses for case statements, well that is my question, can you use case statements in triggers, how you would translate the following in case statement?
    FOR EACH ROW
    WHEN ( (new.sgbstdn_levl_code = 'UG')
    and
    ( (NEW. SGBSTDN_STST_CODE NOT IN ('GR','SA','AS','IS') )
    OR
    ( (NEW. SGBSTDN_STST_CODE = 'IS' ) AND
    (NEW. SGBSTDN_STYP_CODE IN ('N' , 'T' )) AND
    (OLD. SGBSTDN_STST_CODE = 'AS' ) ) ) )
    ==================================================================================================
    CREATE OR REPLACE TRIGGER CC_STUD_WITHDRAWAL
    AFTER UPDATE OR INSERT ON SATURN . SGBSTDN
    FOR EACH ROW
    WHEN ( (new.sgbstdn_levl_code = 'UG')
    and
    ( (NEW. SGBSTDN_STST_CODE NOT IN ('GR','SA','AS','IS') )
    OR
    ( (NEW. SGBSTDN_STST_CODE = 'IS' ) AND
    (NEW. SGBSTDN_STYP_CODE IN ('N' , 'T' )) AND
    (OLD. SGBSTDN_STST_CODE = 'AS' ) ) ) )
    DECLARE
    v_params gokparm.t_parameterlist;
    event_code gtveqnm.gtveqnm_code%TYPE;
    firstname spriden.spriden_first_name%TYPE;
    lastname spriden.spriden_last_name%TYPE;
    middlename spriden.spriden_mi%TYPE;
    id spriden.spriden_id%TYPE;
    CURSOR get_stud_name IS
    SELECT
    spriden_id ,
    spriden_last_name ,
    spriden_first_name ,
    spriden_mi
    FROM
    saturn.spriden
    WHERE spriden_pidm = :NEW.SGBSTDN_PIDM
    AND spriden_change_ind IS NULL;
    BEGIN
    IF goksyst . f_isSystemLinkEnabled ( 'WORKFLOW' ) THEN
    event_code := SUBSTR ( gokevnt.F_CheckEvent ( 'WORKFLOW' ,'CC_STUDENT_WITHDRAW' ),1,20);
    OPEN get_stud_name ;
    FETCH get_stud_name INTO id , lastname , firstname , middlename ;
    CLOSE get_stud_name ;
    ----pass parameters to the event
    v_params ( 1 ).param_value := 'CC_STUDENT_WITHDRAW' ;
    v_params ( 2 ).param_value := '' ;
    v_params ( 3 ).param_value := 'Student Withdrawal:' || lastname || ',' || firstname || ' ' ||
    middlename ;
    v_params ( 4 ).param_value := :NEW.sgbstdn_pidm ;
    v_params ( 5 ).param_value := id ;
    v_params ( 6 ).param_value := lastname ;
    v_params ( 7 ).param_value := firstname ;
    v_params ( 8 ).param_value := middlename ;
    v_params ( 9 ).param_value := :NEW.sgbstdn_term_code_eff ;
    v_params ( 10 ).param_value := :NEW.SGBSTDN_STST_CODE ;
    v_params ( 11 ).param_value := :NEW.SGBSTDN_STYP_CODE ;
    gokparm.Send_Param_List ( event_code , v_Params );
    END IF;
    END;
    /

    You could delete a fair number of extraneous parentheses.
    CREATE OR REPLACE TRIGGER cc_stud_withdrawal
      AFTER UPDATE OR INSERT
      ON saturn.sgbstdn
      FOR EACH ROW
      WHEN     NEW.sgbstdn_levl_code = 'UG'
           AND (   NEW.sgbstdn_stst_code NOT IN ('GR', 'SA', 'AS', 'IS')
                OR (    NEW.sgbstdn_stst_code = 'IS'
                    AND NEW.sgbstdn_styp_code IN ('N', 'T')
                    AND OLD.sgbstdn_stst_code = 'AS'))

  • Can you use home share where there are 2 itunes on the same computer but under different user profiles?

    Can you use 'Home Share' where there are 2 itunes on the same computer, but under different user profiles?

    check out method one from this support article -> How to use multiple iPods, iPads, or iPhones with one computer

  • HELP I have lost all my motion 4 content from all the files, ive looked in all the files and they are all empty. can i get replacement content as I my dvds where stolen along. with my other laptop.

    HELP I have lost all my motion 4 content from all the files, ive looked in all the files and they are all empty. can i get replacement content as I my dvds where stolen along. with my other laptop. any Ideas. would be a great help. For Motion 4.

    Try a hard reset:- hold the home button AND the power switch - ignore the red "Slide to power off" and wait ubntil the iPhone powers down, and then restarts - at the Apple logo, release all the buttons and see if your contacts reappear.

Maybe you are looking for

  • How to export bookmaks from an old hard drive

    my old computer crashed i removed the hard drive to extract files and i cannot find my book marks i pick up an drive kit so i can hook up my old drive via usb and i have found no help for anyone doing it this way i was running xp and i dont remember

  • 10.4.6 broke my Internet Sharing HELP!

    Hi There I just updated my HOST computer - a G5 dual 2.0g - from OS 10.4.7 to 10.4.8. I previously connected a PowerBook G4 1.25g to the above HOST via FIREWIRE and AIRPORT under 10.4.7 and both worked perfectly. After updating it doesn't work! Under

  • Are you allowed to track the browser info on IOS?

    I want to make a news app that shows you relevant news on the basis of your IOS browser information and i wonder if you are allowed to do this on IOS. It is obvious that the user needs to accept the terms of condition but does Apple accept this?

  • List of transations supported in SAP 4.6c and not in ECC6.0

    Hi Folks, My company is planning to upgrade the SAP 4.6c to mySAP (ECC 6.0). Is it possible to get the list of transactions of 4.6C which are not supported in ECC6.0 if yes then any1 let me know how??

  • HT4489 how to import Contacts in iCloud from Exchange?

    how to import Contacts in iCloud from Exchange?