我有 12 个位置,每个位置都运行自己的数据库。每个数据库的设置完全相同,都有自己的信息。数据库都是SQL2005或2008服务器。我编写了一个多表查询来获取支付销售佣金的信息。此查询适用于 12 个数据库中的 10 个,其他 2 个数据库不返回任何结果。如果我使用 2 个连接表运行查询(在 2 个数据库上它不起作用)它可以工作,但是其他有问题的表与“where”语句无关。如果它适用于所有其他数据库,任何人都可以解释为什么它不能在 2005 服务器数据库中运行吗?我的代码是:
SELECT Customers.sBarcode AS Barcode, Customers.dtCreated AS Created, CustomerUnlimitedTransactions.sCreatedBy AS [Commission pays],
Surcharges.sDescription AS [Unlimited Package], Surcharges.dblCommission AS [Commission Amount], Users.sUserName AS Greeter, @startdate AS Start_Date,
@enddate AS End_Date
FROM CustomerUnlimitedTransactions INNER JOIN
Customers ON CustomerUnlimitedTransactions.lCustomerId = Customers.lCustomerID INNER JOIN
VIPAccountTypes ON CustomerUnlimitedTransactions.lMonthlyAccountTypeId = VIPAccountTypes.lAccountType INNER JOIN
Surcharges ON VIPAccountTypes.lSurchargeId = Surcharges.Surcharge_ID INNER JOIN
Users ON Customers.lGreeterID = Users.ID_User
WHERE (Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102)) AND
(CustomerUnlimitedTransactions.sTransactionType = N'sale') AND (CustomerUnlimitedTransactions.lMonthlyAccountTypeId = '11') OR
(Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102)) AND
(CustomerUnlimitedTransactions.sTransactionType = N'sale') AND (CustomerUnlimitedTransactions.lMonthlyAccountTypeId = '12')
ORDER BY Created
如果我像这样运行查询,我仍然不会返回任何结果:
SELECT Customers.sBarcode AS Barcode, Customers.dtCreated AS Created, CustomerUnlimitedTransactions.sCreatedBy AS [Commission pays],
Surcharges.sDescription AS [Unlimited Package], Surcharges.dblCommission AS [Commission Amount], Users.sUserName AS Greeter, @startdate AS Start_Date,
@enddate AS End_Date
FROM CustomerUnlimitedTransactions INNER JOIN
Customers ON CustomerUnlimitedTransactions.lCustomerId = Customers.lCustomerID INNER JOIN
VIPAccountTypes ON CustomerUnlimitedTransactions.lMonthlyAccountTypeId = VIPAccountTypes.lAccountType INNER JOIN
Surcharges ON VIPAccountTypes.lSurchargeId = Surcharges.Surcharge_ID INNER JOIN
Users ON Customers.lGreeterID = Users.ID_User
WHERE (Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102))
ORDER BY Created
如果我像这样运行查询,我会得到结果:
SELECT Customers.sBarcode AS Barcode, Customers.dtCreated AS Created, CustomerUnlimitedTransactions.sCreatedBy AS [Commission pays],
Surcharges.sDescription AS [Unlimited Package], Surcharges.dblCommission AS [Commission Amount], Users.sUserName AS Greeter, @startdate AS Start_Date,
@enddate AS End_Date
FROM CustomerUnlimitedTransactions INNER JOIN
Customers ON CustomerUnlimitedTransactions.lCustomerId = Customers.lCustomerID
WHERE (Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102))
ORDER BY Created
没有任何 Null 与上述任何查询相关联。这同样适用于 12 个中的 10 个......是的,数据库完全相同。(如果我在一年的日期范围内运行此查询,我会得到结果)但是我应该仅显示上周的 25 个结果,而我在 2 个中都没有显示任何结果。