Common table expression

Hi,
In DB2, there is a concept called 'common table expressions'.
A common table expression is like a temporary view that is defined and used for the duration of an SQL statement. You can define a common table expression for the SELECT, INSERT, and CREATE VIEW statements. Each common table expression must have a unique name and be defined only once. However, you can reference a common table expression many times in the same SQL statement.
Eg :--
insert into test1 with test_view1(column2) as (select colum1 * 2 from test2) select column2 from test_view1;
Here 'test_view1' is a temporary view created with a select statement. This view has the scope only within this SQL statement. The data inserted into the table 'test1' is selected from the temporary view.
This can be achieved in Oracle by creating views and then using the INSERT .. SELECT statement. But I like to know whether any direct equivalent available for this in Oracle, something like 'INSERT .. WITH <view_name>' as in DB2.
Please advise,
Thanks,
Smitha

Er...you mean like this?
Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
JServer Release 9.2.0.6.0 - Production
SQL> INSERT INTO emp
  2     WITH v_emp AS
  3          (SELECT empno + 1, ename, job, mgr,
  4                  hiredate, sal, comm, deptno
  5           FROM   emp)
  6     SELECT *
  7     FROM   v_emp;
14 rows created.
SQL> No, nothing like that whatsoever.

Similar Messages

  • Create a view using common table expressions

    I'm having trouble finding out if this is even possible in Oracle.
    Here is what I'm trying to accomplish with my view. A subject takes a survey numerous times across numerous dates. Depending on the responses to different questions, that subject is marked as having a set disease or affliction. The view that I'm building collects the necessary answers for each survey into one place (the first common table expression), then in subsequent CTEs, I'm adding the disease classification when the responses to the needed questions match. Sometimes the requirements state that the survey can't already be positive for other diseases, meaning that I need to ultimately discount them when checking subsequent diseases (each disease is built using it's own CTE of which there are 13). I built the query which performs these tasks with no problems. It runs and gives the responses that I'm looking for.
    The problem comes into play when I attempt to wrap the existing working query into a create view process. When I add CREATE OR REPLACE VIEW TESTENV.TESTVIEW AS ( and the ending ); after the query, it is giving me the error: ORA-32034: unsupported use of WITH clause.
    Am I getting the error simply because I can't put CTE's into views, or is it because the syntax for putting CTEs into Views is different than a normal view build process? Also, in case it makes a difference, the version of Oracle I'm using is: Oracle 10g.
    Thank you.

    Here is the view that I'm trying to build:
    <pre class="jive-pre">
    CREATE OR REPLACE VIEW GI_DEV.PAIN_COMP AS (
    WITH gatheredQuestions AS (
    SELECT
    s.date_of_survey_id,
    a.question_1 as A_1,
    a.question_5 as A_5,
    a.question_6 as A_6,
    a.question_7 as A_7,
    a.question_8 as A_8,
    a.question_9 as A_9,
    a.question_10 as A_10,
    a.question_12 as A_12,
    a.question_13 as A_13,
    a.question_14 as A_14,
    a.question_15 as A_15,
    a.question_16 as A_16,
    b.question_1 as B_1,
    b.question_4 as B_4,
    b.question_5 as B_5,
    b.question_6 as B_6,
    b.question_7 as B_7,
    b.question_8 as B_8,
    b.question_9 as B_9,
    b.question_11 as B_11,
    b.question_12 as B_12,
    b.question_13 as B_13,
    b.question_14 as B_14,
    b.question_15 as B_15,
    b.question_16 as B_16,
    b.question_16a_a as B_16a_a,
    b.question_16a_b as B_16a_b,
    b.question_16a_c as B_16a_c,
    b.question_16a_d as B_16a_d,
    b.question_16a_e as B_16a_e,
    b.question_16a_f as B_16a_f,
    b.question_16b as B_16b,
    c.question_1 as C_1,
    c.question_2 as C_2,
    c.question_3 as C_3,
    c.question_8 as C_8,
    c.question_9 as C_9,
    c.question_10 as C_10,
    c.question_11 as C_11,
    c.question_11a as C_11a,
    c.question_11b as C_11b,
    d.question_1 as D_1,
    d.question_2 as D_2,
    d.question_3 as D_3,
    d.question_4 as D_4,
    d.question_5 as D_5,
    d.question_5a as D_5a,
    d.question_5b as D_5b,
    d.question_5c as D_5c,
    d.question_6 as D_6,
    d.question_6a as D_6a,
    d.question_6b as D_6b,
    d.question_6c as D_6c,
    d.question_6d as D_6d
    FROM
    new_date_of_survey s
    LEFT OUTER JOIN new_section_a a on a.solid_visit_a_id = s.date_of_survey_id
    LEFT OUTER JOIN new_section_b b on b.solid_visit_b_id = s.date_of_survey_id
    LEFT OUTER JOIN new_section_c c on c.solid_visit_c_id = s.date_of_survey_id
    LEFT OUTER JOIN new_section_d d on d.solid_visit_d_id = s.date_of_survey_id
    ), functDyspepsia as (
    SELECT
    date_of_survey_id,
    'Functional Dyspepsia' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.A_1 in (4, 5)
    and g.A_5 in (3, 4, 5, 6)
    and g.A_6 in (0, 2, 3)
    and (g.A_7 in (0, 2) and g.A_8 in (0,2))
    and (g.A_9 in (0, 2) and g.A_10 in (0, 2))
    ), IBSLower as (
    SELECT
    date_of_survey_id,
    'IBS Lower' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.B_1 in (3, 4, 5)
    and g.B_4 in (2, 3, 4, 5, 6)
    and 2 <= (CASE WHEN g.B_5 in (3, 4, 5) THEN 1 ELSE 0 END) + (CASE WHEN (g.B_6 in (3, 4, 5) and g.B_7 not in (3, 4, 5)) or (g.B_6 not in (3, 4, 5) and g.B_7 in (3, 4, 5)) THEN 1 ELSE 0 END) + (CASE WHEN (g.B_8 in (3, 4, 5) and g.B_9 not in (3, 4, 5)) or (g.B_8 not in (3, 4, 5) and g.B_9 in (3, 4, 5)) THEN 1 ELSE 0 END)
    ), IBSUpper as (
    SELECT
    date_of_survey_id,
    'IBS Upper' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.A_1 in (3, 4, 5)
    and g.A_5 in (3, 4, 5, 6)
    and 2 <= (CASE WHEN g.A_6 in (3, 4, 5) THEN 1 ELSE 0 END) + (CASE WHEN (g.A_7 in (3, 4, 5) and g.A_8 not in (3, 4, 5)) or (g.A_7 not in (3, 4, 5) and g.A_8 in (3, 4, 5)) THEN 1 ELSE 0 END) + (CASE WHEN (g.A_9 in (3, 4, 5) and g.A_10 not in (3, 4, 5)) or (g.A_9 not in (3, 4, 5) and g.A_10 in (3, 4, 5)) THEN 1 ELSE 0 END)
    ), abMigraine as (
    SELECT
    date_of_survey_id,
    'Abdominal Migraine' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.B_16 in (3, 4, 5)
    and 2 <= ((CASE WHEN g.B_16a_a = 2 THEN 1 ELSE 0 END) + (CASE WHEN g.B_16a_b = 2 THEN 1 ELSE 0 END) + (CASE WHEN g.B_16a_c = 2 THEN 1 ELSE 0 END) + (CASE WHEN g.B_16a_d = 2 THEN 1 ELSE 0 END) + (CASE WHEN g.B_16a_e = 2 THEN 1 ELSE 0 END) + (CASE WHEN g.B_16a_f = 2 THEN 1 ELSE 0 END))
    and g.B_16b = 2
    ), lowerFunctAbPainSyndrome as (
    SELECT
    date_of_survey_id,
    'Functional Abdominal Pain Syndrome - Lower' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.B_1 in (4, 5)
    and g.B_4 in (3, 4, 5, 6)
    and date_of_survey_id not in (select date_of_survey_id from functDyspepsia union select date_of_survey_id from abMigraine union select date_of_survey_id from IBSLower union select date_of_survey_id from IBSUpper)
    and
    ((g.B_15 in (2,3,4,5) and 2 > ((CASE WHEN g.B_11 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.B_12 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.B_13 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.B_14 in (2,3,4,5) THEN 1 ELSE 0 END)))
    or (g.B_15 not in (2,3,4,5) and 2 <= ((CASE WHEN g.B_11 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.B_12 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.B_13 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.B_14 in (2,3,4,5) THEN 1 ELSE 0 END))))
    ), upperFunctAbPainSyndrome as (
    SELECT
    date_of_survey_id,
    'Functional Abdominal Pain Syndrome - Upper' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.A_1 in (4, 5)
    and g.A_5 in (3, 4, 5, 6)
    and date_of_survey_id not in (select date_of_survey_id from functDyspepsia union select date_of_survey_id from abMigraine union select date_of_survey_id from IBSLower union select date_of_survey_id from IBSUpper)
    and
    ((g.A_16 in (2,3,4,5) and 2 > ((CASE WHEN g.A_12 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.A_13 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.A_14 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.A_15 in (2,3,4,5) THEN 1 ELSE 0 END)))
    or (g.A_16 not in (2,3,4,5) and 2 <= ((CASE WHEN g.A_12 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.A_13 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.A_14 in (2,3,4,5) THEN 1 ELSE 0 END) + (CASE WHEN g.A_15 in (2,3,4,5) THEN 1 ELSE 0 END))))
    ), lowerFunctAbPain as (
    SELECT
    date_of_survey_id,
    'Functional Abdominal Pain - Lower' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.B_1 in (3, 4, 5)
    and g.B_4 in (3, 4, 5, 6)
    and date_of_survey_id not in (select date_of_survey_id from functDyspepsia union select date_of_survey_id from abMigraine union select date_of_survey_id from IBSLower union select date_of_survey_id from IBSUpper union select date_of_survey_id from upperFunctAbPainSyndrome union select date_of_survey_id from lowerFunctAbPainSyndrome)
    ), upperFunctAbPain as (
    SELECT
    date_of_survey_id,
    'Functional Abdominal Pain - Upper' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.A_1 in (3, 4, 5)
    and g.A_5 in (3, 4, 5, 6)
    and date_of_survey_id not in (select date_of_survey_id from functDyspepsia union select date_of_survey_id from abMigraine union select date_of_survey_id from IBSLower union select date_of_survey_id from IBSUpper union select date_of_survey_id from upperFunctAbPainSyndrome union select date_of_survey_id from lowerFunctAbPainSyndrome)
    ), functConstipation as (
    SELECT
    date_of_survey_id,
    'Functional Constipation' as pain_type
    FROM gatheredQuestions g
    WHERE
    2 <= ((CASE WHEN g.C_1 in (0, 2) THEN 1 ELSE 0 END) + (CASE WHEN (g.C_2 in (2,3) and g.C_3 <> 2) or (g.C_2 not in (2, 3) and g.C_3 = 2) THEN 1 ELSE 0 END) + (CASE WHEN g.C_8 = 2 THEN 1 ELSE 0 END) + (CASE WHEN g.C_9 in (3, 4, 5) THEN 1 ELSE 0 END) + (CASE WHEN g.C_10 = 2 THEN 1 ELSE 0 END) + (CASE WHEN g.C_11 in (4, 5, 6) THEN 1 ELSE 0 END))
    and date_of_survey_id not in (select date_of_survey_id from IBSLower union select date_of_survey_id from IBSUpper)
    ), fecalIncontinence as (
    SELECT
    g.date_of_survey_id,
    'Nonretentive Fecal Incontinence' as pain_type
    FROM
    gatheredQuestions g
    left outer join new_date_of_survey s on s.date_of_survey_id = g.date_of_survey_id
    left outer join new_demographics d on d.solid_demographics_id = s.demo_id
    WHERE
    4 <= trunc((months_between(s.survey_date, d.date_of_birth))/12)
    and g.C_11 in (4, 5, 6)
    and g.C_11a in (2, 3)
    and g.C_11b in (3, 4, 5, 6)
    and g.date_of_survey_id not in (select date_of_survey_id from functConstipation)
    ), aerophagia as (
    SELECT
    date_of_survey_id,
    'Aerophagia' as pain_type
    FROM gatheredQuestions g
    WHERE
    2 <= ((CASE WHEN (g.D_1 in (3, 4, 5) and g.D_2 not in (3, 4, 5)) or (g.D_1 not in (3, 4, 5) and g.D_2 in (3, 4, 5)) THEN 1 ELSE 0 END) + (CASE WHEN g.D_3 in (3, 4, 5) THEN 1 ELSE 0 END) + (CASE WHEN g.D_4 in (3, 4, 5) THEN 1 ELSE 0 END))
    ), cycVomitSyndrome as (
    SELECT
    date_of_survey_id,
    'Cycllic Vomiting Syndrome' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.D_5 in (4, 5)
    and g.D_5a in (3, 4, 5, 6)
    and g.D_5b = 2
    and g.D_5c = 2
    ), adolRumSyndrome as (
    SELECT
    date_of_survey_id,
    'Adolescent Rumination Syndrome' as pain_type
    FROM gatheredQuestions g
    WHERE
    g.D_6 in (4, 5)
    and g.D_6a = 2
    and g.D_6b = 0
    and g.D_6c = 0
    and g.D_6d = 0
    ), gatheredPains as (
    +
    select * from functDyspepsia
    union
    select * from IBSLower
    union
    select * from IBSUpper
    union
    select * from abMigraine
    union
    select * from lowerFunctAbPainSyndrome
    union
    select * from upperFunctAbPainSyndrome
    union
    select * from lowerFunctAbPain
    union
    select * from upperFunctAbPain
    union
    select * from functConstipation
    union
    select * from fecalIncontinence
    union
    select * from aerophagia
    union
    select * from cycVomitSyndrome
    union
    select * from adolRumSyndrome
    SELECT
    date_of_survey_id,
    string_agg(pain_type) as PainType
    FROM gatheredPains
    GROUP BY date_of_survey_id
    </pre>
    I tried remove the leading and ending parenthesis, and ended up getting the error that it is missing the select keyword. When I take the create view step out of this, the query builds with no problem, so I know that it's not missing a select or that it isn't in the correct place.
    Thank you for all your help so far.

  • Using Common Table Expressions in BO Universe

    Hi All,
    How to use Using Common Table Expressions(CTE) in BO Universe ?
    i created the stored procedure and tried to use it in universe but i'm not able to.
    Request you to help me out with this.
    Regards,
    Ravichandra K

    Did you get a chance to look at this guide chapter 7?
    [http://help.sap.com/businessobject/product_guides/boexir31/en/xi3-1_designer_en.pdf]
    Bashir Awan

  • Datatypes in a recursive common table expression

    I'm trying to use a (recursive) common table expression to generate a list of dates. The following code is working fine with 11.2
    WITH dates ( nr, dt ) AS (
        SELECT 1, DATE '2005-02-01'
        from dual
        UNION ALL
        SELECT d.nr + 1, DATE '2005-02-01' + 1
        FROM dates d
        WHERE d.nr < 30
    SELECT dt
    FROM dates;But I would like to avoid repeating the start date in the recursive part, and I tried the following:
    WITH dates ( nr, dt ) AS (
        SELECT 1, DATE '2005-02-01'
        from dual
        UNION ALL
        SELECT d.nr + 1, d.dt + 1
        FROM dates d
        WHERE d.nr < 30
    SELECT dt
    FROM dates;But I get the following error: ORA-01790: expression must have same datatype as corresponding expression
    Now from my understanding the datatype should be DATE for both parts of the UNION.
    I also tried it with d.dt + interval '1' day but that produces the same error message. using to_date() instead of the ANSI date literal does not change it either.
    What am I missing here?

    castorp wrote:
    I'm trying to use a (recursive) common table expression to generate a list of dates. The following code is working fine with 11.2
    WITH dates ( nr, dt ) AS (
    SELECT 1, DATE '2005-02-01'
    from dual
    UNION ALL
    SELECT d.nr + 1, DATE '2005-02-01' + 1
    FROM dates d
    WHERE d.nr < 30
    SELECT dt
    FROM dates;But I would like to avoid repeating the start date in the recursive part, and I tried the following:
    WITH dates ( nr, dt ) AS (
    SELECT 1, DATE '2005-02-01'
    from dual
    UNION ALL
    SELECT d.nr + 1, d.dt + 1
    FROM dates d
    WHERE d.nr < 30
    SELECT dt
    FROM dates;But I get the following error: ORA-01790: expression must have same datatype as corresponding expression
    Now from my understanding the datatype should be DATE for both parts of the UNION.
    I also tried it with d.dt + interval '1' day but that produces the same error message. using to_date() instead of the ANSI date literal does not change it either.
    What am I missing here?http://www.orafaq.com/forum/mv/msg/95011/463394/102589/#msg_463394

  • Memory usage Common table Expressions

    Hope view will use temporary memory while using it.. and
    If I use Common table Expressions (with <datsetname> select query )
    in stored procedure instead of view will it use the temp memory ?
    Many thanks
    Kalinga

    Why would it be any different? Its still a query. It will use any memory available for sorting
    (defined by sort_area_size)
    and if it exceeds this it will use the temporary tablespace.
    If their is more than one temporary tablespace , it will use the temporary tablespace which that user has been assigned.

  • What happens to unused common table expressions ,Does this affect in performance or ?

    If I write a query with one or more common table expressions to which I
    don't actually refer in the query, do they just get pruned off or do
    they get executed regardless? how does it affect in performance
    Prem Shah

    Try below
    seems when the CTE is not refer in the query then statement inside CTE is not executing at all even if it nested CTE, see for your self
    Create table UserInfo
    UserId int primary key,
    UserName varchar(30)
    GO
    Create table UserInfo1
    UserId int primary key,
    UserName varchar(30)
    GO
    insert into UserInfo
    select 1001,'X1' union all
    select 1002,'X2' union all
    select 1009 ,'X9'
    GO
    insert into UserInfo1
    select 1001,'X1' union all
    select 1002,'X2' union all
    select 1009 ,'X9'
    GO
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    GO
    Begin tran
    select * from UserInfo1 where UserId between 1001 and 1009
    and UserName = 'XXXX'
    --Commit
    PRINT 'WITH out CTE access in select'
    SET STATISTICS IO ON
    ;WITH CTE1 AS
    (Select * From UserInfo1)
    select * From UserInfo
    PRINT 'WITH CTE access in select'
    ;WITH CTE1 AS
    (Select * From UserInfo1)
    select * From UserInfo a inner join CTE1 b on a.UserId=b.UserId
    Stats IO
        WITH out CTE access in select
        (3 row(s) affected)
        Table 'UserInfo'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
        (1 row(s) affected)
        WITH CTE access in select
        (3 row(s) affected)
        Table 'UserInfo1'. Scan count 0, logical reads 6, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
        Table 'UserInfo'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
        (1 row(s) affected)
    Thanks
    Saravana Kumar C

  • Using Common Table Expressions (CTE) in a DSV Named Query

    Hi,
    I have a scenario where I need to "generate" fact data, so I'm using a CTE to generate the data. For maintainability reasons I have this CTE declared inside a view and in the DSV Named Query I just have a SELECT * FROM FactView.
    This works fine as long as the recursion level falls within the default limit of 100. If it does not, I have to specify the maximum recursion level such as:
    SELECT *
    FROM FactView
    OPTION (MAXRECURSION 0)
    The problem is I can't specify the Named Query like this. As explained elsewhere my query is embeded as a subquery and the OPTION keyword cannot be used in a subquery.
    Is there any other way I can specify the maximum recursion level on a Named Query?

    Load your fact table data into staging table and then process fact using direct load from staging table.

  • Extracting Metadata using Common Table Expressions MSSQL

    I have been using the below SQL query to extract subsets of the Metadata structure from several dimensions in HFM. The process works well and is simple/quick to develop and incorporate in existing processes. I am curious if anyone else has been using a similar query to extract subsets of HFM Structure directly from HFM or other approaches to extract a subset of HFM Structure.
    /*     The query starts with a "Top Parent" and will return all parent/child information below that member. For this example I want all the metadata for the c1 dimension so I am using the top most parent which by default has an ID of -1. You can look up and member's id in the "APP_DIM_Item" table and use that as the "Top Parent" for the query. It is possible to correctly extract only a subset of the overall metadata for each dimension.
    WITH dim (ItemID, ParentID, Level)
    AS
    SELECT
    el.ItemID
    ,el.[ParentID]
    ,0 AS Level
    FROM [<APP>CUSTOMLAYOUT] el     
    WHERE ParentID = -1 -- The "Top Parent" -1 will extract all metadata
              and lDimID = 1 -- Needed only for the custom dimensions.
    UNION ALL
    SELECT
         el.ItemID
    ,el.ParentID
    ,Level + 1 as Level
    FROM [ <APP>_CUSTOM_LAYOUT] el     
    INNER JOIN dim AS d
    ON el.ParentID = d.ItemID
    where el.lDimID = 1 -- needed for the custom dimensions
    /*     Once the structure is extracted to the temp dim table you can query back and
         join to the Item table to add addtional memeber details. If you leave the query as
         is you will only see the parent/child Unique ID and level informtaion.
    -- Parent/Child ID Structure
    SELECT * FROM dim
    -- Parent/Child Label Structure
    Select Dim.ItemID, Dim.ParentID, Dim.Level, Member.Label, Parent.Label
    From Dim
    inner JOIN <APP>CUSTOMITEM Member -- Member Detail
    on Member.ItemID = dim.ItemID AND Member.lDimID = 1
    inner JOIN <APP>CUSTOMITEM Parent -- Parent Detail
    on Parent.ItemID = dim.ParentID AND Parent.lDimID = 1

    Did you get a chance to look at this guide chapter 7?
    [http://help.sap.com/businessobject/product_guides/boexir31/en/xi3-1_designer_en.pdf]
    Bashir Awan

  • Where is the build table express vi located in the function table?

    I must be blind but I can't locate the build table express vi in lv8 or 8.5.  Someone slap be upside the head and point it out.  Thanks

    It's on the Controls palette. Were you looking on the Functions palette?
    Message Edited by Dennis Knutson on 04-23-2008 08:22 PM
    Attachments:
    Express XY Graph.PNG ‏17 KB

  • Generic report query that will work against common table ..

    Context
    We have a custom audit log scheme that inserts all table changes into a backing table via triggers. For example, an insert into table ORDER would cause a insert into ORDER_AUD.
    Problem
    We want to build a generic report query that will work against any audit table.
    While constructing a report query for a class that uses inheritance, we see TopLink? add an extra where-clause.
    We are required to set the ReferenceClass? for the query, however, the tables we are querying are not mapped to TopLink?. We just want to map the query results to objects of the Reference Class.
    The extra where-clause specifies the table of the Reference Class, but since that table is not in the From clause, the query doesn't work.
    The ReportQuery? in Question
    public DatabaseQuery loadAuditHistory(Class cls, String audTableName ) {
    String emanAuditTableName = "eman_audit";
    ExpressionBuilder expressionBuilder = new ExpressionBuilder();
    Expression exp = expressionBuilder.getTable(audTableName)
    .getField("instance_id")
    .equal(expressionBuilder.getParameter("instanceId"))
    .and(expressionBuilder.getTable(audTableName).getField("audit_id")
    .equal(expressionBuilder.getTable(emanAuditTableName).getField("audit_id")));
    ReportQuery query = new ReportQuery(expressionBuilder);
    query.setReferenceClass(cls);
    query.setSelectionCriteria(exp);
    query.addArgument("instanceId");
    query.addAttribute("Changed By", expressionBuilder.getTable(emanAuditTableName).getField("audit_user"));
    query.addAttribute("Host", expressionBuilder.getTable(emanAuditTableName).getField("audit_host"));
    query.addAttribute("UTC Timestamp", expressionBuilder.getTable(emanAuditTableName).getField("audit_utc_timestamp"));
    query.addAttribute("Action", expressionBuilder.getTable(audTableName).getField("table_action_code"));
    Descriptor orderDescriptor = session.getDescriptor(cls);
    Vector mappings = orderDescriptor.getMappings();
    for (Iterator iter = mappings.iterator(); iter.hasNext();) {
    Object element = (Object) iter.next();
    if (element instanceof DirectToFieldMapping) {
    DirectToFieldMapping mapping = (DirectToFieldMapping)element;
    DatabaseField field = mapping.getField();
    String tableName = field.getTableName();
    String fieldName = field.getName();
    query.addAttribute(fieldName, expressionBuilder.getTable(audTableName).getField(fieldName));
    Expression version = expressionBuilder.getTable(audTableName).getField("version_id");
    query.addOrdering(version.descending());
    query.setName(GET_AUDIT_HISTORY);
    return query;
    Code Generated by the ReportQuery?
    SELECT t0.audit_user, t0.audit_host, t0.audit_utc_timestamp,
    t1.table_action_code, t1.INSTANCE_ID, t1.STATUS,
    t1.REQUESTER, t1.DESCRIPTION, t1.RECIPIENT
    FROM eman_audit t0, example_order_aud t1
    WHERE (((t1.instance_id = 60432)
    AND (t1.audit_id = t0.audit_id))
    AND (EXAMPLE_ORDER.CLASS_NAME = 'eman.infra.toplink.example.project.model.Order'))
    ORDER BY t1.version_id DESC
    Descriptor used to Map Oder.class
    public Descriptor buildOrderItemDescriptor() {
    Descriptor descriptor = new Descriptor();
    descriptor.setJavaClass(eman.infra.toplink.example.project.model.OrderItem.class);
    descriptor.addTableName("ORDERITEM");
    descriptor.addPrimaryKeyFieldName("ORDERITEM.INSTANCE_ID");
    // Inheritance properties.
    descriptor.getInheritancePolicy().setClassIndicatorFieldName("ORDERITEM.CLASS_NAME");
    descriptor.getInheritancePolicy().useClassNameAsIndicator();
    // Descriptor properties.
    descriptor.useSoftCacheWeakIdentityMap();
    descriptor.setIdentityMapSize(100);
    descriptor.useRemoteSoftCacheWeakIdentityMap();
    descriptor.setRemoteIdentityMapSize(100);
    descriptor.setSequenceNumberFieldName("ORDERITEM.INSTANCE_ID");
    descriptor.setSequenceNumberName("CIM_ID");
    VersionLockingPolicy lockingPolicy = new VersionLockingPolicy();
    lockingPolicy.setWriteLockFieldName("ORDERITEM.VERSION_ID");
    descriptor.setOptimisticLockingPolicy(lockingPolicy);
    descriptor.setAlias("OrderItem");
    // Query manager.
    descriptor.getQueryManager().checkCacheForDoesExist();
    //Named Queries
    // Event manager.
    // Mappings.
    DirectToFieldMapping instanceIdMapping = new DirectToFieldMapping();
    instanceIdMapping.setAttributeName("instanceId");
    instanceIdMapping.setFieldName("ORDERITEM.INSTANCE_ID");
    descriptor.addMapping(instanceIdMapping);
    DirectToFieldMapping ItemNumberMapping = new DirectToFieldMapping();
    ItemNumberMapping.setAttributeName("ItemNumber");
    ItemNumberMapping.setFieldName("ORDERITEM.ITEM_NUMBER");
    descriptor.addMapping(ItemNumberMapping);
    DirectToFieldMapping QuantityMapping = new DirectToFieldMapping();
    QuantityMapping.setAttributeName("Quantity");
    QuantityMapping.setFieldName("ORDERITEM.QUANTITY");
    descriptor.addMapping(QuantityMapping);
    OneToOneMapping orderMapping = new OneToOneMapping();
    orderMapping.setAttributeName("order");
    orderMapping.setReferenceClass(eman.infra.toplink.example.project.model.Order.class);
    orderMapping.setRelationshipPartnerAttributeName("orderItems");
    orderMapping.dontUseIndirection();
    orderMapping.addForeignKeyFieldName("ORDERITEM.ORDER_ID", "EXAMPLE_ORDER.INSTANCE_ID");
    descriptor.addMapping(orderMapping);
    return descriptor;

    I'm am very confused as to what you are trying to do, are you trying to query Order objects from the ORDER_AUD historical table instead of the table that the class is mapped to?
    TopLink 10.1.3 (DP3) has built in support for historization, you may wish to investigate this support to allow historical querying of your audit tables.
    In general you cannot use a ReportQuery to do this, a ReportQuery is for querying data from objects based on an object-level criteria. I think that you want to query objects, but just using different SQL than the descriptors are mapped to.
    You could do this through a custom SQL read query,
    i.e.
    ReadAllQuery query = new ReadAllQuery(Order.class);
    query.setSQLString("Select * from ORDER_AUD where INSTANCE_ID = #id");
    query.addArgument("id");
    Vector argumentValues = new Vector(1);
    argumentValues.add(id);
    List objects = (List) session.executeQuery(query, argumentValues);
    As long as the field names in the audit and the original table matched this would work. However if this is a historical table, then I would guess that there might be multiple objects with the same id, so querying these historical objects could confuse the TopLink cache. You could set dontMaintainCache() on the query to avoid this.
    In general you would probably be much better off having two sessions, one with the descriptors mapped to the normal tables, and one with the descriptors mapped to the audit tables. Then you would be able to query both using regular object-level queries.

  • Find a common table in all apps

    Hi,
    is anyone know how to find a table or view which has been used in more than one applications under the same work space or schema. I just tried to search but it's given me only app so I have to search each app individually?
    for example table X1 has used in app1, app2, app3 and so on.
    Many thanks,
    Kind regards,

    Maybe this is relevant. This article is fairly advanced but if you are into these things, it might help.
    APEX application dependancies & deployement (See Sergio's second reply.)
    Good luck,
    Howard
    P.S. It's way beyond what I get into.

  • How do I get the Build Table Express VI of Labview?

    How can I get the build table VI ? is a package of labview?  or how can I get it?? please help, I need to make a table of a signal thnx

    It is in the base package of LabVIEW, but must be added from the front panel from the List, Table & Tree pallette (Ex Table).
    Tim W.
    Applications Engineering
    National Instruments
    http://www.ni.com/support 

  • Common Table required for Shipment and Delivery

    Hi Guys,
                 What is the Table where I can give input as Shipment Number and get all the deliveries for that Shipment.
    Thanks,
    Prasad.

    HI
    YOU CAN GET IT FROM TABLE VTTP   by giving input as shipment no. u will get  delivery no. from this table and then from table LIPS by using this delivey no. u can get all delivery details.
    and in addition to that u can also refer link: https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/e944e133-0b01-0010-caa2-be2cb240f657
    regards
    rahul
    Edited by: RAHUL SHARMA on Feb 20, 2009 8:11 AM

  • Use a common table with many to many relationship

    Hello,
    I have two SQL tables: Job and Employee. I need to compare Job Languages Proficiencies and Employee Languages Proficiencies. A Language Proficiency is composed by a Language and a Language Level.
    create table dbo.EmployeeLanguageProficiency (
    EmployeeId int not null,
    LanguageProficiencyId int not null,
    constraint PK_ELP primary key clustered (EmployeeId, LanguageProficiencyId)
    create table dbo.JobLanguageProficiency (
    JobId int not null,
    LanguageProficiencyId int not null,
    constraint PK_JLP primary key clustered (JobId, LanguageProficiencyId)
    create table dbo.LanguageProficiency (
    Id int identity not null
    constraint PK_LanguageProficiency_Id primary key clustered (Id),
    LanguageCode nvarchar (4) not null,
    LanguageLevelId int not null,
    constraint UQ_LP unique (LanguageCode, LanguageLevelId)
    create table dbo.LanguageLevel (
    Id int identity not null
    constraint PK_LanguageLevel_Id primary key clustered (Id),
    Name nvarchar (80) not null
    constraint UQ_LanguageLevel_Name unique (Name)
    create table dbo.[Language]
    Code nvarchar (4) not null
    constraint PK_Language_Code primary key clustered (Code),
    Name nvarchar (80) not null
    My question is about LanguageProficiency table. I added an Id has PK but I am not sure this is the best option.
    What do you think about this scheme?
    Thank You,
    Miguel

    That should be find as you alread have unique  non clustered index on the other two columns. you can also define this way 
    create table dbo.LanguageProficiency (
    Id int identity not null,
    LanguageCode nvarchar (4) not null,
    LanguageLevelId int not null,
    constraint PK_LanguageProficiency_Id primary key Nonclustered(LanguageCode, LanguageLevelId)
    Create CLUSTERED index CL_ID on dbo.LanguageProficiency(ID)
    What you is need is -- FOreign key constraints between these tables. I see that you are columns but are not enforcing Foreign key constraints.. you need those..
    Also you have composite primary keys, unless you guarantee that they will ever increasing numbers, it is not useful to define a composite PK.. it leads to page spilts...
    Hope it Helps!!

  • Common Table Expersion - inside existing query - How to add it in?

    I need to add somethint like the below query to an existing query. How do I do that?
    with emp_att_t
    as
    select empid,
           count(*) over(partition by empid order by 1) total_cnt,
           sum(decode(present_yn, 'N',1,0)) over(partition by empid order by 1) abs_cnt
      from emp_att
    select eh.empid, eh.name
      from emp eh
      join emp_att_t ed
        on eh.empid = ed.empid
    where ed.total_cnt > 100
       and ed.abs_cnt > 5thanks for looking.
    Edited by: GMoney on Mar 7, 2013 2:00 PM

    Please disregard the query it's self - it has nothing to do with my question really.
    The below is from a question you answered for me in this thread: Re: MAX Value for multiple fields in same Query
    WITH     got_rn          AS
         SELECT     ck.circuit_design_id
         ,     sr.document_number
         ,     ck.product_id
         ,     sr.activity_ind
         ,     sr.order_compl_dt
         ,     ROW_NUMBER () OVER ( PARTITION BY  ck.circuit_design_id
                                   ORDER BY          sr.document_number     -- or sr.order_compl_dt
                                       DESC
                           ) AS rn
         FROM    ck
         JOIN     src     ON     ck.circuit_design_id     = src.circuit_design_id
         JOIN     sr     ON     src.document_number     = sr.document_number
    --     WHERE     ...     -- If you need other filtering, put it here
    SELECT       circuit_design_id
    ,       document_number
    ,       product_id
    ,       activity_ind
    ,       order_compl_dt
    FROM       got_rn
    WHERE       rn     = 1
    ORDER BY  circuit_design_id     -- if wantedAll I am really asking is how to do an INNER JOIN on one or more other queries.
    Example
    select *
    from (
    Select abc, def
    from t1, t2
    where t1.abc = t2.abc) result1
    inner join
    WITH     got_rn          AS
         SELECT     ck.circuit_design_id
         ,     sr.document_number
         ,     ck.product_id
         ,     sr.activity_ind
         ,     sr.order_compl_dt
         ,     ROW_NUMBER () OVER ( PARTITION BY  ck.circuit_design_id
                                   ORDER BY          sr.document_number     -- or sr.order_compl_dt
                                       DESC
                           ) AS rn
         FROM    ck
         JOIN     src     ON     ck.circuit_design_id     = src.circuit_design_id
         JOIN     sr     ON     src.document_number     = sr.document_number
    --     WHERE     ...     -- If you need other filtering, put it here
    SELECT       circuit_design_id
    ,       document_number
    ,       product_id
    ,       activity_ind
    ,       order_compl_dt
    FROM       got_rn
    WHERE       rn     = 1
    result2
    ON result1.abc = result2.activity_indEdited by: GMoney on Mar 7, 2013 2:23 PM

Maybe you are looking for