Factored SubQuery to insert data

Hello,
I am trying to use a Factored SubQuery to insert data.. I am getting the following error:
Error report:
SQL Error: ORA-00928: missing SELECT keyword
00928. 00000 - "missing SELECT keyword"
Below is an example of what I am trying to do.
Thanks, sck
WITH
fsqAcutals AS
SELECT
Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code,
SUM(CASE WHEN Ttime.Quarter = '1' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q01,
SUM(CASE WHEN Ttime.Quarter = '2' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q02,
SUM(CASE WHEN Ttime.Quarter = '3' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q03,
SUM(CASE WHEN Ttime.Quarter = '4' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q04
FROM TIME Ttime INNER JOIN Sales_Actuals Trev ON Ttime.Time_Code = Trev.Time_Code
WHERE
(Trev.Is_Active = 'Y') AND (Ttime.Year = '2007') AND
(Trev.Account_Code IS NOT NULL) AND (Trev.Sub_RSC_Code IS NOT NULL) AND (Trev.BD_Code IS NOT NULL)
GROUP BY Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code
fsqTarget AS
SELECT
Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code,
SUM(CASE WHEN Ttime.Quarter = '1' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q01,
SUM(CASE WHEN Ttime.Quarter = '2' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q02,
SUM(CASE WHEN Ttime.Quarter = '3' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q03,
SUM(CASE WHEN Ttime.Quarter = '4' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q04
FROM TIME Ttime INNER JOIN Sales_Target Trev ON Ttime.Time_Code = Trev.Time_Code
WHERE
(Trev.Is_Active = 'Y') AND (Ttime.Year = '2007') AND
(Trev.Account_Code IS NOT NULL) AND (Trev.Sub_RSC_Code IS NOT NULL) AND (Trev.BD_Code IS NOT NULL)
GROUP BY Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code
INSERT INTO Stage_Report_Revenue
(Revenue_Year, Forecast_Flag, Account_Code, Sub_RSC_Code, BD_Code,
Actuals_Q01, Actuals_Q02, Actuals_Q03, Actuals_Q04, Target_Q01)
SELECT
Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code,
Actual_Q01, Actual_Q02, Actual_Q03, Actual_Q04, Ttgt.Target_Q01
FROM
fsqAcutals Tact
LEFT JOIN fsqTarget Ttgt ON
(Tact.Year = Ttgt.Year) AND
(Tact.Account_Code = Ttgt.Account_Code) AND
(Tact.Sub_RSC_Code = Ttgt.Sub_RSC_Code) AND
(Tact.BD_Code = Ttgt.BD_Code);
COMMIT;

If I recall correctly it should be -
INSERT INTO Stage_Report_Revenue
(Revenue_Year, Forecast_Flag, Account_Code, Sub_RSC_Code, BD_Code,
Actuals_Q01, Actuals_Q02, Actuals_Q03, Actuals_Q04, Target_Q01)
WITH
fsqAcutals AS
SELECT
Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code,
SUM(CASE WHEN Ttime.Quarter = '1' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q01,
SUM(CASE WHEN Ttime.Quarter = '2' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q02,
SUM(CASE WHEN Ttime.Quarter = '3' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q03,
SUM(CASE WHEN Ttime.Quarter = '4' THEN COALESCE(Trev.Actuals_Amount, 0) ELSE 0 END) AS Actual_Q04
FROM TIME Ttime INNER JOIN Sales_Actuals Trev ON Ttime.Time_Code = Trev.Time_Code
WHERE
(Trev.Is_Active = 'Y') AND (Ttime.Year = '2007') AND
(Trev.Account_Code IS NOT NULL) AND (Trev.Sub_RSC_Code IS NOT NULL) AND (Trev.BD_Code IS NOT NULL)
GROUP BY Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code
fsqTarget AS
SELECT
Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code,
SUM(CASE WHEN Ttime.Quarter = '1' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q01,
SUM(CASE WHEN Ttime.Quarter = '2' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q02,
SUM(CASE WHEN Ttime.Quarter = '3' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q03,
SUM(CASE WHEN Ttime.Quarter = '4' THEN COALESCE(Trev.Target_Amount, 0) ELSE 0 END) AS Target_Q04
FROM TIME Ttime INNER JOIN Sales_Target Trev ON Ttime.Time_Code = Trev.Time_Code
WHERE
(Trev.Is_Active = 'Y') AND (Ttime.Year = '2007') AND
(Trev.Account_Code IS NOT NULL) AND (Trev.Sub_RSC_Code IS NOT NULL) AND (Trev.BD_Code IS NOT NULL)
GROUP BY Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code
SELECT
Ttime.Year, Trev.Account_Code, Trev.Sub_RSC_Code, Trev.BD_Code,
Actual_Q01, Actual_Q02, Actual_Q03, Actual_Q04, Ttgt.Target_Q01
FROM
fsqAcutals Tact
LEFT JOIN fsqTarget Ttgt ON
(Tact.Year = Ttgt.Year) AND
(Tact.Account_Code = Ttgt.Account_Code) AND
(Tact.Sub_RSC_Code = Ttgt.Sub_RSC_Code) AND
(Tact.BD_Code = Ttgt.BD_Code);Any way, does the select alon (without the insert) works?
Amiel Davis

Similar Messages

  • Cannot insert data from local database into remote database using subquery

    I have two oracle databases on different host.
    One is version 8i in Host A and the other is 9i in Host B.
    First, I try to create a dblink in 8i to 9i, but it fail.
    Then, I create a dblink in 9i to 8i, it success.
    I have already tried some select statement in 9i to view the table in 8i, which is success and that means the dblink is really work.
    The sql statement is like
    Select * from table1@hostA
    Then I have tried running some INSERT statement in 9i, which is copy data from table 1 in 8i to table 2 in 8i, both tables are in 8i. The sql statement is like
    Insert INTO table2@hostA(field1, field2)
    select field1, field2
    from table1@hostA
    This also success.
    Also, I have tried to use the following INSERT statement
    Insert INTO table2@hostA(field1, field2) values ('XXX', 'XXX)
    This also success.
    However, when I try to insert data from table in 9i to table in 8i using the following INSERT statement, it failed.
    Insert INTO table2@hostA(field1, field2)
    select field1, field2
    from table1
    The statement can execute, but all the rows inserted with every field value equal to NULL. I have tried to run the select part, it can return the data. But when I run the INSERT statement above, the value inserted into table2 is all equal to NULL.
    One more thing, I discover that the databases are using different NLS_CHARACTERSET
    9i is AL32UTF8
    8i is WE8ISO8859P1
    Is this relevant to my problem?
    Does anyone know the solution about this problem?
    Thanks!!

    How is the best way to do this ?How much are you planning to send?
    You can use COPY command. Ensure that you have valid database link between two databases.
    Available options are:
    create - creates a new table. errors out if the destination table exists.
    replace - drop the destination table and re-creates with data.
    insert - inserts data if the destination table exists.
    append– appends data into an existing table.
    use set arraysize 5000 -The arraysize specifies the number of rows that SQL*Plus will retrieve from the database at one time.
    copy from scott/tiger@ORCL to scott/tiger@ORCL92 create new_emp using select * from emp;

  • Subquery for inserting doesn't work in Oracle package

    I have experienced a very strange scenario while inserting data inside a Oracle package.
    I have two tables:
    - table "A"
    Columns: "ID", "Value1", "...."
    - table "A_Backup", which contains backup data for table A. It has one more column "BATCH_NUMBER" than table A
    Columns: "BATCH_NUMBER", "ID", "Value1", "...."
    I created following procedure in a package to backup data from table "A" to "A_Backup".
    procedure proc_backup (v_id in number) is
    declare
    v_batch_number varchar2(20);
    begin
    /** generate a batch number using system date */
    select 'BATCH' || to_char(sysdate, 'YYYYMMDDHH24MISS') into v_batch_number from dual;
    /** insert Batch_NUMBER + data from A into A_BACKUP */
    insert into A_BACKUP (select v_batch_number, id, value1, ... from A where A.id = v_id);
    end proc_backup;
    When I debug the procedure, it will not insert any data into A_BACKUP. Apparently, there are some data in table "A" meets criteria "A.id = v_id".
    The strange thing: If I create same procedure. But this time I didn't put procedure inside the package, insert query will work.
    Please help, I have spent a couple of days on this and never make it work. I also tried cursor, it doesn't work either. It seems Oracle package doesn't support "virtual table" (subquery in insert) or whatever you call it.

    Welcome to the forum!
    Whenever you post provide your 4 digit Oracle version (result of SELECT * FROM V$VERSION).
    I don't see any package or test code that calls the procedure or error messages or results from any procedure calls.
    You say you have a problem with a package but don't post the package version of the code you are having a problem with.
    How is anyone supposed to find a problem in code that you don't post? And when you post use \ tags as discussed in the FAQ.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Why should we create index on  the table after inserting data ?

    Please tell me the Reason, why should we create index on the table after inserting data .
    while we can also create index on the table before insertion of the data.

    The choice depends on a number of factors, the main being how many rows are going to be inserted in the table as a percentage of the existing rows, or the percentage growth.
    Creating index after a table has been populated works better when the tables are large or the inserts are large for the following reasons
    1. The sort and creation of index is more efficient when done in batch and written in bulk. So works faster.
    2. When the index is being written blocks get acquired as more data gets written. So, when a large number of rows get inserted in a table that already has an index , the index data blocks start splitting / chaining. This increases the "depth" of the inverted b-tree makes and that makes the index less efficient on I/O. Creating index after data has been inserted allows Orale to create optical block distribution/ reduce splitting / chaining
    3. If an index exists then it too is routed through the undo / redo processes. Thats an overhead which is avoided when you create index after populating the table.
    Regards

  • How many ways to insert data in the R/3 database

    hi
                 abappers,
    could u pls tell me for this question.

    Hi Basha,
    There are many ways to insert data in the R/3 database.
    Based on lot of factors, it will be decided which option to use.
    For example:
    1. Through a BDC ABAP program
    2. Through LSMW tool
    3. Through IDOCS using ALE or EDI
    4. Through ABAP program using insert/modify statement
    5. Manually creating entres in the table
    6. Using transaction code SM30 (Maintain table Views)
    Hope this helps.
    Please reward if useful.
    Thanks,
    Srinivasa

  • Error while insert data using execute immediate in dynamic table in oracle

    Error while insert data using execute immediate in dynamic table created in oracle 11g .
    first the dynamic nested table (op_sample) was created using the executed immediate...
    object is
    CREATE OR REPLACE TYPE ASI.sub_mark AS OBJECT (
    mark1 number,
    mark2 number
    t_sub_mark is a class of type sub_mark
    CREATE OR REPLACE TYPE ASI.t_sub_mark is table of sub_mark;
    create table sam1(id number,name varchar2(30));
    nested table is created below:
    begin
    EXECUTE IMMEDIATE ' create table '||op_sample||'
    (id number,name varchar2(30),subject_obj t_sub_mark) nested table subject_obj store as nest_tab return as value';
    end;
    now data from sam1 table and object (subject_obj) are inserted into the dynamic table
    declare
    subject_obj t_sub_mark;
    begin
    subject_obj:= t_sub_mark();
    EXECUTE IMMEDIATE 'insert into op_sample (select id,name,subject_obj from sam1) ';
    end;
    and got the below error:
    ORA-00904: "SUBJECT_OBJ": invalid identifier
    ORA-06512: at line 7
    then when we tried to insert the data into the dynam_table with the subject_marks object as null,we received the following error..
    execute immediate 'insert into '||dynam_table ||'
    (SELECT

    887684 wrote:
    ORA-00904: "SUBJECT_OBJ": invalid identifier
    ORA-06512: at line 7The problem is that your variable subject_obj is not in scope inside the dynamic SQL you are building. The SQL engine does not know your PL/SQL variable, so it tries to find a column named SUBJECT_OBJ in your SAM1 table.
    If you need to use dynamic SQL for this, then you must bind the variable. Something like this:
    EXECUTE IMMEDIATE 'insert into op_sample (select id,name,:bind_subject_obj from sam1) ' USING subject_obj;Alternatively you might figure out to use static SQL rather than dynamic SQL (if possible for your project.) In static SQL the PL/SQL engine binds the variables for you automatically.

  • Can we insert data directly in standard table

    hi
    how can we insert data in standard table directly.
    pls reply urgently.

    Hi sapna yes u can insert data to the SAP tables, as shown below...
    use the table mara in the place of m
    REPORT  ZTEST_INSERT.
    TABLES: <m>.
    DATA: wa_m TYPE <m>.
    wa_m-ernam = 'ZTEST'.
    insert into <m> VALUES wa_m.
    if sy-subrc = 0.
      WRITE / 'Records inserted Successfully'.
    ENDIF.
    reward if usefull,
    Vishnu. R
    Edited by: vishnu ramanathan on Sep 18, 2008 2:17 PM

  • Can i insert data in my database from a web intelligence report?

    if that's not possible,can I do it from a stored procedure based Universe or with a derived table? Does BusinessObjects in general allow me to execute statements to insert data in my database?

    Hi Erika,
    afaik it is not possible write back to database from web intelligence .
    from dashboards with web services you can achieve this.

  • Not able to insert data in database via forms

    Hi there. i have been trying to insert data into my database, which i am connected to remotely. i have written some sql code for a button.
    INSERT INTO address_repository VALUES('ADD'|| :address.address_id, :address.address_street_num,
    :address.address_street_name, :address.address_suburb, :address.address_postcode, :address.address_state, :address.address_country);
    where the above are the above are textfield items. my code compiles fine, and no errors are displayed on my form, but when i click the button, nothing happens, and the page is busy, and appears to be processing something, but does nothing.

    don't write
    INSERT INTO address_repository VALUES('ADD'|| :address.address_id, :address.address_street_num,
    :address.address_street_name, :address.address_suburb, :address.address_postcode, :address.address_state, :address.address_country);
    write
    INSERT INTO address_repository (address_id, ....., ....., .....
    VALUES('ADD'|| :address.address_id, :address.address_street_num,
    :address.address_street_name, :address.address_suburb, :address.address_postcode, :address.address_state, :address.address_country);
    if the same error is still active, then we look further, ok?

  • Refresh LOV after inserting data in correspondent table

    Hi all,
    I am using Jdeveloper 11.1.1.2 and ADFBC.
    I have a page with a table (tableA) with some fields. One of them has a LOV associated to another table(tableB). I have a button (button1) that shows a popup(popup1) and opens a new task flow with a fragment to insert data in the tableB.
    After inserting/modifying rows in tableB and after pressing commit (property: end-transaction) or rollback buttons (property: end-transaction and restore save point), LOV does not refresh.
    I have tried to set the property partial triggers of the LOV to button1 or popup1 and it does not work.
    How can I solve it?
    Thank you.
    Andrea

    How about VO for table B ? Did you refresh that ? I didn't use the same VO for table & LOV.
    so in "List data Source" -- VO -- I am using "SQL statement" and statement includes fields from Table B and Table A.
    VO for LDS using "Rows populated by a SQL query with read-only access". I didn't use "Updateable Access through Entity Objects".
    Sample of VO of LDS:
    select A.model_number modelNumber, a.part_number partNumber, b.MODEM_TYPE ModemType
    from table A a,Table B b
    where a.PART_NUMBER_ID = b.PART_NUMBER_ID
    and a.part_number_id =:partID
    Edited by: albertpi on Mar 16, 2010 7:26 AM

  • Inserting data in remote database with remote procedure

    Hi,
    I have a problem concerning inserting data in a remote database (10g) by using a remote procedure.
    I call the procedure from a 11g database just by doing:
    begin
    getlocalfilecontent@remoteserv(param)
    commit;
    end;
    And it wouldn't insert the data into the remote table.
    While when I execute the same procedure locally on that remote database (10g)
    It does insert the data I want.
    What could be causing the problem here, am I overseeing something?
    Edited by: user7634309 on 14-Aug-2009 02:14

    Already thanks for the fast replies
    The dblink works perfectly. I can select data etc with this dblink.
    The procedure on the remote database is the same.
    One other thing, when I execute the remote procedure on local database 2nd time I get the
    ORA-ERR: ORA-02041: client database did not begin a transaction.
    This could be fixed by doing a commit or rollback but still no inserting in remote table.
    So to sum it up:
    What doesn't work:(no inserting)
    On 11g server that calls remotely the procedure
    begin
    getlocalfilecontent@remoteserv(parameter);
    commit;
    end;
    What works(does insert)
    On 10g server calling the procedure locally
    begin
    getlocalfilecontent(param);
    commit;
    end;
    So everything is the same except on the 11g server I add the dblink spec behiind the procedurecall

  • Inserting data to a . SDF database (SQL Server Compact Edition connection)

    Hi, all.
    I'm working on a project that worked fine while connecting to SQL Server 2008. I used a connection string like this:
    Driver={SQL Server Native Client 10.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
    Now, I've replaced all database connections in my project to work with SQL Server Compact Edition, and my connection string is something like this:
    Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=myDir\myDataBase.sdf; 
    After changing the type of connection, I'm getting an error on "DB Tools Insert Data.vi". This is what I get:
    Error -2147217872 occurred at NI_Database_API.lvlib:Cmd Execute.vi->NI_Database_API.lvlib: DB Tools Insert Data.vi->Main.vi
    Possible reason(s):ADO Error: 0x80040E30 Exception occured in Microsoft SQL Server Compact OLE DB Provider: The given type name was unrecognized. [,,,,,] in NI_Database_API.lvlib:Rec Create - Command.vi->NI_Database_API.lvlib:Cmd Execute.vi->NI_Database_API.lvlib: DB Tools Insert Data.vi->Main.vi
    This is a simplified block diagram (as I said, it worked perfectly while using SQL Server 2008 connection):
    Could you please help me?
    Thanks in advance,
    Francisco

    Hi, Steve. Thanks for your answers.
    I worked around this problem by using directly a SQL query instead of this function (you were right, using DBTools Execute Query VI). But now I'm getting problems with other functions, when inserting data into the DB including NULL values.
    I make a SQL query like this using DBTools Execute Query VI:
    INSERT INTO Table1 (Column1, Column2, Column3, Column4, Column5, Column6) VALUES ('Value1', 'Value2', 'Value3', 'NULL', 'Value5', 'Value6') 
    and get the following error:
    ADO Error: 0x80040E07Exception occured in Microsoft SQL Server Compact OLE DB Provider: A literal value in the command could not be converted to the correct type due to a reason other than data overflow. [,,,,,] in NI_Database_API.lvlib:Conn Execute.vi->INSERT (new) restricción (DBCT).vi->Restricciones a Base de Datos (DBCT).vi->Definir - editar restricciones.vi->Main.vi
    The same SQL query worked perfectly when using the SQL Server 2008 connection, so it's not a database structure issue.
    Could somebody please help me?
    Regards,
    Francisco

  • How to insert data into a table only when data has changed its value (when compared to the previous inserted value)

    I wish to insert data into a table only when the value of the inserted data has changed. Thus, in a time series, if the value of the data at time, t-1, is 206 then if the data to be inserted at time t is 206, then it is skipped (not entered).
    If the value of the data at time t+1 is 206, it is skipped also; until the value changes, so if the value at t+1 was 205, then that would be inserted, and if at time t+2 the data is 206, it would be inserted too.
    What is the best way to do it without increasing overheads?

    This view works:
    SELECT
    i.IDNO,i.[Date],i.[Level]
    FROM
    mytable i
    INNER
    JOIN mytable
    d
    ON
    d.IDNO
    = i.IDNO-1
    WHERE
    i.[Level]
    <> d.[Level]
    on this mytable below.  A trigger could be quite useful here although I am cautious using them. However I wish to avoid the overhead by not having a temp table (which could be sizable).  mytable below
    should give 3 lines. The IDNO is an identity column.
    IDNO
    Item
    Date
    Level
    1
    X24
    12/23/13 10:41
    22996
    2
    X24
    12/23/13 10:41
    22996
    3
    X24
    12/23/13 9:21
    23256
    4
    X24
    12/23/13 9:21
    23256
    5
    X24
    12/23/13 9:22
    23256
    6
    X24
    12/23/13 9:22
    23256
    7
    X24
    12/23/13 9:22
    22916

  • How do you access a web service to insert data into table?

    Ok I have a simple html/jsp survey form to host on our web site. Because of firewall issues I cannot do a direct connect to the database and insert the data from the form. So I need to take the data from the survey form, and pass it to an existing web service, or create a web service that will insert this information for me into the database table?
    Thanks again.
    orozco

    Thanks for the reply shanu. Well, because of firewall issues, as a work around we use a web service to insert data into the database table. Since the Web Service already exist, we just thought it would be easy enough just to pass the parameters to it, and have it do the insert. So really all I need is just to communicate with the already existing web service. I will be honest though I have not really worked with web services before.
    orozcom

  • Error using DB Tools Insert Data.vi

    Hi
    I am trying to insert data into a Paradox database and I am getting the following error:
    "Error -2147467259 occurred at Conn Execute.vi->DB Tools Create Table.vi->DB Tools Create Table If Not Exist.vi->DB Tools Insert Data.vi->Header Infromation 20061019 two inserts.vi
    Possible Reasons: Exception occured in Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Paradox Driver] The Microsoft Jet database engine cannot open the file ''.  It is already opened exclusively by another user, or you need permission to view its data. in Conn Execute.vi->DB Tools Create Table.vi->DB Tools Create Table If Not Exist.vi->DB Tools Insert Data.vi->Header Infromation 20061019 two inserts.vi"
    Right now the vi is set up to create a table if one does not exist.
    Any Suggestions on this .

    Hello Shani,
    Have you had a chance to look over the other questions Crystal asked above?  Those answers will help us to better help you.  I also found the following discussion forum - I know they are using a different database, but I think you can tweak the solution so it works for you too. 
    Have a fantastic Wednesday!
    Janell R | Applications Engineer

Maybe you are looking for