Sql statement for join tables

hi there..i need ur helps
Here are my tables n column name for each tables
a) rep_arrngt
Name
REP_ARRNGT_ID
REP_ARRNGT_DESC
REP_ARRNGT_TYPE
ACCT_CAT
DEF_INDUSTRY_CODE
MEDIA_ID
LANGUAGE_ID
CURRENCY
PAGE_ID
b) bci_rep_arrng
Name
REP_ARRNGT_ID
BCI
SUB_SUM_CODE
DP_SUB_SUM_CODE
c) acct_rep_arrngt_link
Name
ACCT_NO
REP_ARRNGT_ID
DOC_TYPE
LAST_BILL_DATE
BILL_FREQUENCY
SCALING
EFF_FROM_DATE
EFF_TO_DATE
MEDIA_ID
Actually, i want to get the unique value for sub_sum_code according to the bci..
i already use join tables n here is my sql statement :
SELECT T1.SUB_SUM_CODE,T1.BCI,T1.REP_ARRNGT_ID,T2.REP_ARRNGT_ID,T3.REP_ARRNGT_ID FROM BCI_REP_ARRNG T1, REP_ARRNGT T2, ACCT_REP_ARRNGT_LINK T3 WHERE T1.REP_ARRNGT_ID=T2.REP_ARRNGT_ID AND T2.REP_ARRNGT_ID=T3.REP_ARRNGT_ID AND BCI='TTA00F06'
n my results is :
SUB_SUM_CODE BCI REP_ARRNGT_ID
TBGSR TTA00F06 R1
TBGSR TTA00F06 R1
TBGSR TTA00F06 R1
TBGSR TTA00F06 R1
I get the repeated results for sub_sum_code..
so, what i need to do if i want only 1 row results like this :
[u]SUB_SUM_CODE BCI REP_ARRNGT_ID
TBGSR TTA00F06 R1
i try to use group by, but i get the error..plz help me

If you only want "to get the unique value for sub_sum_code according to the bci" then why are you joining the tables in the first place? Not knowing PKs etc you could just use DISTINCT in your select statement.
SELECT DISTINCT T1.SUB_SUM_CODE,
                T1.BCI
FROM BCI_REP_ARRNG T1
AND BCI='TTA00F06'

