多个INNER JOIN SQL Server Compact Edition

时间:2012-02-21 16:33:59

标签: sql vb.net inner-join multiple-tables

我试图在SQL Server CE中的单个查询中进行多个连接。我知道SQL Server CE不支持多个SELECT,但我找不到有关多个INNER JOIN的任何信息。我一直在ProjectItemMaster(在FROM之后)和第一次INNER JOIN上获得令牌错误。警告这是一个用于生成报告的丑陋查询。

GetCutSheetDataByCustPnumTagQuery定义为:

SELECT ReportCalculations.ItemNumber, ReportCalculations.PartNumber,
       ReportCalculations.calcWidth, ReportCalculations.calcHeight, 
       ReportCalculations.calcQuantity, ReportCalculations.Description,
       ReportCalculations.PrintonCutSheet, ProjectItemMaster.Quantity, 
       ProjectItemMaster.HingeDirection, ProjectItemMaster.ItemDescription,
       ProjectItemMaster.MetalFinish, ProjectItemMaster.Width, 
       ProjectItemMaster.Height, ProjectItemMaster.CustomerID, 
       CustomerMaster.CustomerId AS Id_CM, ProjectItemMaster.GlassType,  
       ProjectItemMaster.Tag AS Expr1, ReportCalculations.Tag, 
       ProjectItemMaster.ItemNumber AS Expr2, ReportCalculations.CalcX, 
       ReportCalculations.CalcY, ProjectItemMaster.OpeningWidth,
       ProjectItemMaster.ReturnDirection, ProjectItemMaster.PanelDirection, 
       ProjectItemMaster.ReturnWidth, ProjectItemMaster.ButtressHeight,
       ProjectItemMaster.ButtressWidth, ProjectItemMaster.AvailThickness, 
       ProjectItemMaster.PanelThickness, ProjectItemMaster.Image,
       ProjectItemMaster.SoftwareVersion, ProjectItemMaster.DatabaseVersion, 
       ProdlineMaster.Series, ProdlineMaster.Report, ProjectItemMaster.Addons,
       ProjectItemMaster.PanelWidth, CustomerMaster.CustomerName, 
       ProjectMaster.WO, ProjectMaster.PO, ProdlineMaster.SeriesName, 
       ReportCalculations.Series AS Expr3
FROM ProjectItemMaster 
INNER JOIN CustomerMaster 
ON ProjectItemMaster.CustomerID = CustomerMaster.CustomerId 
INNER JOIN ReportCalculations 
ON ProjectItemMaster.Tag = ReportCalculations.Tag 
AND ProjectItemMaster.CustomerID = ReportCalculations.CustomerID 
AND ProjectItemMaster.ProjectNumber = ReportCalculations.ProjectNumber 
INNER JOIN ProdlineMaster 
ON ReportCalculations.Series = ProdlineMaster.Series 
INNER JOIN ProjectMaster 
ON CustomerMaster.CustomerId = ProjectMaster.CustomerId 
AND ProjectItemMaster.ProjectNumber = ProjectMaster.ProjectNumber
WHERE (ReportCalculations.PrintonCutSheet = 'Y') 
AND (ProjectItemMaster.ProjectNumber = @ProjectNumber) 
AND (CustomerMaster.CustomerId = @CustomerID) 
AND (ProjectItemMaster.CustomerID = @CustomerID) 
AND (ProjectItemMaster.Tag = @TAG)

这是在SQL Compact数据库上执行VB.NET中的查询的功能。

Public Function getCutSheetInfobyCustProdTag(ByRef customerID As String, 
                                             ByRef projectNumber As Integer, 
                                             ByVal tag As String) 
                                      As System.Data.DataTable

    Dim mydata As New DataTable
    GetCutSheetDataByCustPnumTagQuery.Parameters.Clear()
    GetCutSheetDataByCustPnumTagQuery.Parameters.AddWithValue("@ProjectNumber", 
                                                                 projectNumber)
    GetCutSheetDataByCustPnumTagQuery.Parameters.AddWithValue("@CustomerID", 
                                                                    customerID)
    GetCutSheetDataByCustPnumTagQuery.Parameters.AddWithValue("@TAG", tag)

    Try
       splConnection.Open()
       Dim reader As SqlCeDataReader = GetCutSheetDataByCustPnumTagQuery.ExecuteReader
       mydata.Load(reader)
    Catch ex As Exception
       MessageBox.Show("Problem with reportcalculations table", "Error", 
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
       ' Return False
    Finally
       splConnection.Close()
    End Try
    'Return True
    Return mydata

End Function

如果我不能使用多个INNER JOIN,我可以将查询分解为多个不同的查询,但我不愿意。因此,维护SQL Server CE和程序的SQL Server版本更容易。

1 个答案:

答案 0 :(得分:2)

当您提供没有明确说明其类型和长度的Query参数时,显然SQL Compact不喜欢它。该查询一直试图将CustomerID“987654”转换为数字。我认为这是因为将值硬编码到查询中使得它运行良好但是当我使用@CustomerID时,程序抛出了一个预期。因此,从parameters.addwithvalue(Name,Value)切换到Parameters.add(Name,Type,Length)解决了这些问题。

 Public Function getCutSheetInfobyCustProdTag(ByRef customerID As String, ByRef projectNumber As Integer, ByVal tag As String) As System.Data.DataTable

        Dim mydata As New DataTable
        GetCutSheetDataByCustPnumTagQuery.Parameters.Clear()
        GetCutSheetDataByCustPnumTagQuery.Parameters.Add("@ProjectNumber", System.Data.SqlDbType.Int)
        GetCutSheetDataByCustPnumTagQuery.Parameters("@ProjectNumber").Value = projectNumber
        GetCutSheetDataByCustPnumTagQuery.Parameters.Add("@CustomerID", System.Data.SqlDbType.NVarChar, 25)
        GetCutSheetDataByCustPnumTagQuery.Parameters("@CustomerID").Value = customerID
        GetCutSheetDataByCustPnumTagQuery.Parameters.Add("@TAG", System.Data.SqlDbType.NVarChar, 50)
        GetCutSheetDataByCustPnumTagQuery.Parameters("@TAG").Value = tag

        Try
            splConnection.Open()
            Dim reader As SqlCeDataReader = GetCutSheetDataByCustPnumTagQuery.ExecuteReader
            mydata.Load(reader)
        Catch ex As Exception
            MessageBox.Show("Problem with reportcalculations table", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally
            splConnection.Close()
        End Try

        Return mydata
    End Function

感谢@ X-Zero的帮助