views:

389

answers:

3

I have the following query, now the strange thing is if I run this query on my development and pre-prod server it runs fine. If I run it on production it fails.

I have figured out that if I run just the Select statement its happy but as soon as I try insert into the table variable it complains.

DECLARE @RESULTS TABLE
    (
     [Parent] VARCHAR(255)
    ,[client] VARCHAR(255)
    ,[ComponentName] VARCHAR(255)
    ,[DealName] VARCHAR(255)
    ,[Purchase Date] DATETIME
    ,[Start Date] DATETIME
    ,[End Date] DATETIME
    ,[Value] INT
    ,[Currency] VARCHAR(255)
    ,[Brand] VARCHAR(255)
    ,[Business Unit] VARCHAR(255)
    ,[Region] VARCHAR(255)
    ,[DealID] INT
    )

INSERT  INTO @RESULTS
SELECT DISTINCT
    ClientName 'Parent'
   ,F.ClientID 'client'
   ,ComponentName
   ,A.DealName
   ,CONVERT(SMALLDATETIME , ISNULL(PurchaseDate , '1900-01-01')) 'Purchase Date'
   ,CONVERT(SMALLDATETIME , ISNULL(StartDate , '1900-01-01')) 'Start Date'
   ,CONVERT(SMALLDATETIME , ISNULL(EndDate , '1900-01-01')) 'End Date'
   ,DealValue 'Value'
   ,D.Currency 'Currency'
   ,ShortBrand 'Brand'
   ,G.BU 'Business Unit'
   ,C.DMRegion 'Region'
   ,DealID
FROM
    LTCDB_admin_tbl_Deals A
    INNER JOIN dbo_DM_Brand B
    ON A.BrandID = B.ID
    INNER JOIN LTCDB_admin_tbl_DM_Region C
    ON A.Region = C.ID
    INNER JOIN LTCDB_admin_tbl_Currency D
    ON A.Currency = D.ID
    INNER JOIN LTCDB_admin_tbl_Deal_Clients E
    ON A.DealID = E.Deal_ID
    INNER JOIN LTCDB_admin_tbl_Clients F
    ON E.Client_ID = F.ClientID
    INNER JOIN LTCDB_admin_tbl_DM_BU G
    ON G.ID = A.BU
    INNER JOIN LTCDB_admin_tbl_Deal_Components H
    ON A.DealID = H.Deal_ID
    INNER JOIN LTCDB_admin_tbl_Components I
    ON I.ComponentID = H.Component_ID
WHERE
    EndDate != '1899-12-30T00:00:00.000'
    AND StartDate < EndDate
    AND B.ID IN ( 1 , 2 , 5 , 6 , 7 , 8 , 10 , 12 )
    AND C.SalesRegionID IN ( 1 , 3 , 4 , 11 , 16 )
    AND A.BU IN ( 1 , 2 , 3 , 4 , 5 , 6 , 8 , 9 , 11 , 12 , 15 , 16 , 19 , 20 , 22 , 23 , 24 , 26 , 28 , 30 )
    AND ClientID = 16128

SELECT ... FROM @Results

I get the following error

Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1021.ComponentName" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1011.Currency" could not be bound.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2454'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2461'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2491'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2490'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2482'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2478'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2477'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2475'.

EDIT - EDIT - EDIT - EDIT - EDIT - EDIT through a process of elimination I have found that following and wondered if anyone can shed some light on this.

  1. If I remove only the DISTINCT the query runs fine, add the DISTINCT and I get strange errors.
  2. Also I have found that if I comment the following lines then the query runs with the DISTINCT what is strange is that none of the columns in the table LTCDB_admin_tbl_Deal_Components is referenced so I don't see how the distinct will affect that.
INNER JOIN LTCDB_admin_tbl_Deal_Components H 
ON A.DealID = H.Deal_ID
+2  A: 

Are any of these Views? I seem to remember getting weird errors like that after changing view definitions and not running sp_refreshview. I can't see the text "Tbl1021" anywhere so I'm assuming this is likely to be in a View definition?

