Đây là một followup cho một câu hỏi tôi hỏi ngày hôm qua:proc lưu trữ cho tập kết quả khác với TSQL, chỉ có trên một số máy chủ
Have you ever had SQL Server 2008 return a different result set than SQL Server 2000?
nơi tôi ban đầu nghĩ rằng các thủ tục lưu trữ đã được đưa ra kết quả differnt trên SQL2000 so với sql2008, nhưng tôi đã thực hiện thu hẹp hơn một chút vấn đề và loại bỏ khá nhiều mã để làm cho nó trở thành một vấn đề đơn giản/có thể tái sản xuất. Tóm tắt là, một phần của TSQL khi chạy như một proc trả về một câu trả lời khác nhau mà cùng một chút mã chạy như chỉ TSQL, nhưng chỉ trên máy chủ khách hàng của tôi, không phải trên một trong các máy chủ thử nghiệm của tôi.
Khi tôi chạy TSQL này:
DECLARE @PropertyID int
DECLARE @PortfolioID int
DECLARE @StartDate datetime
DECLARE @EndDate datetime
DECLARE @AcctMethod tinyint
SET @PropertyId=3555
--SET @PortfolioId = null
SET @StartDate= '3/1/2010'
SET @EndDate='2/28/2011'
SET @AcctMethod=1
DECLARE @ErrorMsg varchar(70)
DECLARE @ExclAcct tinyint
SET NOCOUNT ON
CREATE TABLE #IncomeStatement (
PropertyID int,
GLAccountID int,
SubTotalAccountID int,
Debits money,
Credits money,
YTDDebits money,
YTDCredits money,
PZDebits money,
PZCredits money,
AccountType tinyint
)
--Initialize Temporary Table
INSERT INTO #IncomeStatement(PropertyID, GLAccountID, SubTotalAccountID, AccountType, Debits, Credits, YTDDebits, YTDCredits, PZDebits, PZCredits)
SELECT PropertyID, ID, SubTotalAccountID, AccountType, 0, 0, 0, 0, 0, 0
FROM ChartOfAccounts
WHERE (PropertyID = @PropertyID OR @PropertyID Is Null)
AND (@PortfolioID is null OR PropertyID in (select PropertyID from PortfolioProperty where [email protected]))
AND (Category > 3 or CashFlowCode <> 0)
--Period Activity
IF @AcctMethod = 1
SET @ExclAcct = 0
ELSE
SET @ExclAcct = 1
UPDATE Bal
SET
Debits = Debits + D.TotDebit,
Credits = Credits + D.TotCredit
FROM #IncomeStatement Bal
INNER JOIN (SELECT GLAccountID, Sum(Debit) AS TotDebit, Sum(Credit) AS TotCredit
FROM GLTransaction GT
WHERE (GT.PropertyID = @PropertyID OR @PropertyID Is Null)
AND AccountingMethod <> @ExclAcct
AND Posted = 1
AND TranDate >= @StartDate
AND TranDate <= @EndDate
GROUP BY GLAccountID) AS D
ON BAL.GLAccountID = D.GLAccountID
select * from #IncomeStatement where GLAccountID=11153
drop table #IncomeStatement
tôi nhận được một số lượng thẻ ghi nợ của $ 124,27, tuy nhiên, khi tôi quay đoạn code trên vào một thủ tục lưu trữ như thế này:
CREATE Procedure [dbo].[sp_test]
@PropertyID int = Null,
@PortfolioID int = Null,
@StartDate datetime = Null,
@EndDate datetime = Null,
@AcctMethod tinyint = 1
AS
DECLARE @ErrorMsg varchar(70)
DECLARE @ExclAcct tinyint
SET NOCOUNT ON
CREATE TABLE #IncomeStatement (
PropertyID int,
GLAccountID int,
SubTotalAccountID int,
Debits money,
Credits money,
YTDDebits money,
YTDCredits money,
PZDebits money,
PZCredits money,
AccountType tinyint
)
--Initialize Temporary Table
INSERT INTO #IncomeStatement(PropertyID, GLAccountID, SubTotalAccountID, AccountType, Debits, Credits, YTDDebits, YTDCredits, PZDebits, PZCredits)
SELECT PropertyID, ID, SubTotalAccountID, AccountType, 0, 0, 0, 0, 0, 0
FROM ChartOfAccounts
WHERE (PropertyID = @PropertyID OR @PropertyID Is Null)
AND (@PortfolioID is null OR PropertyID in (select PropertyID from PortfolioProperty where [email protected]))
AND (Category > 3 or CashFlowCode <> 0)
--Period Activity
IF @AcctMethod = 1
SET @ExclAcct = 0
ELSE
SET @ExclAcct = 1
UPDATE Bal
SET
Debits = Debits + D.TotDebit,
Credits = Credits + D.TotCredit
FROM #IncomeStatement Bal
INNER JOIN (SELECT GLAccountID, Sum(Debit) AS TotDebit, Sum(Credit) AS TotCredit
FROM GLTransaction GT
WHERE (GT.PropertyID = @PropertyID OR @PropertyID Is Null)
AND AccountingMethod <> @ExclAcct
AND Posted = 1
AND TranDate >= @StartDate
AND TranDate <= @EndDate
GROUP BY GLAccountID) AS D
ON BAL.GLAccountID = D.GLAccountID
select * from #IncomeStatement where GLAccountID=11153
drop table #IncomeStatement
và sau đó thực hiện nó loke:
EXEC sp_test @PropertyID=3555, @StartDate='03/01/2010', @EndDate='02/28/2011'
Tôi nhận được số tiền ghi nợ là 248,54 đô la, gấp đôi số tiền đó.
Tôi thực sự bối rối. Điều odder, là nếu tôi sao lưu cơ sở dữ liệu này, và sau đó sao chép nó vào một trong hai máy chủ win2003 của tôi chạy sql2000, hoặc máy chủ win2008 của tôi chạy SQL2008R2, nó hoạt động chính xác trong cả hai trường hợp. Vì vậy, nó có vẻ như nó là một máy chủ hoặc thiết lập cơ sở dữ liệu đó gây ra vấn đề, nhưng đã hết những thứ để kiểm tra - hy vọng một bộ mắt mới có thể chỉ ra một cái gì đó rõ ràng tôi đang mất tích.
Hành vi lạ, lạ. – alex
Điều đó thực sự kỳ quặc. Bạn có thể cho biết liệu bạn có bắt đầu với cùng số lượng bản ghi trong #IncomeStatement trong cả hai trường hợp không? Ngoài ra, bạn đã thử sử dụng biến bảng thay vì bảng tạm thời chưa? Có lẽ có một thiết lập trên tempdb trên máy chủ khách hàng đang gây ra một số sự kỳ lạ ở đây. – rsbarro
Bạn đã kiểm tra xem SET ANSI_NULLS có tạo sự khác biệt không? http://msdn.microsoft.com/en-us/library/ms188048.aspx –