Similar Messages

  • Sql statement for a table name with a space in between

    Hi,
    I just noticed that one of my tables for Access is consisted of two word. It is called "CURRENT CPL". How would I put this table name into an sql statement. When I did what I normally do, it only reads the CURRENT and thinks that's the table name.
    Thanks
    Feng

    I just noticed that one of my tables for Access is
    consisted of two word. It is called "CURRENT CPL".
    How would I put this table name into an sql
    statement. When I did what I normally do, it only
    reads the CURRENT and thinks that's the table name.That is called a quoted identifier. The SQL (not java) for this would look like this....
    select "my field" from "CURRENT CPL"
    The double quote is the correct character to use. Note that quoted identifiers are case sensitive, normal SQL is not.

  • SQL query for join table and multiple values

    Trying to join two tables , Emphours and EmpStatus to get
    result which gives each emplyees hour 
    worked each day
    in past  say 1 year in what status. I need result similar to table 3 , Hours Can also be grouped per week
    all I need Is Each employees hours in each week and his status and position at that time if possible
    any help will be highly appreciated. Thank you 
    note: payday is every other Friday- week runs from Saturday through Friday
    EmpStatus Table tracks when employees status changed
    EmpHours
    employee
    workday
    payday
    hours
    position
    101
    1/1/2014
    1/3/2014
    8
    assistant
    101
    1/3/2014
    1/3/2014
    8
    assistant
    101
    1/4/2014
    1/17/2014
    8
    assistant
    101
    1/5/2014
    1/17/2014
    8
    assistant
    101
    1/7/2014
    1/17/2014
    8
    assistant
    101
    1/8/2014
    1/17/2014
    8
    assistant
    101
    1/9/2014
    1/17/2014
    8
    assistant
    101
    1/11/2014
    1/17/2014
    8
    assistant
    101
    1/13/2014
    1/17/2014
    8
    assistant
    101
    1/14/2014
    1/17/2014
    8
    assistant
    101
    1/18/2014
    2/14/2014
    8
    assistant
    102
    1/1/2014
    1/3/2014
    7
    manager
    102
    1/25/2014
    1/31/2014
    7
    manager
    102
    1/26/2014
    1/31/2014
    7
    manager
    102
    1/28/2014
    1/31/2014
    7
    manager
    102
    1/31/2014
    1/31/2014
    7
    manager
    103
    1/1/2014
    1/3/2014
    5
    intern
    103
    1/31/2014
    1/31/2014
    6
    intern
    104
    1/14/2014
    1/17/2014
    5
    supervisor
    104
    1/30/2014
    1/31/2014
    6
    supervisor
    EmpStatus
    employee
    start_date
    status
    101
    1/1/2014
    parttime
    101
    1/18/2014
    fulltime
    102
    1/1/2014
    seasonal
    102
    1/18/2014
    fulltime
    103
    1/1/2014
    partime
    103
    1/18/2014
    fulltime
    104
    1/4/2014
    parttime
    104
    1/18/2014
    fulltime
    Table 3
    employee
    status
    hours
    position
    workday
    weekend
    payday
    101
    parttime
    8
    assistant
    1/1/2014
    1/3/2014
    1/3/2014
    101
    parttime
    8
    assistant
    1/3/2014
    1/3/2014
    1/3/2014
    101
    parttime
    8
    assistant
    1/4/2014
    1/10/2014
    1/17/2014
    101
    parttime
    8
    assistant
    1/5/2014
    1/10/2014
    1/17/2014
    101
    parttime
    8
    assistant
    1/7/2014
    1/10/2014
    1/17/2014
    101
    parttime
    8
    assistant
    1/8/2014
    1/10/2014
    1/17/2014
    101
    parttime
    8
    assistant
    1/9/2014
    1/10/2014
    1/17/2014
    101
    parttime
    8
    assistant
    1/11/2014
    1/17/2014
    1/17/2014
    101
    parttime
    8
    assistant
    1/13/2014
    1/17/2014
    1/17/2014
    101
    parttime
    8
    assistant
    1/14/2014
    1/17/2014
    1/17/2014
    101
    fulltime
    8
    assistant
    1/18/2014
    1/24/2014
    2/14/2014
    102
    seasonal
    7
    manager
    1/1/2014
    1/3/2014
    1/3/2014
    102
    fulltime
    7
    manager
    1/25/2014
    1/25/2014
    2/14/2014
    102
    fulltime
    7
    manager
    1/26/2014
    1/26/2014
    2/14/2014
    102
    fulltime
    7
    manager
    1/28/2014
    1/28/2014
    2/14/2014
    102
    fulltime
    7
    manager
    1/31/2014
    1/31/2014
    2/14/2014
    103
    parttime
    5
    intern
    1/1/2014
    1/3/2014
    1/3/2014
    103
    fulltime
    6
    intern
    1/31/2014
    1/31/2014
    2/14/2014
    104
    parttime
    5
    supervisor
    1/14/2014
    1/17/2014
    1/17/2014
    104
    fulltime
    6
    supervisor
    1/30/2014
    1/31/2014
    1/31/2014

    Hello David,
    Try this query
    set dateformat mdy;
    declare @EmpHours table
    (Employee int,workday date,payday date,hours int,position varchar(50));
    insert into @EmpHours values
    (101,'1/1/2014','1/3/2014',8,'assistant'),
    (101,'1/3/2014','1/3/2014',8,'assistant'),
    (101,'1/4/2014','1/17/2014',8,'assistant'),
    (101,'1/5/2014','1/17/2014',8,'assistant'),
    (101,'1/7/2014','1/17/2014',8,'assistant'),
    (101,'1/8/2014','1/17/2014',8,'assistant'),
    (101,'1/9/2014','1/17/2014',8,'assistant'),
    (101,'1/11/2014','1/17/2014',8,'assistant'),
    (101,'1/13/2014','1/17/2014',8,'assistant'),
    (101,'1/14/2014','1/17/2014',8,'assistant'),
    (101,'1/18/2014','2/14/2014',8,'assistant'),
    (102,'1/1/2014','1/3/2014',7,'manager'),
    (102,'1/25/2014','1/31/2014',7,'manager'),
    (102,'1/26/2014','1/31/2014',7,'manager'),
    (102,'1/28/2014','1/31/2014',7,'manager'),
    (102,'1/31/2014','1/31/2014',7,'manager'),
    (103,'1/1/2014','1/3/2014',5,'intern'),
    (103,'1/31/2014','1/31/2014',6,'intern'),
    (104,'1/14/2014','1/17/2014',5,'supervisor'),
    (104,'1/30/2014','1/31/2014',6,'supervisor');
    --select * from @EmpHours
    declare @EmpStatus table
    (employee int,start_date date,status varchar(20));
    insert into @EmpStatus values
    (101,'1/1/2014','parttime'),
    (101,'1/18/2014','fulltime'),
    (102,'1/1/2014','seasonal'),
    (102,'1/18/2014','fulltime'),
    (103,'1/1/2014','partime'),
    (103,'1/18/2014','fulltime'),
    (104,'1/4/2014','parttime'),
    (104,'1/18/2014','fulltime');
    WITH C AS
    SELECT es.employee,es.start_date, es.status, ROW_NUMBER() OVER(partition by employee ORDER BY start_date) AS rownum
    FROM @EmpStatus ES
    CTE_RANGES as(
    SELECT cur.employee,Cur.start_date start_range, cur.status,case when nxt.start_date is null then '2099-12-31' else dateadd(d,-1,Nxt.start_date) end AS end_range
    FROM C AS Cur
    left JOIN C AS Nxt
    ON Nxt.rownum = Cur.rownum + 1 and cur.employee=nxt.employee)
    select eh.*,es.status from @EmpHours EH join CTE_RANGES Es on EH.Employee =es.employee and EH.workday between es.start_range and es.end_range
    --where es.employee=101
    You will need a calender table too which can be joined to the output of the above query to get the weekend dates.
    You can find the T-SQL code to generate the calender here
    http://stackoverflow.com/questions/19191577/t-sql-function-to-generate-calendar-table
    and posting the questions with necessary DDL , DML (like I have posted) would help us a lot. 
    Satheesh
    My Blog

  • Forming generic sql query   for joining multiple sap tables in ABAp

    Hi,
    I am new to this abap field ,facing an issue onsap-jco project . I have used RFC_READ_TABLE  FM ,Customized this FM but facing an issue how to write generic  open SQl select statement  for joining multiple tables  using RFC_READ_TABLE .Kindly help on this issue.
    Thanks.

    something like this? If your tuples are not single columns, then you'll have to use dynamic sql to achieve the same result.with
    table_1 as
    (select '|Xyz|Abc|Def|' tuple from dual
    table_2 as
    (select '|Data1|Data21|Data31|Data41|Data51|' tuple from dual union all
    select '|Data2|Data22|Data32|Data42|Data52|' tuple from dual union all
    select '|Data3|Data23|Data33|Data43|Data53|' tuple from dual union all
    select '|Data4|Data24|Data34|Data44|Data54|' tuple from dual union all
    select '|Data5|Data25|Data35|Data45|Data55|' tuple from dual
    select case the_row when 1
                        then tuple
                        else '|---|---|' || substr(tuple,instr(tuple,'|',1,3) + 1)
           end tuple
      from (select substr(a.tuple,instr(a.tuple,'|',:one_one),instr(a.tuple,'|',:one_one + 1)) ||
                   substr(a.tuple,instr(a.tuple,'|',1,:one_two) + 1,instr(a.tuple,'|',1,:one_two + 1) - instr(a.tuple,'|',1,:one_two)) ||
                   substr(b.tuple,instr(b.tuple,'|',1,:two_one) + 1,instr(b.tuple,'|',1,:two_one + 1) - instr(b.tuple,'|',1,:two_one)) ||
                   substr(b.tuple,instr(b.tuple,'|',1,:two_two) + 1,instr(b.tuple,'|',1,:two_two + 1) - instr(b.tuple,'|',1,:two_two)) tuple,
                   rownum the_row
              from table_1 a,table_2 b
    order by the_rowRegards
    Etbin
    Message was edited by:Etbin
    user596003

  • Sql statement for retrieving the last update time of a table

    Hello all,
    Can somebody give me an example of sql statement for retrieving the last update time of an oracle table.
    Thank you
    Il

    Thanks for the fast replies. It works great when I test it as a sql statement but when trying to populate a datalist with it it raises the following exception:
    Exception Details: System.ArgumentException: SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN is neither a DataColumn nor a DataRelation for table DefaultView
    Part of the Datalist Code:
    ItemTemplate>
    Line 12:             SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN)):
    Line 13:             <asp:Label ID="SCN_TO_TIMESTAMP_MAX_ORA_ROWSCN__Label" runat="server" Text='<%# Eval("[SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN))]") %>'>
    Line 14:             </asp:Label><br/>
    Line 15:             <br/>
    {code}
    Why is this happening? Any ideas?
    Il                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Problem with SQL Statement for Result Filtering

    Dear Visual Composer Experts,
    Here is another Question from me: I have a SQL Query that is working as the data service
    Select AB.AgingBandID, AB.AgingBand,
    Sum(Case when priority='Emergency' then '1' Else 0 End) as [Emergency],
    Sum(Case when priority='Ugent' then '1' Else 0 End) as Ugent,
    Sum(Case when priority='High' then '1' Else 0 End) as High,
    Sum(Case when priority='Medium' then '1' Else 0 End) as Medium,
    Sum(Case when priority='Low' then '1' Else 0 End) as Low
    from DimAgingBand AB left outer join
    (Select AgingBandID , priority , yeardesc
    from vNotifications where YearDesc = (select year(getdate())-1)) as vN
    on AB.AgingBandID=vN.AgingBandID
    where AB.AgingBandID<>'1'  
    Group by  AB.AgingBandID, AB.AgingBand
    Order by AB.AgingBandID
    That would return me a table as in the following:
         Agingband     E     U     H     M     L
         < 1week     0     0     0     0     1
         1 - 2 weeks     0     0     0     0     0
         2 - 4weeks     0     0     0     0     1
    > 1month     8     2     1     1     6
    Now that I would like to add some parameters to filter the result, so I modify the query and put it in the SQL Statement input port of the same data service. The query is like this:
         "Select AB.AgingBandID, AB.AgingBand,Sum(Case when priority='Emergency' then '1' Else 0 End) as [Emergency],Sum(Case when priority='Ugent' then '1' Else 0 End) as Ugent,Sum(Case when priority='High' then '1' Else 0 End) as High,Sum(Case when priority='Medium' then '1' Else 0 End) as Medium,Sum(Case when priority='Low' then '1' Else 0 End) as Low from DimAgingBand AB left outer join (Select AgingBandID , priority , yeardesc from vNotifications where YearDesc like '2009%' and Branch like '" & if(STORE@selectedBranch=='ALL', '%', STORE@selectedBranch) & "' and MainWorkCentre like '%') as vN on AB.AgingBandID=vN.AgingBandID where AB.AgingBandID<>'1' Group by AB.AgingBandID, AB.AgingBand Order by AB.AgingBandID"
    However this input port query keeps giving me error as NullPointerException. I have actually specified a condition where the query will run if only STORE@selectedBranch != u2018u2019.
    I have other filtering queries working but they are not as complicated query as this one. Could it be possible that query in the input port cannot handle left outer join?
    Could it be anything else?
    Help is very much appreciated.
    Thanks & Regard,
    Sarah

    Hi,
    Thank you very much for your replys. I've tested if the dynamic value of the condition is integer, it's OK
    But if the ClassID type is not integer, it's string, I write  a SQL Statement like:
    "Select DBADMIN.Class.ClassName from DBADMIN.Class where DBADMIN.Class.ClassID = '1' "
    or with dynamic condition:
    "Select DBADMIN.Class.ClassName from DBADMIN.Class where DBADMIN.Class.ClassID = '"&@ClassID&"'"
    or I write the SQL Statement for insert/update/delete data,
    I always have errors.
    I've tested if the dynamic value of the condition is integer, it's OK
    Do you know this problem ?
    Thank you very much & kindly regards,
    Tweety

  • How to write Sql statement for M:M relationship set?

    If theres two Entity sets "Department" and "Employee".....
    Department Entity contains this attribute dno,dname,address etc and
    "Employee" entity contain attribute like Empno,Empname,date,time etc
    and theres Many to Many relationship between Employee and Deapartment Entity set,i.e.each employee can work in any department, and each department has at least one employee (Many to Many relationship)
    What should be the SQL statement to create table for Many to Many relationship sets for Employee and Department set..
    Any helps will be appreciated.
    Femi

    create 3 tables: employee, department and empdept
    in the empdept table you reference foreign keys from the 2 other tables. so if my employee id was 42 and I work for departments 1,2 and 3, this table would look like this:
    emp_fk | dept_fk
    42           1
    42           2
    42           3

  • Need SQL statement for this logic....

    Hi,
    I want a SQL statement for updating the following changed last number .
    Cuurently its:
    SELECT * FROM TEST;
    LAST NUMBER     CHANGED LAST NUMBER
    123518          
    12355265     
    123674659     
    9087648970     
    After updating with the required SQL statement table should look like
    LAST NUMBER     CHANGED LAST NUMBER
    123518          0000123518
    12355265     0012355265
    123674659     0123674659
    9087648970     9087648970
    the last number should be appended with ZEROs and the length of changed last number should be 10 always. Hope its clear.
    Appreciate your help.
    Thanks in advance
    Devender

    select last_number, lpad(to_char(last_number), 10 , '0') FROM test

  • SQL statement for calculating performance targets

    Hi
    I have taken of the admin of a database which stores project goals and scores. I have to develop a way to calculate how well all projects meet these scores. The table concerned is called goal and looks like this:
    goal
    goal_name
    project_code
    current_value
    good_value
    bad_value
    This can be for many different goals, for example if a project wants to get up a goal of having no more than 5 bugs in the project. I can also set a bad value, say 20, so if any projects have 20 or more bugs, triggers or alerts can be sent So I can enter into this table. The reason for putting 5 for good not 20 is because these scores are to be realistic.
    project_code = foo
    goal_name = software bugs
    good_value = 5
    bad_value = 20
    However, some goals may have the values switched and be in a much higher range, or may even be a percentage. For example one for number of sales could be
    project_code = foo
    goal_name = software sales
    good_value = 200
    bad_value = 50
    or project delay
    project_code = foo
    goal_name = sproject delay
    good_value = 0%
    bad_value = 50%
    i am trying to develop a SQL statement so I can get a % score of how well a goal is performing, so I can see
    What is the goal % for all foo goals
    Or what is the goal % for goal 'software sales'
    And also more importantly, how well are the goals doing globally.
    The requirement for doing this is using a single SQL statement, well, one SQL statement for the requirements I listed above, so for example the semantics are
    SELECT average(goal perforance) WHERE .... project = foo .... or goal = software sales... etc
    I am having trouble doing this, I have been banging my head against mydesk all day. the biggest thing is thowing me off is that the good value can be higher or lower than the bad value, and I am having trouble visualizing how to but this conditional statement in SQL
    One more thing, the percentage returned should never be more than 0% or 100%.
    If anyone has any ideas or pointers, please help me out,
    Thanks for your time,
    Message was edited by:
    user473327

    I am having trouble doing this, I have been banging
    my head against mydesk all day. the biggest thing is
    thowing me off is that the good value can be higher
    or lower than the bad value, and I am having trouble
    visualizing how to but this conditional statement in
    SQLI haven't looked at your requirements in detail cos I don't have time for such cumbersome tasks. However, you could have two UNION'd select statements, one which caters for the good > bad and one which caters for the good < bad. Also and alternative would be the use of DECODE or CASE statements in your select which are good for switching things around based on conditions.
    ;)

  • Sql statement for network version of JavaDB database

    Hi,
    Could somebody please tell is it a must to have double quote for field name inside the SQL statement for JavaDB
    I am developing the project using netbean 5.5.1 IDE
    integrating netwok version of JavaDB.
    example
    select "field1","field2" from table1
    instead of the simple sql like
    select field1,field2 from table1
    I find that simple select sql query could not work without having the double quotation eclosed for any of the fieldname called by the SQL statement.
    I am not sure is it only unique to netbeans IDE environment , in principle simple plain query should work , this is however not for netbeans IDE, wht is the reason behind?
    Thanks

    The Quotes around the fieldnames are normally only needed if the field is the same as a reserved word, or has a non-standard character in the field. Look at the JavaDB/Derby manual (specifically the one with the SQL reference) to find out what the reserved words and special characters are, then redefine your table.

  • SQL Statement for Composite Primary key

    Hi,
    I need a SQL statement for finding the Distinct values for combination of 2 Columns( Composite Primary key). I dont want to use Data Dictionary views.
    Table A
    c1 c2
    1 1
    1 2
    2 3
    Now for the above table combination of c1 and c2 is unique.
    Please advice.
    Cheers
    Ramkannan.A

    This?
    PRAZY@solarc> with tableA as
      2  (select 1 col1,2 col2 from dual
      3  union all
      4  select 1,1 from dual
      5  union all
      6  select 1,2 from dual
      7  union all
      8  select 2,3 from dual
      9  union all
    10  select 1,2 from dual
    11  union all
    12  select 2,3 from dual
    13  )
    14  select distinct col1, col2 from TableA
    15  /
          COL1       COL2
             2          3
             1          2
             1          1Regards,
    Prazy

  • Execute the sql statement stored in table

    friends i m in complication that, how can i execute sql statement which is stored in database table.
    how can i fetch that particular statement from table and make it execute!!!
    Thanks in advance

    I think we should stop at this point and have a little think.
    My understanding is that your plan is to do the following
    -Execute a Dynamic SQL statement which is constructed from values in several columns in a table (Bad idea to begin with)
    -The result of the execution is then loaded into PL/SQL.....not sure how you are handling different column lists but I'll assume you are not having to convert every column value to a VARCHAR2
    -The values returned are then used in an INSERT statement
    So basically your goal here is to take some data from one table and insert it into another table by moving data from SQL to PL/SQL then back to SQL in an elaborate, dynamic way. This is a costly waste of resources and will not scale (this is before we get to the problem of having to read the SQL statement from a table in the first place)
    I would recommend looking the INSERT SELECT statement here: http://psoug.org/reference/insert.html
    This will get the job done in one go. If you really do need it to be dynamic (which you don't, trust me) then you can still store this statement in your "dynamic SQL" table and execute it using EXECUTE IMMEDIATE although I can say for sure that you're going to run into more problems down the line with this architecture, it won't scale very well, and you'll probably waste at least *1* day of your time every week trying to fix a bug caused by the removal of a table, change to a column name etc.......
    However if you believe that storing SQL in a table is the best solution to your problem then go ahead.....at least when you find out that it isn't you'll have learned a valuable lesson.

  • Sql statement in a table not accepting variable

    I have the following problem on 10.1.0.3.0 with varialbe in an execute immediate statement
    here is the code that I am using
    declare
    remote_data_link varchar2(25) := 'UDE_DATATRANSFER_LINK';
    FROM_SCHEMA VARCHAR2(40) := 'UDE_OLTP';
    l_last_process_date date := to_date(to_char(sysdate,'mm-dd-yyyy hh:mi:ss'),'mm-dd-yyyy hh:mi:ss') - 1;
    stmt varchar2(4000) := 'MERGE into applicant_adverseaction_info theTarget USING (select * from '||FROM_SCHEMA||'.applicant_adverseaction_info@'||remote_data_link||' where last_activity > :l_last_process_date ) theSource ON(theTarget.applicant_id = theSource.applicant_id) WHEN MATCHED THEN UPDATE SET theTarget.cb_used = theSource.cb_used, theTarget.cb_address = theSource.cb_address, theTarget.scoredmodel_id = theSource.scoredmodel_id, theTarget.last_activity = theSource.last_activity WHEN NOT MATCHED THEN INSERT(CB_USED, CB_ADDRESS, SCOREDMODEL_ID, APPLICANT_ID, LAST_ACTIVITY) values(theSource.cb_used, theSource.cb_address, theSource.scoredmodel_id, theSource.applicant_id, theSource.last_activity)';
    stmt2 varchar2(4000) := 'MERGE into edm_application theTarget USING (select * from '||from_schema||'.edm_application@'||remote_data_link||' where last_activity > :l_last_process_date) theSource ON (theTarget.edm_appl_id = theSource.edm_appl_id) WHEN MATCHED THEN UPDATE SET theTarget.APP_REF_KEY = theSource.APP_REF_KEY, theTarget.IMPORT_REF_KEY = theSource.IMPORT_REF_KEY, theTarget.LAST_ACTIVITY = theSource.LAST_ACTIVITY WHEN NOT MATCHED THEN INSERT (EDM_APPL_ID, APP_REF_KEY, IMPORT_REF_KEY, LAST_ACTIVITY) values(theSource.EDM_APPL_ID, theSource.APP_REF_KEY, theSource.IMPORT_REF_KEY, theSource.LAST_ACTIVITY)';
    v_error varchar2(4000);
    T_MERGE VARCHAR2(4000);
    stmt3 varchar2(4000);
    BEGIN
    select merge_sql
    INTO T_MERGE
    from transfertables
    where table_name= 'edm_application';
    remote_data_link:= 'UDE_DATATRANSFER_LINK';
    FROM_SCHEMA := 'UDE_OLTP';
    --DBMS_OUTPUT.PUT_LINE(SUBSTR(stmt2,1,200));
    --STMT2 := T_MERGE;
    dbms_output.put_line(from_schema||' '||remote_data_link||' '||l_last_process_date);
    EXECUTE IMMEDIATE stmt2 using l_last_process_date;
    --execute immediate stmt3 ;
    dbms_output.put_line(from_schema||' '||remote_data_link||' '||l_last_process_date);
    dbms_output.put_line(substr(stmt2,1,200));
    commit;
    EXCEPTION
    WHEN OTHERS THEN
    V_ERROR := SQLCODE||' '||SQLERRM;
    v_ERROR := V_ERROR ||' '||SUBSTR(stmt2,1,200);
    DBMS_OUTPUT.PUT_LINE(V_ERROR);
    --dbms_output.put_line(substr(stmt2,1,200));
    END;
    This works perfectly
    but if I change it to get the same statement in a db table
    declare
    remote_data_link varchar2(25) := 'UDE_DATATRANSFER_LINK';
    FROM_SCHEMA VARCHAR2(40) := 'UDE_OLTP';
    l_last_process_date date := to_date(to_char(sysdate,'mm-dd-yyyy hh:mi:ss'),'mm-dd-yyyy hh:mi:ss') - 1;
    stmt varchar2(4000) := 'MERGE into applicant_adverseaction_info theTarget USING (select * from '||FROM_SCHEMA||'.applicant_adverseaction_info@'||remote_data_link||' where last_activity > :l_last_process_date ) theSource ON(theTarget.applicant_id = theSource.applicant_id) WHEN MATCHED THEN UPDATE SET theTarget.cb_used = theSource.cb_used, theTarget.cb_address = theSource.cb_address, theTarget.scoredmodel_id = theSource.scoredmodel_id, theTarget.last_activity = theSource.last_activity WHEN NOT MATCHED THEN INSERT(CB_USED, CB_ADDRESS, SCOREDMODEL_ID, APPLICANT_ID, LAST_ACTIVITY) values(theSource.cb_used, theSource.cb_address, theSource.scoredmodel_id, theSource.applicant_id, theSource.last_activity)';
    stmt2 varchar2(4000) := 'MERGE into edm_application theTarget USING (select * from '||from_schema||'.edm_application@'||remote_data_link||' where last_activity > :l_last_process_date) theSource ON (theTarget.edm_appl_id = theSource.edm_appl_id) WHEN MATCHED THEN UPDATE SET theTarget.APP_REF_KEY = theSource.APP_REF_KEY, theTarget.IMPORT_REF_KEY = theSource.IMPORT_REF_KEY, theTarget.LAST_ACTIVITY = theSource.LAST_ACTIVITY WHEN NOT MATCHED THEN INSERT (EDM_APPL_ID, APP_REF_KEY, IMPORT_REF_KEY, LAST_ACTIVITY) values(theSource.EDM_APPL_ID, theSource.APP_REF_KEY, theSource.IMPORT_REF_KEY, theSource.LAST_ACTIVITY)';
    v_error varchar2(4000);
    T_MERGE VARCHAR2(4000);
    stmt3 varchar2(4000);
    BEGIN
    select merge_sql
    INTO T_MERGE
    from transfertables
    where table_name= 'edm_application';
    remote_data_link:= 'UDE_DATATRANSFER_LINK';
    FROM_SCHEMA := 'UDE_OLTP';
    --DBMS_OUTPUT.PUT_LINE(SUBSTR(stmt2,1,200));
    STMT2 := T_MERGE;
    dbms_output.put_line(from_schema||' '||remote_data_link||' '||l_last_process_date);
    EXECUTE IMMEDIATE stmt2 using l_last_process_date;
    --execute immediate stmt3 ;
    dbms_output.put_line(from_schema||' '||remote_data_link||' '||l_last_process_date);
    dbms_output.put_line(substr(stmt2,1,200));
    commit;
    EXCEPTION
    WHEN OTHERS THEN
    V_ERROR := SQLCODE||' '||SQLERRM;
    v_ERROR := V_ERROR ||' '||SUBSTR(stmt2,1,200);
    DBMS_OUTPUT.PUT_LINE(V_ERROR);
    --dbms_output.put_line(substr(stmt2,1,200));
    END;
    I get ora-00900 invalid sql statement
    can somebody explain why this happens
    Thanks

    I agree with jan and anthony. Your post is too long and ill-formatted. However here's my understanding of your problem (with examples though slightly different ones).
    1- I have a function that returns number of records in a any given table.
      1  CREATE OR REPLACE FUNCTION get_count(p_table varchar2)
      2     RETURN NUMBER IS
      3     v_cnt number;
      4  BEGIN
      5    EXECUTE IMMEDIATE('SELECT count(*) FROM '||p_table) INTO v_cnt;
      6    RETURN v_cnt;
      7* END;
    SQL> /
    Function created.
    SQL> SELECT get_count('emp')
      2  FROM dual
      3  /
    GET_COUNT('EMP')
                  14
    2- I decide to move the statement to a database table and recreate my function.
    SQL> CREATE TABLE test
      2  (stmt varchar2(2000))
      3  /
    Table created.
    SQL> INSERT INTO test
      2  VALUES('SELECT count(*) FROM p_table');
    1 row created.
    SQL> CREATE OR REPLACE FUNCTION get_count(p_table varchar2)
      2     RETURN NUMBER IS
      3     v_cnt number;
      4     v_stmt varchar2(4000);
      5  BEGIN
      6     SELECT stmt INTO v_stmt
      7     FROM test;
      8     EXECUTE IMMEDIATE(v_stmt) INTO v_cnt;
      9     RETURN v_cnt;
    10  END;
    11  /
    Function created.
    SQL> SELECT get_count('emp')
      2  FROM dual
      3  /
    SELECT get_count('emp')
    ERROR at line 1:
    ORA-00942: table or view does not exist
    ORA-06512: at "SCOTT.GET_COUNT", line 8
    ORA-06512: at line 1
    --p_table in the column is a string and has nothing to do with p_table parameter in the function. And since there's no p_table table in my schema function returns error on execution. I suppose this is what you mean by "sql statement in a table not accepting variable"
    3- I rectify the problem by recreating the function.
      1  CREATE OR REPLACE FUNCTION get_count(p_table varchar2)
      2     RETURN NUMBER IS
      3     v_cnt number;
      4     v_stmt varchar2(4000);
      5  BEGIN
      6     SELECT replace(stmt,'p_table',p_table) INTO v_stmt
      7     FROM test;
      8     EXECUTE IMMEDIATE(v_stmt) INTO v_cnt;
      9     RETURN v_cnt;
    10* END;
    SQL> /
    Function created.
    SQL> SELECT get_count('emp')
      2  FROM dual
      3  /
    GET_COUNT('EMP')
                  14
    Hope this gives you some idea.-----------------------
    Anwar

  • Nested SQL statements for complex, detailed queries.

    Is it  possible to write nested SQL statements for complex, detailed queries. A nested query
    has a WHERE clause that includes a SELECT statement ? Is it true or false ?

    Hi wahid,
    Here are pretty good examples: 
    http://www.databasejournal.com/features/mssql/article.php/3464481/Using-a-Subquery-in-a-T-SQL-Statement.htm
    http://technet.microsoft.com/en-us/library/aa213252(v=sql.80).aspx
    Regards Harsh

  • How to get SQL script for generating table, constraint, indexes?

    I'd like to get from somewhere Oracle tool for generating simple SQL script for generating table, indexes, constraint (like Toad) and it has to be Oracle tool but not Designer.
    Can someone give me some edvice?
    Thanks!
    m.

    I'd like to get from somewhere Oracle tool for
    generating simple SQL script for generating table,
    indexes, constraint (like Toad) and it has to be
    Oracle tool but not Designer.
    SQL Developer is similar to Toad and is an Oracle tool.
    http://www.oracle.com/technology/products/database/sql_developer/index.html

Maybe you are looking for