If so there is a script here to refresh all views http://stackoverflow.com/questions/2491298/how-do-i-create-a-stored-procedure-that-calls-sp-refreshview-for-each-view-in-the

Martin Smith
These aren't views they are synonyms, pointing to different servers. Now before everyone says there is a difference in data between server, read my comment on the question, about it having something to do with the DISTINCT as that is the strangest part the SELECT always return one row but if I specify DISTINCT if throws an error. And on top of that this worked until this morning and I'm not aware of any changes. So basically what would cause this strange error
Eugene Niemand
Hmm, I was thinking that maybe that could be explained by a cached query plan for the view scenario. Has the definition of the Synonyms changed recently? Edit - Just seen that you say there have been no changes. I'm equally baffled then!
Martin Smith
Does seem very odd. Are the servers you are conecting to all SQL2008 instances as well or earlier versions? Can you reproduce with an extremely simple query against the synonyms pointing at Tbl1021.ComponentName or Tbl1011.Currency or is it only with the complex query using DISTINCT that the problem arises?
Martin Smith
Ok I tried the following:DECLARE @results TABLE ( Currency VARCHAR(255))INSERT INTO @results SELECT DISTINCT [Currency] FROM [WILLOW].[LTCDB].LTCDB_admin.tbl_CurrencyNo Problems
Eugene Niemand
I now tried it again for a different ClientID and there was 29 rows with or without the distinct but as soon as I specified the distinct it caused an error
Eugene Niemand
I presume that because of the error it doesn't successfully manage to generate an execution plan either that you can look at? Might be one for Microsoft PSS.
Martin Smith
Ok I just tried to include execution plan which didn't work, however the estimated execution plan works. Just by the way what is PSS.
Eugene Niemand
PSS = Microsoft Product Support Services. It makes absolutely no sense at all to me that simply adding DISTINCT to a query that returns results should produce this sort of error.
Martin Smith
Please see my update. Also I did try do a simple version as suggested before with the offending table "DECLARE @results TABLE ( [DealID] INT)INSERT INTO @resultsSELECT DISTINCT [A].[DealID] FROM [WILLOW].[LTCDB].LTCDB_admin.tbl_Deals AINNER JOIN [WILLOW].[LTCDB].LTCDB_admin.tbl_Deal_Components HON A.DealID = H.Deal_ID" but this works so I'm really confused
Eugene Niemand
A: 

Eugene Without running the code on my Development system, the first thing I would resolve is the inconsistent use of alias in the code You should as good coding practice always where possible identify the object {owner} I.E by using in this example prefix each object with the relevant alias A, B, C etc..

I.E for example ClientName 'Parent' make this A.ClientName 'Parent' if this columns was in table aliased as A

SELECT DISTINCT ClientName 'Parent' ,F.ClientID 'client' ,ComponentName ,A.DealName ,CONVERT(SMALLDATETIME , ISNULL(PurchaseDate , '1900-01-01')) 'Purchase Date' ,CONVERT(SMALLDATETIME , ISNULL(StartDate , '1900-01-01')) 'Start Date' ,CONVERT(SMALLDATETIME , ISNULL(EndDate , '1900-01-01')) 'End Date' ,DealValue 'Value' ,D.Currency 'Currency' ,ShortBrand 'Brand' ,G.BU 'Business Unit' ,C.DMRegion 'Region' ,DealID FROM LTCDB_admin_tbl_Deals A

Neil Hambly
A: 

Ok I still don't know what caused this or what the correct answer is but here is what I ended up doing to get it fixed.

  1. I created a duplicate of the offending table and populated it with a copy of the data.
  2. I created all the same keys, indexes etc etc
  3. Tested the query and guess what it worked.

So I made a backup of the data in the table dropped the offending table recreated it with all the keys, indexes etc etc and now order is restored.

All the queries that used to fail now works perfectly. Very strange

Eugene Niemand