views:

371

answers:

4

Hello,

I have a Linq to SQL query that was working just fine with SQL Server 2005 but, I have to deploy the web app with a SQL Server 2000 and, when executing that query, I get his error:

"System.Data.SqlClient.SqlException: The column prefix 't0' does not match with a table name or alias name used in the query."

I have more queries but it doesn't seems to have problems with those. Now, this is the query:

from warningNotices in DBContext_Analyze.FARs
where warningNotices.FAR_Area_ID == filter.WarningAreaID &&
      warningNotices.FAR_Seq == filter.WarningSeq &&
      warningNotices.FAR_Year == filter.WarningYear
      orderby warningNotices.FAR_Seq ascending
      select new Search_Result
      {
      FAR_Area_ID = warningNotices.FAR_Area_ID,
     FAR_Seq = warningNotices.FAR_Seq,
     FAR_Year = warningNotices.FAR_Year,
     DateTime_Entered = (DateTime)warningNotices.DateTime_Entered == null ?   DateTime.MaxValue : (DateTime)warningNotices.DateTime_Entered,
     Time_Entered = warningNotices.Time_Entered,
     OrigDept = warningNotices.OrigDept,
     Part_No = warningNotices.Part_No,
     DateTime_Analyzed = (DateTime)warningNotices.DateTime_Analyzed == null ? DateTime.MaxValue : (DateTime)warningNotices.DateTime_Analyzed,
     Analyzed_By = warningNotices.Analyzed_By,
     MDR_Required = (Char)warningNotices.MDR_Required == null ? Char.MinValue : (Char)warningNotices.MDR_Required,
     Resp_Dept = (from FARSympt in DBContext_Analyze.FAR_Symptoms
                  where FARSympt.FAR_Area_ID == warningNotices.FAR_Area_ID &&
          FARSympt.FAR_Year == warningNotices.FAR_Year &&
          FARSympt.FAR_Seq == warningNotices.FAR_Seq
           select new { FARSympt.Resp_Dept}).FirstOrDefault().Resp_Dept,
     Sympt_Desc = (from SymptomsCatalog in DBContext_Analyze.Symptoms
        where SymptomsCatalog.symptom_ID == filter.Status_ID
            select new { 
                                       SymptomsCatalog.Sympt_Desc
                                     }).FirstOrDefault().Sympt_Desc,
     Status_ID = warningNotices.Status.HasValue ? warningNotices.Status.Value : 0
     };

Previously I had a "Distinc" in the subquery for the Resp_Dept field, but I removed it.

Any ideas? Thanks in advance for your comments =)

A: 

Can you set the context.log to console.out or some stream so we can get the raw sql it is putting out, this will help us diagnose.

Quintin Robinson
Yes, I can =) Let me do that =)
A: 

This is query I get from the SQL Server profiler:

exec sp_executesql N'SELECT [t0].[FAR_Seq], [t0].[FAR_Year], 
    (CASE 
        WHEN ([t0].[DateTime_Entered]) IS NULL THEN @p3
        ELSE [t0].[DateTime_Entered]
     END) AS [DateTime_Entered], [t0].[Time_Entered], [t0].[OrigDept], [t0].[Part_No], 
    (CASE 
        WHEN ([t0].[DateTime_Analyzed]) IS NULL THEN @p4
        ELSE [t0].[DateTime_Analyzed]
     END) AS [DateTime_Analyzed], [t0].[Analyzed_By], 
    (CASE 
        WHEN (UNICODE([t0].[MDR_Required])) IS NULL THEN @p5
        ELSE CONVERT(NChar(1),[t0].[MDR_Required])
     END) AS [MDR_Required], (
    SELECT [t2].[Resp_Dept]
    FROM (
        **SELECT TOP (1)** [t1].[Resp_Dept]
        FROM [dbo].[FAR_Symptoms] AS [t1]
        WHERE (UNICODE([t1].[FAR_Area_ID]) = UNICODE([t0].[FAR_Area_ID])) AND ([t1].[FAR_Year] = [t0].[FAR_Year]) AND ([t1].[FAR_Seq] 
= [t0].[FAR_Seq])
        ) AS [t2]
    ) AS [Resp_Dept], (
    SELECT [t4].[Sympt_Desc]
    FROM (
        **SELECT TOP (1)** [t3].[Sympt_Desc]
        FROM [dbo].[Symptoms] AS [t3]
        WHERE [t3].[symptom_ID] = @p6
        ) AS [t4]
    ) AS [Sympt_Desc], [t0].[FAR_Area_ID], 
    (CASE 
        WHEN [t0].[Status] IS NOT NULL THEN [t0].[Status]
        ELSE @p7
     END) AS [Status_ID]
FROM [dbo].[FARs] AS [t0]
WHERE (UNICODE([t0].[FAR_Area_ID]) = @p0) AND ([t0].[FAR_Seq] = @p1) AND ([t0].[FAR_Year] = @p2)
ORDER BY [t0].[FAR_Seq]',N'@p0 int,@p1 int,@p2 varchar(2),@p3 datetime,@p4 datetime,@p5 nchar(1),@p6 int,@p7 
int',@p0=76,@p1=7204,@p2='08',@p3=''9999-12-31 23:59:59:997'',@p4=''9999-12-31 23:59:59:997'',@p5=N' ',@p6=0,@p7=0

The only think that I see there that may not in SQL Server 2000 is the '()' in the "Select top..." but I'm not sure if that is what is causing the problem and, also, I don't know how that could be fixed =S

Thanks again =)

A: 

Do you have the latest Service Pack for Visual Studio and the framework?

I just checked some of my Linq generated SQL and it is using "Top 1" correctly against a SQL Server 2000 database.

TGnat
I double checked the generated query and yes, the top is generated without the quotes. Now, what else is wrong? I also checked other queries and they are using the 't0' prefix without problems =(
An other thing: in the server, we do not have the SP1 for the .Net Framework 3.5, so I'm going to try updating that.
A: 

Hello again,

after several testing and review the DB, I found that the problem was a legacy table I was working on: that table has "text" type fields. Also, I had to remove some "Distinct" instructions in a nested query I had.

I found this and, after review that, I found that I have to change my queries and that the "Distinct" instruction does not work correctly. As a side note, let me say that the nested queries can also generate unexpected behavior.

So, the real lesson here is that if you need to deploy this against a SQL Server 2000, set an instance of the server and test against it!!! XD

Thanks a lot of your help =)

For info, there are other known differences - for example, non-trivial paging completely fails sue to the lack of ROW_NUMBER. AFAIK, it isn't officially supported on 2000 even if some simple things work.
Marc Gravell