2010-09-21 44 views
0

雖然試圖通過將視圖與其他兩個表加入來從視圖中提取數據,但出現以下錯誤:「SQL Server無法處理NText的比較,文本,Xml或圖像數據類型「。嘗試加入linq-to-sql中的視圖時出錯

這裏是查詢:

var expeditions = from VE in context.ViewExpeditions 
         join SIAGR in context.SiteInAdviseGoodsRef on VE.DeliveryNotes equals SIAGR.Value 
         join SIA in context.SiteInAdvise on SIAGR.SiteInAdviseId equals SIA.Id 
         where SIA.Id == SiteInAdviseID 
         select VE; 

這裏是查看ViewExpeditions:

SELECT ve.*, S.[$Refex] as SiteRefex,c.[$Refex] as ServiceRefex , DeliveryNotes = LEFT(o.list, LEN(o.list)-1),st.Status,st.StatusRefex 
from(
SELECT  
B.[$Id] AS Booking, 
B.[$Refex] AS BookingRefex, 
B.SiguxCPUE AS CartaDePorte, 
SUM(ISNULL(BG.Weight, 0)) AS Weight, 
SUM(ISNULL(BG.Volume, 0)) AS Volume, 
ISNULL(SUBSTRING(B.Comments, 1, 100),'') AS Comments, 
'Type' = 
     CASE 
     WHEN PickupSite IS NULL THEN 'Pickup' 

     ELSE 'Delivery' 
     END, 
'Name' = 
     CASE 
     WHEN PickupSite IS NULL THEN PickupName 

     ELSE DeliveryName 
     END, 
     'City' = 
     CASE 
     WHEN PickupSite IS NULL THEN PickupCity 

     ELSE DeliveryCity 
     END, 
'PostalCode' = 
     CASE 
     WHEN PickupSite IS NULL THEN PickupPostalCode 

     ELSE DeliveryPostalCode 
     END, 

'ContactName' = 
     CASE 
     WHEN PickupSite IS NULL THEN ISNULL(B.PickupContactName,'') 

     ELSE ISNULL(B.DeliveryContactName,'') 
     END, 
'ContactPhone' = 
     CASE 
     WHEN PickupSite IS NULL THEN ISNULL(B.PickupContactPhone,'') 

     ELSE ISNULL(B.DeliveryContactPhone,'') 
     END, 



B.PickupDate AS DataExp, 
B.DeliveryDate, 
coalesce(B.PickupSite,B.DeliverySite) as 'Site', 
b.Service,isnull(B.SiguxState,0) as SiguxState 

FROM   dbo.BookingGoods AS BG 
INNER JOIN dbo.Booking AS B ON BG.BookingId = B.[$Id] 

WHERE  (B.ExecutedBy = 2) AND B.SiguxCPUE is not null AND B.[$IsDeleted]=0 
GROUP BY B.PickupCity,b.PickupContactName,b.PickupContactPhone,b.PickupName,b.PickupPostalCode, B.DeliveryName,B.DeliverySite, B.DeliveryCity, B.DeliveryPostalCode, B.DeliveryContactName, B.DeliveryContactPhone, B.PickupDate, B.[$Id], B.SiguxCPUE, B.[$Refex], B.Comments,b.PickupSite,B.Service,B.SiguxState,B.DeliveryDate 

) ve 
INNER join [ViewBookingActualStatus] st on st.Booking=ve.Booking 
INNER join Service c on ve.Service=c.[$Id] 
INNER JOIN dbo.Site AS S ON S.[$Id] = ve.Site 
outer APPLY 
( 
     SELECT distinct 
      CONVERT(VARCHAR(12), dbo.BookingGoodsRef.Value) + ', ' AS [text()] 
     FROM 
      dbo.BookingGoodsRef 
     WHERE 
      dbo.BookingGoodsRef.BookingId = ve.Booking and BookingGoodsRef.Type=13 

     FOR XML PATH('') 
    ) o (list) 

我要去哪裏錯了呢?

注意:如果我嘗試運行linqpad查詢,它並沒有給我在所有的任何錯誤,並生成以下SQL:

SELECT [t0].[Booking], [t0].[BookingRefex], [t0].[CartaDePorte], [t0].[Weight], [t0].[Volume], [t0].[Comments], [t0].[Type], [t0].[Name], [t0].[City], [t0].[PostalCode], [t0].[ContactName], [t0].[ContactPhone], [t0].[DataExp], [t0].[DeliveryDate], [t0].[Site], [t0].[Service], [t0].[SiguxState], [t0].[SiteRefex], [t0].[ServiceRefex], [t0].[DeliveryNotes], [t0].[Status], [t0].[StatusRefex] 
FROM [ViewExpeditions] AS [t0] 
INNER JOIN [SiteInAdviseGoodsRef] AS [t1] ON [t0].[DeliveryNotes] = [t1].[Value] 
INNER JOIN [SiteInAdvise] AS [t2] ON [t1].[SiteInAdviseId] = [t2].[$Id] 
WHERE [t2].[$Id] = @p0 

回答

0

如果從LinqPad複製生成的SQL進入SSMS並嘗試運行它,你會得到你期望的結果嗎?

我猜想你加入的一列(可能是DeliveryNotes)是ntexttext。 SQL Server無法加入text列 - 必須將兩列都轉換爲varchar或在兩列上都使用substring

+0

我很笨,忘記在字段DeliveryNotes上執行.ToString()。這樣做後,它運行得非常好! – Hallaghan 2010-09-21 16:30:40