Skip to content

Incorrect translation of query to SQL with CASE WHEN ... END #634

@giuliohome

Description

@giuliohome

Description

I'm trying to fix an issue in my project and I need to avoid duplicate records with a more advanced join condition.

 query {
     for c in context.OilPhysical.EndurCost do
     join n in context.OilPhysical.EndurNominationValid
         on ( (c.CargoId, c.DeliveryId, c.DealNumber) = 
             (n.CargoId, n.DeliveryId, if (n.DeliveryDealNumber>0) then n.DeliveryDealNumber else n.ReceiptDealNumber) )
     where (c.BookingCompany = book &&
         c.FeeStatus <> Cost.ClosedFeeStatus &&
         c.FeeType <> delete_type &&
             ( c.CounterpartyId = i_str ||
               c.FeeId = i || c.CargoId = i )
         )
     take 90
     select c
 }

Repro steps

I've activated the sql logging (and I'm testing my library via f# interactive) against the effective db where the issue has been originated.

#if INTERACTIVE
FSharp.Data.Sql.Common.QueryEvents.SqlQueryEvent 
|> Event.add (fun e -> 
    printfn  "Executing SQL: %s" (e.ToRawSql()))
#endif

The problem is that there are 3 errors in the generated SQL

SELECT TOP 90 [c].[CalculationVolume] as '[c].[CalculationVolume]',[c].[CargoID] as '[c].[CargoID]',[c].[CounterpartyID] as '[c].[CounterpartyID]',
[c].[CounterpartyLongName] as '[c].[CounterpartyLongName]',[c].[CounterpartyShortName] as '[c].[CounterpartyShortName]',[c].[Currency] as '[c].[Currency]',
[c].[DealNumber] as '[c].[DealNumber]',[c].[DeliveryID] as '[c].[DeliveryID]',[c].[Fee] as '[c].[Fee]',[c].[FeeID] as '[c].[FeeID]',[c].[FeePV] as '[c].[FeePV]',
[c].[FeeStatus] as '[c].[FeeStatus]',[c].[FeeType] as '[c].[FeeType]',[c].[LastUpdate] as '[c].[LastUpdate]',[c].[OneTimePaymentDate] as '[c].[OneTimePaymentDate]',
[c].[OperationType] as '[c].[OperationType]',[c].[ParcelID] as '[c].[ParcelID]',[c].[PayRec] as '[c].[PayRec]',[c].[PriceUnit] as '[c].[PriceUnit]',
[c].[TransactionNumber] as '[c].[TransactionNumber]',[c].[UserID] as '[c].[UserID]',[c].[VolumeCalculationType] as '[c].[VolumeCalculationType]',
[c].[booking_company] as '[c].[booking_company]' 
FROM [OilPhysical].[EndurCost] as [c] 
INNER JOIN  [OilPhysical].[EndurNominationValid] as [n] on [c].[CargoID] = [n].[CargoID] AND [c].[DeliveryID] = [n].[DeliveryID] 
AND [c].[DealNumber] = CASE WHEN ([c].[DeliveryDealNumber] > 0) THEN [n].[DeliveryDealNumber] ELSE [c].[ReceiptDealNumber] ENDWHERE ((([c].[FeeType] <> 'Deleted' AND ([c].[booking_company] = 'US_ETS INC - LE' AND [c].[FeeStatus] <> '2-CLOSED')) 
AND ([c].[CargoID] = 20052 OR ([c].[CounterpartyID] = '20052' OR [c].[FeeID] = 20052))))
  1. the key END is attached to the key WHERE

  2. ([c].[DeliveryDealNumber] > 0) should be ([n].[DeliveryDealNumber] > 0)

  3. ELSE [c].[ReceiptDealNumber] sohuld be ELSE [n].[ReceiptDealNumber]

Expected behavior

Correct sql and no exception.

Actual behavior

There is an exception

Error System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlException: Incorrect syntax near 'ENDWHERE'.
Incorrect syntax near 'c'.
   at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__180_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'ENDWHERE'.
Incorrect syntax near 'c'.
   at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__180_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
ClientConnectionId:3915fd7a-2a64-4d97-860f-3d98587c5d3f
Error Number:102,State:1,Class:15<---

Known workarounds

At the moment I'm not aware of any known workarounds and this is urgent.

Related information

  • Used database: SQL Server 2016
  • Operating system Windows Server 2016
  • .NET Framework 4.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions