mssql动态查询实体没有获取值

时间:2015-03-23 09:00:34

标签: sql-server

你好我的动态查询和我测试过的这个程序正在运行。 但是不将数据带到服务器端(实体)

visual studio 2012 框架4.5 实体商店程序

 public IEnumerable<spGetInvoiceDetailSearch_Result> GetInvoiceDetailedSearch(InvoiceModel item)
    {
        return DALContext.GetInvoiceDetailedSearch(item);
    }


ALTER PROCEDURE [dbo].[spGetInvoiceDetailSearch] @InvoiceItemID INT
    ,@InvoiceTypeID INT
    ,@VesselID INT
    ,@PaidBy NVARCHAR(50)
    ,@InvoiceNo NVARCHAR(50)
    ,@CompanyID INT
    ,@InvoiceFromDate DATE
    ,@InvoiceToDate DATE
    ,@FromDueDate DATE
    ,@ToDueDate DATE
    ,@FromAmount DECIMAL(18, 4)
    ,@ToAmount DECIMAL(18, 4)
    ,@DueDateType NVARCHAR(50)
AS
BEGIN
    DECLARE @SQLQuery AS NVARCHAR(4000)

    SELECT @SQLQuery = 
        'SELECT        dbo.Invoices.InvoiceID, dbo.Invoices.CompanyID, dbo.Invoices.VesselID, dbo.Invoices.InvoiceNo, dbo.Invoices.DueDate, dbo.Invoices.Amount, 
                         dbo.Invoices.Comment, dbo.Invoices.IsPaid, dbo.Invoices.PaymentDate, dbo.Invoices.PaidBy, dbo.Invoices.Period, dbo.Invoices.InvoiceDate, 
                         dbo.Invoices.InvoiceCurrencyCode, dbo.Invoices.InvoiceAmount, dbo.Invoices.IsReceived, dbo.Invoices.IsProforma, dbo.Invoices.InvoiceTypeID, 
                         dbo.Invoices.IsDeleted, dbo.Invoices.Parity, dbo.Invoices.DueDateType, dbo.Vessels.Name AS VesselName, dbo.InvoiceVsInvoiceItems.ItemPrice as ItemPrice, 
                         dbo.InvoiceVsInvoiceItems.InvoiceItemID as InvoiceItemID, dbo.InvoiceVsInvoiceItems.VAT as VAT, dbo.InvoiceVsInvoiceItems.ItemType as ItemType, dbo.InvoiceItems.Name AS InvoiceItemName, 
                         dbo.Companies.Name AS CompanyName, dbo.InvoiceTypes.Name AS InvoiceTypeName
FROM            dbo.Invoices LEFT OUTER JOIN
                         dbo.Companies ON dbo.Invoices.CompanyID = dbo.Companies.CompanyID LEFT OUTER JOIN
                         dbo.InvoiceTypes ON dbo.Invoices.InvoiceTypeID = dbo.InvoiceTypes.InvoiceTypeID LEFT OUTER JOIN
                         dbo.InvoiceVsInvoiceItems ON dbo.Invoices.InvoiceID = dbo.InvoiceVsInvoiceItems.InvoiceID LEFT OUTER JOIN
                         dbo.InvoiceItems ON dbo.InvoiceVsInvoiceItems.InvoiceVsInvoiceItemID = dbo.InvoiceItems.InvoiceItemID LEFT OUTER JOIN
                         dbo.Vessels ON dbo.Invoices.VesselID = dbo.Vessels.VesselID WHERE
                         dbo.Invoices.IsDeleted != 1 
                         and dbo.Vessels.IsDeleted != 1 
                         and dbo.Companies.IsDeleted != 1  '

    SET FMTONLY OFF

    IF @InvoiceItemID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.InvoiceItems.InvoiceItemID= ''' + CAST(@InvoiceItemID AS NVARCHAR(50)) + ''''
    END
      IF @InvoiceTypeID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.InvoiceTypeID= ''' + CAST(@InvoiceTypeID AS NVARCHAR(50)) + ''''
    END
      IF @VesselID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.VesselID= ''' + CAST(@VesselID AS NVARCHAR(50)) + ''''
    END
      IF @PaidBy IS NOT NULL
    BEGIN
        SET @SQLQuery = @SQLQuery + 'AND dbo.Invoices.PaidBy =  ''' + CAST(@PaidBy AS NVARCHAR(50)) + ''''
    END
      IF @InvoiceNo IS NOT NULL
    BEGIN
        SET @SQLQuery = @SQLQuery + 'AND dbo.Invoices.InvoiceNo = ''' + CAST(@InvoiceNo AS NVARCHAR(50)) + ''''
    END
      IF @CompanyID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.CompanyID =  ''' + CAST(@CompanyID AS NVARCHAR(50)) + ''''
    END
      IF @FromAmount IS NOT NULL AND @ToAmount IS NOT NULL      
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.Amount BETWEEN ''' + CAST(@FromAmount AS NVARCHAR(100)) + ''' AND ''' + CAST(@ToAmount AS NVARCHAR(100)) + ''''
    END
      IF @DueDateType IS NOT NULL
    BEGIN
        SET @SQLQuery = @SQLQuery + 'AND dbo.Invoices.DueDateType = ''' + CAST(@DueDateType AS NVARCHAR(50)) + ''''
    END
      IF @InvoiceFromDate IS NOT NULL AND @InvoiceToDate IS NOT NULL

    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.InvoiceDate Between ''' + CAST(@InvoiceFromDate AS NVARCHAR(100)) + ''' AND ''' + CAST(@InvoiceToDate AS NVARCHAR(100)) + ''''
    END
      IF @FromDueDate IS NOT NULL AND @ToDueDate IS NOT NULL

    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.DueDate Between ''' + CAST(@FromDueDate AS NVARCHAR(100)) + ''' AND ''' + CAST(@ToDueDate AS NVARCHAR(100)) + ''''
    END

    EXECUTE (@SQLQuery)
END

并结束问题

我的表日期类型:日期格式但是 服务器显示它像datetime我该怎么做才能将它更改为日期格式.. 谢谢 问候

2 个答案:

答案 0 :(得分:0)

ALTER PROCEDURE [dbo].[spGetInvoiceDetailSearch] @InvoiceItemID INT
    ,@InvoiceTypeID INT
    ,@VesselID INT
    ,@PaidBy NVARCHAR(50)
    ,@InvoiceNo NVARCHAR(50)
    ,@CompanyID INT
    ,@InvoiceFromDate DATE
    ,@InvoiceToDate DATE
    ,@FromDueDate DATE
    ,@ToDueDate DATE
    ,@FromAmount DECIMAL(18, 4)
    ,@ToAmount DECIMAL(18, 4)
    ,@DueDateType NVARCHAR(50)
AS
BEGIN
    DECLARE @SQLQuery AS NVARCHAR(4000)

    SELECT @SQLQuery = 
        'SELECT        dbo.Invoices.InvoiceID, dbo.Invoices.CompanyID, dbo.Invoices.VesselID, dbo.Invoices.InvoiceNo, dbo.Invoices.DueDate, dbo.Invoices.Amount, 
                         dbo.Invoices.Comment, dbo.Invoices.IsPaid, dbo.Invoices.PaymentDate, dbo.Invoices.PaidBy, dbo.Invoices.Period, dbo.Invoices.InvoiceDate, 
                         dbo.Invoices.InvoiceCurrencyCode, dbo.Invoices.InvoiceAmount, dbo.Invoices.IsReceived, dbo.Invoices.IsProforma, dbo.Invoices.InvoiceTypeID, 
                         dbo.Invoices.IsDeleted, dbo.Invoices.Parity, dbo.Invoices.DueDateType, dbo.Vessels.Name AS VesselName, dbo.InvoiceVsInvoiceItems.ItemPrice as ItemPrice, 
                         dbo.InvoiceVsInvoiceItems.InvoiceItemID as InvoiceItemID, dbo.InvoiceVsInvoiceItems.VAT as VAT, dbo.InvoiceVsInvoiceItems.ItemType as ItemType, dbo.InvoiceItems.Name AS InvoiceItemName, 
                         dbo.Companies.Name AS CompanyName, dbo.InvoiceTypes.Name AS InvoiceTypeName
FROM            dbo.Invoices LEFT OUTER JOIN
                         dbo.Companies ON dbo.Invoices.CompanyID = dbo.Companies.CompanyID LEFT OUTER JOIN
                         dbo.InvoiceTypes ON dbo.Invoices.InvoiceTypeID = dbo.InvoiceTypes.InvoiceTypeID LEFT OUTER JOIN
                         dbo.InvoiceVsInvoiceItems ON dbo.Invoices.InvoiceID = dbo.InvoiceVsInvoiceItems.InvoiceID LEFT OUTER JOIN
                         dbo.InvoiceItems ON dbo.InvoiceVsInvoiceItems.InvoiceVsInvoiceItemID = dbo.InvoiceItems.InvoiceItemID LEFT OUTER JOIN
                         dbo.Vessels ON dbo.Invoices.VesselID = dbo.Vessels.VesselID WHERE
                         dbo.Invoices.IsDeleted != 1 
                         and dbo.Vessels.IsDeleted != 1 
                         and dbo.Companies.IsDeleted != 1  '

    SET FMTONLY OFF

    IF @InvoiceItemID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.InvoiceItems.InvoiceItemID= ' + CAST(@InvoiceItemID AS NVARCHAR(50)) + ''
    END
      IF @InvoiceTypeID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.InvoiceTypeID= ' + CAST(@InvoiceTypeID AS NVARCHAR(50)) + ''
    END
      IF @VesselID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.VesselID= ' + CAST(@VesselID AS NVARCHAR(50)) + ''
    END
      IF @PaidBy IS NOT NULL
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.PaidBy =  ''' + CAST(@PaidBy AS NVARCHAR(50)) + ''''
    END
      IF @InvoiceNo IS NOT NULL
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.InvoiceNo = ''' + CAST(@InvoiceNo AS NVARCHAR(50)) + ''''
    END
      IF @CompanyID > 0
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.CompanyID =  ' + CAST(@CompanyID AS NVARCHAR(50)) + ''
    END
      IF @FromAmount IS NOT NULL AND @ToAmount IS NOT NULL      
    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.Amount BETWEEN ''' + CAST(@FromAmount AS NVARCHAR(100)) + ''' AND ''' + CAST(@ToAmount AS NVARCHAR(100)) + ''''
    END
      IF @DueDateType IS NOT NULL
    BEGIN
        SET @SQLQuery = @SQLQuery + '  AND dbo.Invoices.DueDateType = ''' + CAST(@DueDateType AS NVARCHAR(50)) + ''''
    END
      IF @InvoiceFromDate IS NOT NULL AND @InvoiceToDate IS NOT NULL

    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.InvoiceDate Between ''' + CAST(@InvoiceFromDate AS NVARCHAR(100)) + ''' AND ''' + CAST(@InvoiceToDate AS NVARCHAR(100)) + ''''
    END
      IF @FromDueDate IS NOT NULL AND @ToDueDate IS NOT NULL

    BEGIN
        SET @SQLQuery = @SQLQuery + ' AND dbo.Invoices.DueDate Between ''' + CAST(@FromDueDate AS NVARCHAR(100)) + ''' AND ''' + CAST(@ToDueDate AS NVARCHAR(100)) + ''''
    END

    PRINT (@SQLQuery)
END

首先,调试它非常简单。用打印替换EXEC(@SQLQUery),然后你会看到你的实际查询。

你有一些sintax错误(某些地方,AND在前面错过了一个空格),而且你还有一些被视为字符串的整数。

尝试我的更新程序。

答案 1 :(得分:0)

似乎正在调用过程但没有返回任何行,要调试确切的问题,您可以编写实际的硬编码查询,返回1个或多个记录而不是动态查询。 所以在这之后有两种可能性

  1. 通过edmx进行的过程调用会返回数据,这意味着参数值会导致一些问题。

  2. 不会返回任何数据。

  3. 要解决任何问题,您需要检查通过Enitity Framework调用SP时生成的相应sql查询。