Shouldn't using WITH return the same results as if you'd put the results in a table first?

First off, here's my version info:
BANNER
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for HPUX: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
I just reread the documentation again on the subquery factoring clause of SELECT statement, and I didn't see any restrictions that would apply.
Can someone help me understand why I'm getting different results?  I'd like to be able to use the statement that creates MAT3, but for some reason it doesn't work.  However, when I break it up and store the last TMP subquery in a table (MAT1), I'm able to get the expected results in MAT2.
Sorry if the example seems a little esoteric.  I was trying to put something together to help illustrate another problem, so it was convenient to use the same statements to illustrate this problem.
drop table mat1;
create table mat1 as
with skus as (
  select level as sku_id
  from dual
  connect by level <= 1000
  tran_dates as (
  select to_date('20130731', 'yyyymmdd') + level as tran_date
  from dual
  connect by level <= 31
  sku_dates as (
  select s.sku_id,
  t.tran_date,
  case when dbms_random.value * 5 < 4
  then 0
  else 1
  end as has_changes,
  round(dbms_random.value * 10000, 2) as unit_cost
  from skus s
  inner join tran_dates t
  on 1 = 1
select d.sku_id,
  d.tran_date,
  d.unit_cost
  from sku_dates d
  where d.has_changes = 1
drop table mat2;
create table mat2 as
select m.sku_id,
  m.tran_date,
  m.unit_cost,
  min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
  from mat1 m
drop table mat3;
create table mat3 as
with skus as (
  select level as sku_id
  from dual
  connect by level <= 1000
  tran_dates as (
  select to_date('20130731', 'yyyymmdd') + level as tran_date
  from dual
  connect by level <= 31
  sku_dates as (
  select s.sku_id,
  t.tran_date,
  case when dbms_random.value * 5 < 4
  then 0
  else 1
  end as has_changes,
  round(dbms_random.value * 10000, 2) as unit_cost
  from skus s
  inner join tran_dates t
  on 1 = 1
  tmp as (
  select d.sku_id,
  d.tran_date,
  d.unit_cost
  from sku_dates d
  where d.has_changes = 1
select m.sku_id,
  m.tran_date,
  m.unit_cost,
  min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
  from tmp m
select count(*) from mat2;
select count(*) from mat3;
  from tmp m
select count(*) from mat2;
select count(*) from mat3;
select count(*) from mat2;
  COUNT(*)
     31000
Executed in 0.046 seconds
select count(*) from mat3;
  COUNT(*)
         0
Executed in 0.031 seconds

I think there's something else going on.
I made the change you suggested, with a slight modification to retain the same functionality of flagging ~80% of the rows as not having changes.  I then copied that section of my script - included below - and pasted it into my session twice.  Unfortunately, I got different results each time.  I have had a number of strange problems when using the WITH clause, which is one of the reasons I jumped at posting something here when I encountered it again in this context.
Can you help me understand why this would happen?
drop table mat3;
create table mat3 as
with skus as (
  select level as sku_id
  from dual
  connect by level <= 1000
  tran_dates as (
  select to_date('20130731', 'yyyymmdd') + level as tran_date
  from dual
  connect by level <= 31
  sku_dates as (
  select s.sku_id,
  t.tran_date,
  case when dbms_random.value(1,100) * 5 < 400
  then 0
  else 1
  end as has_changes,
  round(dbms_random.value * 10000, 2) as unit_cost
  from skus s
  inner join tran_dates t
  on 1 = 1
  tmp as (
  select d.sku_id,
  d.tran_date,
  d.unit_cost
  from sku_dates d
  where d.has_changes = 1
select m.sku_id,
  m.tran_date,
  m.unit_cost,
  min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
  from tmp m
select count(*) from mat2;
select count(*) from mat3;
152249 < mattk > drop table mat3;
Table dropped
Executed in 0.016 seconds
152249 < mattk > create table mat3 as
                         2  with skus as (
                         3   select level as sku_id
                         4   from dual
                         5   connect by level <= 1000
                         6   ),
                         7   tran_dates as (
                         8   select to_date('20130731', 'yyyymmdd') + level as tran_date
                         9   from dual
                        10   connect by level <= 31
                        11   ),
                        12   sku_dates as (
                        13   select s.sku_id,
                        14   t.tran_date,
                        15   case when dbms_random.value(1,100) * 5 < 400
                        16   then 0
                        17   else 1
                        18   end as has_changes,
                        19   round(dbms_random.value * 10000, 2) as unit_cost
                        20   from skus s
                        21   inner join tran_dates t
                        22   on 1 = 1
                        23   ),
                        24   tmp as (
                        25   select d.sku_id,
                        26   d.tran_date,
                        27   d.unit_cost
                        28   from sku_dates d
                        29   where d.has_changes = 1
                        30   )
                        31  select m.sku_id,
                        32   m.tran_date,
                        33   m.unit_cost,
                        34   min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
                        35   from tmp m
                        36  ;
Table created
Executed in 0.53 seconds
152250 < mattk > select count(*) from mat2;
  COUNT(*)
         0
Executed in 0.047 seconds
152250 < mattk > select count(*) from mat3;
  COUNT(*)
     31000
Executed in 0.047 seconds
152250 < mattk >
152251 < mattk > drop table mat3;
Table dropped
Executed in 0.016 seconds
152252 < mattk > create table mat3 as
                         2  with skus as (
                         3   select level as sku_id
                         4   from dual
                         5   connect by level <= 1000
                         6   ),
                         7   tran_dates as (
                         8   select to_date('20130731', 'yyyymmdd') + level as tran_date
                         9   from dual
                        10   connect by level <= 31
                        11   ),
                        12   sku_dates as (
                        13   select s.sku_id,
                        14   t.tran_date,
                        15   case when dbms_random.value(1,100) * 5 < 400
                        16   then 0
                        17   else 1
                        18   end as has_changes,
                        19   round(dbms_random.value * 10000, 2) as unit_cost
                        20   from skus s
                        21   inner join tran_dates t
                        22   on 1 = 1
                        23   ),
                        24   tmp as (
                        25   select d.sku_id,
                        26   d.tran_date,
                        27   d.unit_cost
                        28   from sku_dates d
                        29   where d.has_changes = 1
                        30   )
                        31  select m.sku_id,
                        32   m.tran_date,
                        33   m.unit_cost,
                        34   min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
                        35   from tmp m
                        36  ;
Table created
Executed in 0.078 seconds
152252 < mattk > select count(*) from mat2;
  COUNT(*)
         0
Executed in 0.031 seconds
152252 < mattk > select count(*) from mat3;
  COUNT(*)
         0
Executed in 0.047 seconds

Similar Messages

  • Have an old mac mini 2008 running OS x 10.6 whenever I shut down or restart it wont find the dell screen again. have tried with different screen same result. have revived it before by unplugging all cables zapping PRAM and starting in safe mode

    Have an old mac mini 2008 running OS x 10.6 Whenever I shut down or restart it won't find the dell screen again. Have tried with different screen same result. Have revived it before by unplugging all cables zapping PRAM and starting in safe mode but no luck now. It has all my daughters exam work on it which she, of course, has not backed up so any suggestions gratefully received.

    Make an appointment at the Genius Bar of an Apple store.
      Apple Retail Store - Genius Bar
    You do have a backup, right?

  • What type of hard drive replacement should i use to replace the hard drive in my first generation mac min 2005 model a 1103?

    what type of hard drive replacement should I use to replace the hard drive on my first generation mac mini model a 1103?   G4 1.25 mhz 40 gb hd 1 gb. mem.

    You'll need a 2.5" parallel ATA interface hard drive. Most are 5400RPM, forget 7200RPM drives and slower 4200rpm drives.  You don't want a serial ATA (SATA) drive, as that has a different connector and won't work.
    One source I've used for drives is Newegg, and their parallel ATA hard drives are at http://www.newegg.com/Product/ProductList.aspx?Submit=ENE&N=100007605%2060000344 2&IsNodeId=1&name=IDE%20Ultra%20ATA100%20%2f%20ATA-6 .  Newegg also has many user comments about the drives. Other sources are OWC at http://eshop.macsales.com/shop/hard-drives/2.5-Notebook/ (Click on ATA on the left) . Look for at least a 3 year warranty. 

  • Function module or BAPI that is used to update the records in RBKP table.

    Hello All,
    Can anybody please give me the name of any Function module or BAPI that is used to update the records in RBKP table.
    Please help me
      I need to change the fiscal year in RBKP table
    Thanks in Advance,
    Regards,
    LIJO

    Hi,
    You can use the BAPIs,
    BAPI_ACC_INVOICE_RECEIPT_CHECK Accounting: Check Invoice Receipt (OAG: LOAD PAYABLE)
    BAPI_ACC_INVOICE_RECEIPT_POST  Accounting: Post Invoice Receipt (OAG: LOAD PAYABLE)
    Hope this helps.
    Regards,
    Renjith Michael.

  • I use podcasts all the time but downloaded my first books from the istore this morning. how do I get them to my ipod?

    I use podcasts all the time but downloaded my first books from the istore this morning, I can't get them synced onto my ipod. What am I doing wrong?

    the audiobooks I purchased show up in itunes under books and under purchased items but i cannot make them sync to my ipod. I followed your directions but it did not help. I have drag and dropped to a playlist which shows up on itunes but not on my ipod even after repeated syncs. when I try to drag and drop to the books or audiobooks in the itunes list it gives me the NO symbol.

  • Call same smartform in program with different text - same result

    Hello,
    I'm facing the following problem:
    In a program, a certain smartforms form can be called multiple times. Only the text can change, depending on the user action.
    However, the resulting form does not show different text for the different actions. The first time it is used, the text is correct, but from the second time onwards, the text in the form will still be same than the first time.
    The text type in the smartform is 'include text', so I can see in the standard text transaction (so10) that the text was indeed changed correctly. But still it shows the same text in the result.
    Is there some option I have missed?
    Thanks

    Hi,
    You can use COMMIT_TEXT.
    The system keeps all text modules for which you defined 'storage in update task' in the corresponding text object in the text memory. As soon as it updates the corresponding application object, it must also place the text modules into the log file.
    The function module COMMIT_TEXT generates for the text modules in the text object a CALL FUNCTION... IN UPDATE TASK statement in accordance with the action to be executed (delete, create, change).
    No COMMIT WORK is created. This must be executed by the application program.
    If you do not specify OBJECT, NAME, ID, and LANGUAGE, the system transfers all texts from the text memory. To limit the function to certain texts, enter values (fully or generically) in the above fields. The system then selects all texts that match the selections in the fields up to the first ' * '.
    By default, the system deletes texts from the text memory as soon as they are written to the log file. If you want to keep updated texts in the text memory, call the function module with the parameter KEEP = 'X'. The system then keeps the texts in the text memory and flags them as updated. When calling COMMIT_TEXT again, the system ignores these texts. If you change such a text again during the transaction (for example, using SAVE_TEXT or DELETE_TEXT), the system deletes the flag. However, you need another COMMIT_TEXT to update the text.
    <b>Reward if helpful.</b>

  • Lookup expression is not working when I am using with in the rectangle box in SSRS

    I have two datasets with common member is ProgramID.
    I have followed the below steps,
    1. Added one table  (2x2) and mapped this table to dataset1. After that I have removed top row.                                               
    That means I am maintaing only  data row.
    2. Added group to the table (ProgramID). Now the programid is sitting at cell#(1,1) and  cell#(1,2) is blank
    3. I have verified the report with preview and able to see the data for my all programs. I have 5 programs
    4. my plan is adding chart and matrix reports to cell# (1,2). If I am adding chart/matrix to the cell#(1,2) then we can't contol the  cell alignments in terms of height x width.
     To resolve that problem, I have added one rectangle control to the cell#(1,2) then I have added my chart and matrix report
    5. After completing the report, I have verified with preview and able to see the reports (chart/matrix) for all my 5 programs by page wise (note: I have provided page break before for my rectangle)
    6. Till this point, I have no issues
    7. Now, I have added one more row under the group. so, we have our main table (2x2). Becuase of programId group we can only see 3 cells in total table  (cell# (1,1) =ProgramId  cell#(1,2)=chart and matix reports under rectangle box; cell#(2,1)=already
    merged with ProgramID group; cell#(2,2)= planned to add one more rectanlge and add two reports (chart and matrix)
    8. Now, i have added rectangle to cell#(2,2) then added chart and matrix reports within the rectangle.
    9. cell#(2,2) reports are needs to extract the data from dataset-2. Here we have common member is ProgramID (ds-1,ds-2)
    10. First I have tested for matrix report by adding lookup expression
          Matrix report (2x2)
         row-1: Cell(1,1)= Gender report heading ; cell(1,2)=Month  (column group)
         Note: month is also common member in both the datasets. 
         row-2: cell(2,1)--  =Lookup(Programid.value, Programid.value,Gender.value,"Dataset2")  (Gender data is Male and Female)
                    Cell(2,2) -- empty for now
     11. I have tried to preview the report and able to see the report without any issues and below are observations
            main Table (2 x2)
             Cell# (1,1)  = ProgramID --Returning all 5 programs properly
             Cell# (1,2)  = Dataset1 related report (chart and matrix reports under rectangle) -- able to see without any issues
             Cell#(2,1)  = We already know this is merged cell. i.e menas ProgramID dispalying
              Cell#(2,2)= Only Male record is dispalying in the matrix report which is under rectanlge (here we have applied Lookup expression.
    Note: male is the first record from the database and Female is the second record
    Also, observed that Cell#(2,2) matrix report is unable to display the gender count information when I have provided count value expression in the cell(2,2) within the matrix and the expression I have uses is
      = sum(Lookup(ProgramId.value,ProgramId.value,GenderCOunt.value,"Dataset2"))
    Please ask me if I am unable to explain or lengthy explain.
    Point : Why the lookup expression is giving the first record only (in case of gender #(male, female) and why it is unable to give the gendercount.  Gendercount cell is totally blank.
    Please help me.
    Kishore.

    Hi Kishore,
    I have tested the issue step by step by following you description, while the first issue works well in my local environment. Based on my research, this can be caused by the lookup expression or it indeed return Male value based on the logic. If you use the
    expression below, it will indeed only return the Male record. So please try to double-check the record in the two datasets and the expression in your environment:
    =lookup(first(Fields!ProgramID.Value,"DataSet1"),Fields!ProgramID.Value,Fields!Gender.Value,"DataSet2")
    As to the second issue, please try to use the following expression:
    =Count(Lookup(fields!ProgramID.value,fields!ProgramID.value,fields!Gender.value,"DataSet2"))
    Besides, if this issue still exist, in order to trouble shoot this issue more efficiently, could you please post both the .rdl  file with all the size properties to us by the following E-mail address?  It is benefit for us to do further analysis.
    E-mail: [email protected]
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • What is the Function MOdule that returns the fields in database table order

    Hello Folks
      I have a dynamic internal table with fields ( which are not in order). I want to display them in the order of which they are present in the database table? Is there any function module that returns the fields in database order?
    FAQ. Please search before posting your question.
    Edited by: Suhas Saha on Oct 10, 2011 10:19 PM

    Hi,
    You can use this BAPI.
    <b>BAPI_SALESORDER_GETLIST</b>
    Reward if useful.
    Regards,
    Vimal

  • Return the Minimum from two tables in a Union

    Hello
    I have written a simple union query from our dialler service which has 2 tables, in inbound and an outbound table. A lead can show in any table depending how they came through to an agent and they can also show in both tables.  I need to return the
    the first instance in the calltime column from both tables but also return all the other information.  I'm expecting multiple entries for the minimum calltime which is fine, I will be using a lookup in excel to extract the time.  Just to make matters
    a little more difficult i'm having to use the OPENQUERY function because its within MYSQL.
    SELECT O.Lead, O.DataList, O.[CallTime] AS 'CallDate', O.[Status], O.Uniqueid, O.Campaign, O.number, O.Advisor, 'Outbound' AS CallType
    FROM OPENQUERY (dialler, 'SELECT * FROM dialler_outbound_data') O
    WHERE O.CallTime >= '2014-12-01'
    UNION
    SELECT I.Lead, I.DataList, [CallTime] AS 'CallDate', I.[Status], I.Uniqueid, I.Campaign, I.Number, I.Advisor, 'Inbound' AS CallType
    FROM OPENQUERY (dialler, 'SELECT * FROM dialler_inbound_data') I
    WHERE I.CallTime >= '2014-12-01'
    Can anyone help please?
    Many Thanks
    Rich

    Hi Rich,
    The use of the ROW_NUMBER function as mentioned and temp tables should do the trick.  See the code below...hope this helps.
    SELECT ROW_NUMBER() OVER(ORDER BY O.[CallTime]) as 'Occurrance'
    ,O.Lead
    ,O.DataList
    ,O.[CallTime] AS 'CallDate'
    ,O.[Status]
    ,O.Uniqueid
    ,O.Campaign
    ,O.number
    ,O.Advisor
    ,'Outbound' AS CallType
    INTO #TEMP1
    FROM OPENQUERY (dialler, 'SELECT * FROM dialler_outbound_data') O
    WHERE O.CallTime >= '2014-12-01'
    SELECT ROW_NUMBER() OVER(ORDER BY I.[CallTime]) as 'Occurrance'
    ,I.Lead
    ,I.DataList
    ,[CallTime] AS 'CallDate'
    ,I.[Status]
    ,I.Uniqueid
    ,I.Campaign
    ,I.Number
    ,I.Advisor
    ,'Inbound' AS CallType
    INTO #TEMP2
    FROM OPENQUERY (dialler, 'SELECT * FROM dialler_inbound_data') I
    WHERE I.CallTime >= '2014-12-01'
    INSERT INTO #FINAL
    SELECT * FROM #TEMP1
    UNION
    SELECT * FROM #TEMP2
    SELECT * FROM #FINAL AS F WHERE F.Occurrance = 1
    I'm new to this so if this helps, I could use your vote. 
    Thanks,
     Zach

  • Can iCloud be used with snow leopard on a Mac if you have an ipad2?

    Can iCloud be used with a Mac that has been upgraded to it's full capacity with snow leopard if you are using it with an ipad2 and iPhone 4 ( with upgraded system)?

    If you have joined iCloud on your iPad, this will be the situation on your Snow Leopard Mac:
    You will be able to access email, calendar and contacts on the iCloud website at http://icloud.com provided your browser is reasonably up-to-date.
    You will not be able to sync contacts or bookmarks from a pre-Lion Mac.
    You will be able to enter the server settings for email manually in the Mail application and access your email. A pictorial instruction page on doing this is here.
    You will not be able to sync your calendars directly.
    Some people have been able to set up calendar syncing by using the method detailed here - this is an unsupported hack and may not be reliable, and may stop working at some future point. BusyCal is an iCal-like calendar application with extra facilities: it can sync with the iCloud Calendar while running on Leopard, Snow Leopard or Lion.
    There appears to be no method of syncing contacts (though Address Book on a Mac can be synced with Google or Yahoo address books - I don't know how reliable this is)

  • Over partition: how to use to return the max of two columns

    For each unique id, I want to select the value of col2 in the record with the most recent date.
    When the rows with the same IDs have the same dates, I want the max value from col2.
    I want one row for each ID, but I'm getting two rows for ID 3333333.
    with data as
    select 1111111 as id, 'a' as col2, to_date('01-JAN-09','dd-mon-yyyy') as the_date from dual union all
    select 2222222 as id, 'b' as col2, to_date('02-JAN-09','dd-mon-yyyy') as the_date from dual union all
    select 2222222 as id, 'c' as col2, to_date('03-JAN-09','dd-mon-yyyy') as the_date from dual union all
    select 2222222 as id, 'd' as col2, to_date('04-JAN-09','dd-mon-yyyy') as the_date from dual union all
    select 3333333 as id, 'e' as col2, to_date('05-JAN-09','dd-mon-yyyy') as the_date from dual union all
    select 3333333 as id, 'f' as col2, to_date('05-JAN-09','dd-mon-yyyy') as the_date from dual
    select id, col2, the_date
    from
    select id, the_date, col2, max(the_date) over (partition by id) as max_the_date, max(col2) over (partition by col2) as max_col2
    from data
    where the_date = max_the_date and col2 = max_col2 order by id
    Expecting this:
    ID     COL2     THE_DATE
    1111111     a     1/1/0009
    2222222     d     1/4/0009
    3333333     f     1/5/0009
    but I'm getting 2 rows for ID 3333333
    Any suggestions?

    TRy this code without subquery
    SELECT   ID, MAX (the_date)KEEP (DENSE_RANK LAST ORDER BY the_date),
             MAX (col2)KEEP (DENSE_RANK LAST ORDER BY the_date)
        FROM DATA
    GROUP BY ID
    ORDER BY ID
    SQL> WITH DATA AS
      2       (SELECT 1111111 AS ID, 'a' AS col2,
      3               TO_DATE ('01-01-2009', 'dd-mm-yyyy') AS the_date
      4          FROM DUAL
      5        UNION ALL
      6        SELECT 2222222 AS ID, 'b' AS col2,
      7               TO_DATE ('02-01-2009', 'dd-mm-yyyy') AS the_date
      8          FROM DUAL
      9        UNION ALL
    10        SELECT 2222222 AS ID, 'c' AS col2,
    11               TO_DATE ('03-01-2009', 'dd-mm-yyyy') AS the_date
    12          FROM DUAL
    13        UNION ALL
    14        SELECT 2222222 AS ID, 'd' AS col2,
    15               TO_DATE ('04-01-2009', 'dd-mm-yyyy') AS the_date
    16          FROM DUAL
    17        UNION ALL
    18        SELECT 3333333 AS ID, 'e' AS col2,
    19               TO_DATE ('05-01-2009', 'dd-mm-yyyy') AS the_date
    20          FROM DUAL
    21        UNION ALL
    22        SELECT 3333333 AS ID, 'f' AS col2,
    23               TO_DATE ('05-01-2009', 'dd-mm-yyyy') AS the_date
    24          FROM DUAL)
    25  SELECT   ID, MAX (the_date)KEEP (DENSE_RANK LAST ORDER BY the_date ),
    26           MAX (col2)KEEP (DENSE_RANK LAST ORDER BY the_date )
    27      FROM DATA
    28      group by id
    29  ORDER BY ID;
            ID MAX(THE_DA M
       1111111 2009-01-01 a
       2222222 2009-01-04 d
       3333333 2009-01-05 f
    SQL> Edited by: Salim Chelabi on 2009-03-05 11:49
    Edited by: Salim Chelabi on 2009-03-05 11:50

  • Problems with returning the license

    I bought a a VLM agreement with licenses, and 2 of the computers crashed how can I return these 2 licenses back to the available pool being as the computers are un-available?

    For the initial version of ALM Adobe has established a process to recover lost e-licenses via customer service. In the event of lost licenses (hard drive crashes, lost or stolen laptop, etc.), customers can call customer service and e-licenses will be added to the pool indicated by the customer (corresponding to the number lost).
    Future versions of ALM will enable for self-service and auto-detection/recovery of lost e-licenses.

  • Error while linked server is used to verify the existence of a table

    Hi All,
    Pls help me solve an issue related to linked Server..
    Following is my query:
    IF EXISTS (select 1 from [LinkedServerName].Sales.sys.tables where name = 'SalesTable') 
    BEGIN
     DECLARE @LastSales varchar(25) 
     SET @LastSales = (select CONVERT(VARCHAR(25),max(LastSalesTime)) from [LinkedServerName].SalesDB.dbo.SalesTable)
    END
    What should happen is, it should check the existence of SalesTable on Linked Server (select 1 from [LinkedServer].Sales.sys.tables where name = 'SalesTable'), and only after the it returns 1, the variable @LastSales should be alloted a value.
    But in my case, as soon as I run the code, it gives the error:
    The OLE DB provider "SQLNCLI10" for linked server "LinkedServerName" does not contain the table ""Sales"."dbo"."SalesTable"". 
    The table either does not exist or the current user does not have permissions on that table.
    My question is that if the condition fails at the very outset returning null (as the table does not exist), then why it is entering BEGIN and throwing the error.
    All your valuable inputs are appreciated !!

    Inconsistency is the hailmark of SQL Server.
    If you say:
    IF EXISTS (select 1 from Sales.sys.tables where name = 'SalesTable') 
    BEGIN
     DECLARE @LastSales varchar(25) 
     SET @LastSales = (select CONVERT(VARCHAR(25),max(LastSalesTime)) from        SalesDB.dbo.SalesTable)
    END
    This will work as you expect. That is, if the table does not exist, the batch will parse and produce no result. This is because SQL Server has deferred name resolution for tables. If a table is missing at compile time, SQL Server suppresses the error in
    hope that the table will be there at run-time.
    But there is an exception to this. For table sources on remote data sources, there is no deferred name resolution, but the table has to be there at compile time. This is quite ironic. Personally, I think deferred name resolution (which was introduced in
    SQL 7) is a big misfeature, but I could buy if there was deferred name resolution for objects on linked server, since the server may be down, and you have coded for this like you have above. Well, if the server is down, the IF EXISTS will fail to compile to.
    You will need to introduce a new scope somewhere to avoid the problem. For instance:
    IF EXISTS (select 1 from [LinkedServerName].Sales.sys.tables where name = 'SalesTable') 
    BEGIN
       DECLARE @LastSales varchar(25) 
       EXEC [Linkedservername].SalesDB.sys.sp_executesql
            N'SET @LastSales = (select CONVERT(VARCHAR(25),max(LastSalesTime)) from SalesTable)',
            N'@LastSales varchar(25) OUTPUT', @LastSales
    END
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Can we make one query to get same results from 3 tables

    CREATE TABLE TABLE1 (NODEID VARCHAR2(4));
    CREATE TABLE TABLE2 (NODEID VARCHAR2(4));
    CREATE TABLE TABLE3 (NODEID VARCHAR2(4));
    INSERT INTO TABLE1 VALUE('1004');
    INSERT INTO TABLE1 VALUE('1004');
    INSERT INTO TABLE1 VALUE('1002');
    INSERT INTO TABLE1 VALUE('1002');
    INSERT INTO TABLE1 VALUE('1001');
    INSERT INTO TABLE1 VALUE('1001');
    INSERT INTO TABLE1 VALUE('1006');
    INSERT INTO TABLE1 VALUE('1006');
    INSERT INTO TABLE1 VALUE('1005');
    INSERT INTO TABLE1 VALUE('1005');
    INSERT INTO TABLE2 VALUE('1004');
    INSERT INTO TABLE2 VALUE('1004');
    INSERT INTO TABLE2 VALUE('1004');
    INSERT INTO TABLE2 VALUE('1002');
    INSERT INTO TABLE2 VALUE('1002');
    INSERT INTO TABLE2 VALUE('1002');
    INSERT INTO TABLE2 VALUE('1002');
    INSERT INTO TABLE3 VALUE('1001');
    INSERT INTO TABLE3 VALUE('1001');
    INSERT INTO TABLE3 VALUE('1006');
    INSERT INTO TABLE3 VALUE('1006');
    INSERT INTO TABLE3 VALUE('1005');
    INSERT INTO TABLE3 VALUE('1005');
    INSERT INTO TABLE3 VALUE('1004');
    INSERT INTO TABLE3 VALUE('1004');
    INSERT INTO TABLE3 VALUE('1004');
    INSERT INTO TABLE3 VALUE('1002');
    INSERT INTO TABLE3 VALUE('1002');
    INSERT INTO TABLE3 VALUE('1002');
    INSERT INTO TABLE3 VALUE('1002');
    Select count(*), count(distinct nodeid)
    from table1, table2,table3
    where table1.nodeid=table2.nodeid and table1.nodeid=table3.nodeid;
    Select count(*), count(distinct nodeid)
    from table1, table3
    where table1.nodeid=table2.nodeid;
    Select count(*), count(distinct nodeid)
    from table2, table3
    where table2.nodeid=table3.nodeid;

    Aside from your insert statements not working... (should be as follows)...
    DROP TABLE TABLE1;
    DROP TABLE TABLE2;
    DROP TABLE TABLE3;
    CREATE TABLE TABLE1 (NODEID VARCHAR2(4));
    CREATE TABLE TABLE2 (NODEID VARCHAR2(4));
    CREATE TABLE TABLE3 (NODEID VARCHAR2(4));
    INSERT INTO TABLE1 VALUES('1004');
    INSERT INTO TABLE1 VALUES('1004');
    INSERT INTO TABLE1 VALUES('1002');
    INSERT INTO TABLE1 VALUES('1002');
    INSERT INTO TABLE1 VALUES('1001');
    INSERT INTO TABLE1 VALUES('1001');
    INSERT INTO TABLE1 VALUES('1006');
    INSERT INTO TABLE1 VALUES('1006');
    INSERT INTO TABLE1 VALUES('1005');
    INSERT INTO TABLE1 VALUES('1005');
    INSERT INTO TABLE2 VALUES('1004');
    INSERT INTO TABLE2 VALUES('1004');
    INSERT INTO TABLE2 VALUES('1004');
    INSERT INTO TABLE2 VALUES('1002');
    INSERT INTO TABLE2 VALUES('1002');
    INSERT INTO TABLE2 VALUES('1002');
    INSERT INTO TABLE2 VALUES('1002');
    INSERT INTO TABLE3 VALUES('1001');
    INSERT INTO TABLE3 VALUES('1001');
    INSERT INTO TABLE3 VALUES('1006');
    INSERT INTO TABLE3 VALUES('1006');
    INSERT INTO TABLE3 VALUES('1005');
    INSERT INTO TABLE3 VALUES('1005');
    INSERT INTO TABLE3 VALUES('1004');
    INSERT INTO TABLE3 VALUES('1004');
    INSERT INTO TABLE3 VALUES('1004');
    INSERT INTO TABLE3 VALUES('1002');
    INSERT INTO TABLE3 VALUES('1002');
    INSERT INTO TABLE3 VALUES('1002');
    INSERT INTO TABLE3 VALUES('1002');and you're queries not working...
    SQL> Select count(*), count(distinct nodeid)
      2  from table1, table2,table3
      3  where table1.nodeid=table2.nodeid and table1.nodeid=table3.nodeid;
    Select count(*), count(distinct nodeid)
    ERROR at line 1:
    ORA-00918: column ambiguously definedI'm guessing you want:
    SQL> Select count(*), count(distinct table1.nodeid)
      2  from table1, table2,table3
      3  where table1.nodeid=table2.nodeid and table1.nodeid=table3.nodeid;
      COUNT(*) COUNT(DISTINCTTABLE1.NODEID)
            50                            2You haven't explained to us what database version you are using or what you want the output to look like when these 3 queries are combined.
    Please read: {message:id=9360002} and learn to post a good question, and use {noformat}{noformat} tags to show your code/data.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Issue with fetching the metadata of Agile tables

    Can some one tell me how to retrieve metadata of all the tables of a particular Agile object and display it into an html page ? (something like how it is displayed in the Agile >Print preview)
    Any help appreciated.
    Thanks

    There is a JavaDoc on SDK. it is located in SDK integration folder in Agile software. you need to access machiend where agile is installed.
    for DB tables stuff, create schema doc using toad by pointing to Agile Schema.
    Swayamsevak

Maybe you are looking for

  • Cleaning kit for Macbook Pro?

    Hello, I have recently purchased the Macbook Pro. But, as the screen and the exterior parts of the Macbook Pro were getting dirty, I am currently trying to find a cleaning kit for the Macbook Pro and for other electronic devices. I hear products like

  • How do I get rid of Bing and adware on my macbook? I have changed the preferences on Safari to google, and tried to download an adware remover, but things are now worse!

    Hi can anyone help me to get rid of Bing and all sorts of Adware on my macbook which arrive everytime I use Safari? I have changed the Safari preferences but Bing won't go away, and worse, it comes up with adverts. I also seem to be losing things I d

  • Table into matrix

    Hi everyone! I've got the following problem. I have to create a package which deals with some mathematical calculations. A procedure in this package takes a name of view as a parameter and has to "convert" this view to a matrix. For this purpose I've

  • Can't find FP read in 2013

      Can not find these tools in LabVIEW 2013 Solved! Go to Solution.

  • Reading TXT files on WEBmode

    Hy My problem is to read TXT-Files in Webmode. In Client-Server mode it works fine. I use the function "text_io". In Webmode i get an error "No data found" (i think it is FRM-40375). In which virtual directory should the files placed on the OAS???? D