问题描述
我正在尝试在 sql Server 中执行以下查询,但出现以下错误:
消息 156,级别 15,状态 1,第 5 行
关键字“FROM”附近的语法不正确。
消息 102,级别 15,状态 1,第 122 行
“公司代码”附近的语法不正确。
此外,在 VS2008 中运行它时,出现以下错误:
关键字“from”附近的语法不正确。
'a' 附近的语法不正确。
'b' 附近的语法不正确。
这是代码:
Using SearchDS As New DataSet
Using objDBcommand As New DBCommand
objDBcommand.CommandText = " select a.*,isNull(b.CalculationType,0) as CalculationType," & _
" isnull(b.amountCode,0) as AmountCode,isNull(b.AmountDescription,'') as AmountDescription " & _
" INTO " & strBasetable & _
" from( " objDBcommand.CommandText = " select a.*,'') as AmountDescription " & _
" INTO " & strBasetable & _
" from( "
objDBcommand.CommandText &= " select isnull(VEE.TotalDelaysDuration,0) as TotalDays,isnull(VEE.TCE,0 ) as NetProfitEstimated,Comp.CompanyCode,Comp.Description as CompanyDescription,b.AccountCode,b.AccountDescription," & _
" case " & _
" when (b.AmountDebitBC - b.AmountCreditBC) > 0 then (b.AmountDebitBC - b.AmountCreditBC) " & _
" else " & clsGlobals.NU(0) & _
" end as AmountDebitBC," & _
" case " & _
" when (b.AmountDebitBC - b.AmountCreditBC) < 0 then (b.AmountDebitBC - b.AmountCreditBC) " & _
" else " & clsGlobals.NU(0) & _
" end as AmountCreditBC," & _
" Voy.CharterPartyDate,Voy.VoyageCode,Voy.Description,Voy.TimeCharterInd,Voy.StartDateTime,Voy.LastVoyageTerminationDateTime,H.LastHireDateTime," & _
" CASE when Voy.TimeCharterInd = " & clsGlobals.NU(1) & " then Voy.Hire " & _
" else case when Voy.TimeCharterInd = " & clsGlobals.NU(0) & " then " & _
" case when voy.FreightRate <> " & clsGlobals.NU(0) & " then voy.FreightRate " & _
" else case when voy.TotalFreight <> " & clsGlobals.NU(0) & " then voy.TotalFreight " & _
" else " & clsGlobals.NU(0) & _
" End " & _
" End " & _
" End " & _
" end as Hire," & _
" case when Voy.TimeCharterInd = " & clsGlobals.NU(0) & " then " & _
" case when voy.FreightRate <> " & clsGlobals.NU(0) & " then " & clsGlobals.NU(1) & _
" else case when voy.TotalFreight <> " & clsGlobals.NU(0) & " then " & clsGlobals.NU(2) & _
" else " & clsGlobals.NU(0) & _
" End " & _
" End " & _
" else " & clsGlobals.NU(0) & _
" end ValueFreight," & _
" Voy.Commission,voy.ActualEndDateTime," & _
" BP.Name," & _
" case " & _
" when Voy.lastVoyageTerminationDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & " then (CAST(Voy.StartDateTime AS NUMERIC(9,2)) - CAST(Voy.lastVoyageTerminationDateTime AS NUMERIC(9,2))) " & _
" else 0 " & _
" end " & _
" AS BallastDays," & _
" isnull( case " & _
" when voy.ActualEndDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & " then voy.ActualEndDateTime " & _
" when voy.EstimatedEndDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & " then voy.EstimatedEndDateTime " & _
" End,0) as TerminatedDate," & _
" case " & _
" when voy.ActualEndDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & " then " & clsGlobals.NU(0) & _
" when voy.EstimatedEndDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & " then " & clsGlobals.NU(1) & _
" End as ActualOrEstimated," & _
" CASE when Voy.TimeCharterInd = 1.0 then " & _
" case when voy.ActualEndDateTime = " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & _
" then " & _
" Case when H.LastHireDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & _
" then CAST(H.LastHireDateTime AS NUMERIC(9,2)) - CAST(Voy.StartDateTime AS NUMERIC(9,2)) " & _
" Else " & clsGlobals.NU(0) & " End " & _
" else " & _
" CAST(voy.ActualEndDateTime AS NUMERIC(9,2)) " & _
" End " & _
" else " & _
" case when voy.ActualEndDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & " then " & _
" CAST(Voy.ActualEndDateTime AS NUMERIC(9,2)) - CAST(Voy.StartDateTime AS NUMERIC(9,2))" & _
" else " & _
" case when voy.EstimatedEndDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & " then " & _
" CAST(voy.EstimatedEndDateTime AS NUMERIC(9,2)) - CAST(voy.StartDateTime AS NUMERIC(9,2))" & _
" Else " & _
clsGlobals.NU(0) & _
" End " & _
" End " & _
" end as 'Voyage Period'," & _
" Case " & _
" when Voy.TimeCharterInd = " & clsGlobals.NU(1) & " AND H.LastHireDateTime <> " & clsGlobals.Q(clsGlobals.AD(clsGlobals.gMINDATE)) & _
" Then CAST(H.LastHireDateTime AS NUMERIC(9,2)) - CAST(lastVoyageTerminationDateTime AS NUMERIC(9,2)) " & _
" Else " & clsGlobals.NU(0) & _
" end as 'Hire Collected' " & _
" FROM voyages voy left outer join VoyagesEstimationHeader VEE on VEE.VoyageCode = voy.Voyagecode and VEE.VesselCode = voy.Vesselcode and VEE.EstimationStatus = '3' " & _
" AND VEE.PrevailingInd = (Case when ((select count(*) from VoyagesEstimationHeader where vesselcode = VEE.VesselCode and VoyageCode = VEE.VoyageCode AND EstimationStatus = '3') > 1 ) then 1 " & _
" else (select PrevailingInd from VoyagesEstimationHeader where vesselcode = VEE.VesselCode and VoyageCode = VEE.VoyageCode AND EstimationStatus = '3') end ) " & _
" LEFT OUTER JOIN BusinessPartners BP ON Voy.ChartererCode = BP.BusinessPartnerCode," & clsFunctions.GetActualTableName(Me.Name,"Companies") & " Comp," & _
" ( " & _
" select Fid.AccountCode,Acc.Description as AccountDescription,FID.SubAccountCode,FID.CompanyCode," & _
" sum( Fid.AmountDebitBC)as 'AmountDebitBC',sum(Fid.AmountCreditBC) as 'AmountCreditBC' " & _
" from FiStatementsDetail FID,FiStatementsHeader FIH,"Accounts_1") & " Acc," & _
clsFunctions.GetActualTableName(Me.Name,"Companies") & " Companies " & _
" where Fid.AccountCode = acc.AccountCode " & _
" And Acc.SubAccountType = " & enmSubAccountType.enmVoyage & _
" AND Fid.TransactionType = FiH.TransactionType" & _
" AND Fid.TransactionNum = FiH.TransactionNum " & _
" AND Fid.FiscalYear = FiH.FiscalYear " & _
" AND FiH.TransactionReasonCode NOT IN ( " & strExcludeTransactionReasons & ")" & _
" AND FID.CompanyCode = Companies.CompanyCode "
If dtpDateFrom.Checked = True Then
objDBcommand.CommandText &= " AND FIH.TransactionDate >= " & clsGlobals.Q(clsGlobals.AD(dtpDateFrom.Value.Date))
End If
If dtpDateto.Checked = True Then
objDBcommand.CommandText &= " AND FIH.TransactionDate <= " & clsGlobals.Q(clsGlobals.AD(dtpDateto.Value.Date))
End If
objDBcommand.CommandText &= " group by Fid.AccountCode,Acc.Description,FID.CompanyCode " & _
" ) b," & _
" ( select max(fid.Hiredate)as LastHireDateTime,Fid.subAccountCode,FID.CompanyCode " & _
" FROM fistatementsDetail fid," & _
clsFunctions.GetActualTableName(Me.Name,"Companies") & " Companies " & _
" WHERE fid.CompanyCode = Companies.CompanyCode "
If Not strSelectedCompanies.Equals(String.Empty) Then
objDBcommand.CommandText &= " AND fid.companycode IN ( " & strSelectedCompanies & ")"
End If
objDBcommand.CommandText &= " GROUP BY FID.CompanyCode,Fid.subAccountCode " & _
" ) H " & _
" where Voy.Voyagecode = b.SubAccountCode " & _
" AND b.CompanyCode = Voy.Vesselcode " & _
" AND voy.VesselCode = Comp.Companycode " & _
" AND voy.VesselCode = H.Companycode " & _
" AND H.SubAccountCode = b.SubAccountCode "
If Not strSelectedCompanies.Equals(String.Empty) Then
objDBcommand.CommandText &= " AND voy.VesselCode IN ( " & strSelectedCompanies & ")"
End If
If Not txtVoyage.CodeText.Equals(String.Empty) OrElse txtVoyage.cboSelectedItems.DataSource IsNot nothing Then
objDBcommand.CommandText &= " AND Voy.VoyageCode"
If Not txtVoyage.CodeText.Equals(String.Empty) Then
objDBcommand.CommandText &= " = " & clsGlobals.Q(txtVoyage.CodeText)
Else
objDBcommand.CommandText &= " IN " & clsFunctions.CriteriaValuesFromrowsCollection(CType(txtVoyage.cboSelectedItems.DataSource,DataSet).Tables(0).Rows,txtVoyage.CodeFieldName)
End If
End If
objDBcommand.CommandText &= ")a left outer Join " & _
"( " & _
" select case " & _
" when Links.CalculationType = 1 then 0 " & _
" when Links.CalculationType = 2 then 1 " & _
" else -1 " & _
" end as TimeCharterInd," & _
" Links.CalculationType,Links.amountCode,Links.AmountDescription,Links.AccountCode " & _
" FROM " & _
"( " & _
" select AL.AmountCode,S.AmountDescription,AL.AccountCode,Al.CalculationType " & _
" from sea_VoyageEstimationAmounts s,ActualLinks AL " & _
" where al.AmountCode = s.AmountCode " & _
" )Links " & _
" )b on a.TimeCharterInd = b.TimecharterInd " & _
" and a.AccountCode = b.AccountCode " & _
" order by a.companyCode,a.AccountCode "
objDBcommand.Execute(SearchDS)
这里是 sql 部分:
SELECT a.*,0) AS CalculationType,isnull(b.amountCode,0) AS AmountCode,'') AS AmountDescription INTO
FROM (
SELECT isnull(VEE.TotalDelaysDuration,0) AS TotalDays,0) AS NetProfitEstimated,Comp.Description AS CompanyDescription,CASE
WHEN (b.AmountDebitBC - b.AmountCreditBC) > 0 THEN (b.AmountDebitBC - b.AmountCreditBC)
ELSE 0
END AS AmountDebitBC,CASE
WHEN (b.AmountDebitBC - b.AmountCreditBC) < 0 THEN (b.AmountDebitBC - b.AmountCreditBC)
ELSE 0
END AS AmountCreditBC,Voy.CharterPartyDate,CASE
WHEN Voy.TimeCharterInd = 1 THEN Voy.Hire
ELSE CASE
WHEN Voy.TimeCharterInd = 0 THEN CASE
WHEN voy.FreightRate <> 0 THEN voy.FreightRate
ELSE CASE
WHEN voy.TotalFreight <> 0 THEN voy.TotalFreight
ELSE 0
END
END
END
END AS Hire,CASE
WHEN Voy.TimeCharterInd = 0 THEN CASE
WHEN voy.FreightRate <> 0 THEN 1
ELSE CASE
WHEN voy.TotalFreight <> 0 THEN 2
ELSE 0
END
END
ELSE 0
END ValueFreight,Voy.Commission,BP.Name,CASE
WHEN Voy.lastVoyageTerminationDateTime <> '1900-01-01 00:00:00' THEN (CAST(Voy.StartDateTime AS NUMERIC(9,2)) - CAST(Voy.lastVoyageTerminationDateTime AS NUMERIC(9,2)))
ELSE 0
END AS BallastDays,isnull(CASE
WHEN voy.ActualEndDateTime <> '1900-01-01 00:00:00' THEN voy.ActualEndDateTime
WHEN voy.EstimatedEndDateTime <> '1900-01-01 00:00:00' THEN voy.EstimatedEndDateTime
END,0) AS TerminatedDate,CASE
WHEN voy.ActualEndDateTime <> '1900-01-01 00:00:00' THEN 0
WHEN voy.EstimatedEndDateTime <> '1900-01-01 00:00:00' THEN 1
END AS ActualOrEstimated,CASE
WHEN Voy.TimeCharterInd = 1.0 THEN CASE
WHEN voy.ActualEndDateTime = '1900-01-01 00:00:00' THEN CASE
WHEN H.LastHireDateTime <> '1900-01-01 00:00:00' THEN CAST(H.LastHireDateTime AS NUMERIC(9,2))
ELSE 0
END
ELSE CAST(voy.ActualEndDateTime AS NUMERIC(9,2))
END
ELSE CASE
WHEN voy.ActualEndDateTime <> '1900-01-01 00:00:00' THEN CAST(Voy.ActualEndDateTime AS NUMERIC(9,2))
ELSE CASE
WHEN voy.EstimatedEndDateTime <> '1900-01-01 00:00:00' THEN CAST(voy.EstimatedEndDateTime AS NUMERIC(9,2)) - CAST(voy.StartDateTime AS NUMERIC(9,2))
ELSE 0
END
END
END AS 'Voyage Period',CASE
WHEN Voy.TimeCharterInd = 1
AND H.LastHireDateTime <> '1900-01-01 00:00:00' THEN CAST(H.LastHireDateTime AS NUMERIC(9,2)) - CAST(lastVoyageTerminationDateTime AS NUMERIC(9,2))
ELSE 0
END AS 'Hire Collected'
FROM voyages voy
LEFT OUTER JOIN VoyagesEstimationHeader VEE ON VEE.VoyageCode = voy.Voyagecode
AND VEE.VesselCode = voy.Vesselcode
AND VEE.EstimationStatus = '3'
AND VEE.PrevailingInd = (CASE
WHEN (
(SELECT count(*)
FROM VoyagesEstimationHeader
WHERE vesselcode = VEE.VesselCode
AND VoyageCode = VEE.VoyageCode
AND EstimationStatus = '3') > 1) THEN 1
ELSE
(SELECT PrevailingInd
FROM VoyagesEstimationHeader
WHERE vesselcode = VEE.VesselCode
AND VoyageCode = VEE.VoyageCode
AND EstimationStatus = '3')
END)
LEFT OUTER JOIN BusinessPartners BP ON Voy.ChartererCode = BP.BusinessPartnerCode,Companies Comp,(
SELECT Fid.AccountCode,Acc.Description AS AccountDescription,sum(Fid.AmountDebitBC)AS 'AmountDebitBC',sum(Fid.AmountCreditBC) AS 'AmountCreditBC'
FROM FiStatementsDetail FID,Accounts Acc,Companies Companies
WHERE Fid.AccountCode = acc.AccountCode
AND Acc.SubAccountType = 2
AND Fid.TransactionType = FiH.TransactionType
AND Fid.TransactionNum = FiH.TransactionNum
AND Fid.FiscalYear = FiH.FiscalYear
AND FiH.TransactionReasonCode NOT IN ('YP','YC')
AND FID.CompanyCode = Companies.CompanyCode
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)