0

此查询在 Sql Server Management Studio 中运行良好,但是当我尝试在数据集中运行并从日期时间选择器传递 Date(@Param2 & @Pram3) 时,它抛出异常转换失败。我不知道这有什么问题,我的其他查询工作正常。这是因为 UNION ALL 而发生的吗?

.NET 代码

startDatePicker.Format = DateTimePickerFormat.Short;
endDatePicker.Format = DateTimePickerFormat.Short;
DataTable ntwTable = ntw.GetData(owner,start,end);
start = startDatePicker.Value.ToShortDateString();
end = endDatePicker.Value.ToShortDateString();

SQL

SELECT        o.Name, SUM(sq.SQuantity * sq.SRate) AS TotalSale, SUM(sq.PrQuantity * sq.PrRate) AS TotalPurchase, (SUM(sq.PQuantity) - SUM(sq.SQuantity)) 
                                 * (SUM(sq.PQuantity * sq.PRate) / SUM(sq.PQuantity)) AS Inventory
        FROM            (SELECT        OwnerId, CompanyId, Quantity AS PQuantity, RatePerShare AS PRate, 0 AS SQuantity, 0 AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            Purchase
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, 0 AS PQuantity, 0 AS PRate, 0 AS SQuantity, 0 AS SRate, Quantity AS PrQuantity, RatePerShare AS PrRate, Date
                                  FROM            Purchase AS pr
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, Quantity AS PQuantity, 0 AS PRate, 0 AS SQuantity, 0 AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            Bonus
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, Quantity AS PQuantity, CostOfShare AS PRate, 0 AS SQuantity, 0 AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            RightShare
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, 0 AS PQuantity, 0 AS PRate, Quantity AS SQuantity, RatePerShare AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            Sales) AS sq INNER JOIN
                                 Owner AS o ON o.OwnerId = sq.OwnerId
        WHERE        (o.OwnerId = @Param1) AND (sq.Date BETWEEN @Param2 AND @Param3)
        GROUP BY sq.CompanyId, o.Name
4

3 回答 3

1

在您的代码中,有两种(明显的)失败可能性。

第一个是date列中的隐式转换错误。如果purchase.date是日期/时间值,但 SSMS 中的列之一union all不是: bonus.date、rightshares.date , orsales.date . To be honest, I would be surprised if this is the case. However, it is easy enough to check for. Just run theunion all` 查询以查看是否发生错误,则会发生这种情况。

第二种可能性是@Param1or有问题@Param2。这些似乎是作为字符串传递的。您可以通过查看字符串并执行以下操作来测试它:

select cast(@Param1 as datetime), cast(@Parame2 as datetime)

一个可能的问题是操作系统和数据库级别的不同日期格式。例如,2013 年 1 月 20 日可能是 1 月 20 日,而另一个错误。如果这是问题所在,您可以使用convert()将参数转换为正确的格式(在数据库中)或在传递它们之前对其进行转换。

于 2013-07-02T17:24:59.683 回答
0

我确定这不是最佳答案,但是,这似乎是由于区域设置造成的。如果更改以下两行:

start = startDatePicker.Value.ToShortDateString();
end = endDatePicker.Value.ToShortDateString();

到:

start = startDatePicker.Value.ToString("yyyy-MM-dd HH:mm:ss")
end = endDatePicker.Value.ToString("yyyy-MM-dd HH:mm:ss")

然后我相信这应该可以正常工作。

于 2013-07-02T17:36:28.687 回答
0

(CONVERT(CHAR(10), sq.Date, 111) 在@Param2 和@Param3 之间)

只需要转换。

于 2013-07-02T17:48:29.147 回答