我有一个大型存储过程,其中包含一个游标和一些舍入。当我运行存储过程并查看分配列时,数字有时会改变一美元,而不会对SQL代码进行任何更改。因此,我可以运行查询,检查记录并查看$ 10300几次,然后它将切换到$ 10299然后返回等。它在所有记录中也不一致。每次运行程序时,准确或关闭的记录都可以是任意组合。
似乎完全是武断的,在我使用SQL的这些年里,我从来没有见过这样的事情。我的猜测是它的四舍五入,但我仍然期望它每次都以相同的方式工作。我要感激任何想法或想法。谢谢!
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spGetDatabase]
-- Add the parameters for the stored procedure here
@FY as CHAR(4),
@Obj_det as CHAR(3),
@Agy_Obj as CHAR(2),
@ArchiveId as INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @temp TABLE ( FY CHAR(4),
PCA CHAR(5),
Index_No CHAR(4),
Obj_det CHAR(3),
Agy_obj CHAR(2),
Appro_sym CHAR(3),
Fund_src CHAR(1),
Fund_No CHAR(4),
Method CHAR(1),
Project CHAR(6),
[Char] CHAR(1),
Allotment NUMERIC(14,0))
DECLARE @RecordId as INTEGER,
@CurrentFY CHAR(4),
@CurrentPCA CHAR(5),
@CurrentIndex_No CHAR(4),
@CurrentObj_det CHAR(3),
@CurrentAgy_obj CHAR(2),
@CurrentAppro_sym CHAR(3),
@CurrentFund_src CHAR(1),
@CurrentFund_No CHAR(4),
@CurrentMethod CHAR(1),
@CurrentProject CHAR(6),
@CurrentChar CHAR(1),
@CurrentAllotment NUMERIC(14,0),
@CurrentSumAllotment NUMERIC(14,0),
@CurrentPcainAllotment NUMERIC(14,0),
@PreviousRecordId as INTEGER,
@PreviousFY CHAR(4),
@PreviousPCA CHAR(5),
@PreviousIndex_No CHAR(4),
@PreviousObj_det CHAR(3),
@PreviousAgy_obj CHAR(2),
@PreviousFund_src CHAR(1),
@PreviousFund_No CHAR(4),
@PreviousMethod CHAR(1),
@PreviousProject CHAR(6),
@PreviousChar CHAR(1),
@PreviousAllotment NUMERIC(14,0),
@PreviousSumAllotment NUMERIC(14,0),
@PreviousPcainAllotment NUMERIC(14,0),
@getUplfile CURSOR
SET @getUplfile = CURSOR FOR
SELECT (ROW_NUMBER()
OVER (ORDER BY FY,
PCA,
Index_No,
Obj_Det,
Agy_Obj) )as RowNumber,
FY,
PCA,
Index_No,
obj_det,
agy_obj,
Appro_sym,
Fund_src,
Fund_No,
Method,
Project,
[Char],
Allotment,
SumAllotment,
PcainAllotment
FROM (SELECT a.fy,
a.pca,
a.index_no,
a.obj_det,
a.agy_obj,
a.appro_sym,
a.fund_src,
a.fund_no,
a.method,
a.project,
a.[char],
a.ALLOTMENT,
c.allotment as SumAllotment,
b.Allotment as PcainAllotment
FROM (SELECT tblpcain.fy,
tblpcain.pca,
tblpcain.index_no,
tblpcain.obj_det,
tblpcain.agy_obj,
tblpcafl.appro_sym,
tblpcafl.fund_src,
tblpcafl.fund_no,
tblpcafl.method,
tblpcafl.project,
tblappro.[char],
ROUND(SUM(CASE
WHEN tblpcain.obj_det = @Obj_det AND
tblpcain.agy_obj = @Agy_Obj AND
tblpcain.pca between '05000' AND '05999' THEN CASE
WHEN tblpcafl.fund_src = 'F' THEN tblpcain.allotment * tblpcafl.W_PNCT
ELSE 0
END
ELSE tblpcain.allotment * tblpcafl.pnct
END),2) as Allotment
FROM tblpcain,
tblpcafl,tblappro
WHERE tblpcain.fy + tblpcain.pca = tblpcafl.fy + tblpcafl.pca AND
tblpcafl.fy + tblpcafl.appro_sym = tblappro.fy + tblappro.appro_sym AND
tblpcain.fy = @FY AND
tblpcain.archiveid = @ArchiveId AND
tblpcafl.archiveid = @ArchiveId AND
tblappro.archiveid = @ArchiveId
GROUP BY tblpcain.fy,
tblpcain.pca,
tblpcain.index_no,
tblpcain.obj_det,
tblpcain.agy_obj,
tblpcafl.appro_sym,
tblpcafl.fund_src,
tblpcafl.fund_no,
tblpcafl.method,
tblpcafl.project,
tblappro.[char]) as a LEFT JOIN
tblpcain as b ON a.fy = b.fy AND a.index_no = b.index_no AND a.pca = b.pca AND a.obj_det = b.obj_det AND a.agy_obj = b.agy_obj LEFT JOIN
(SELECT fy,
pca,
index_no,
obj_det,
agy_obj,
SUM(allotment) as allotment
FROM (SELECT tblpcain.fy,
tblpcain.pca,
tblpcain.index_no,
tblpcain.obj_det,
tblpcain.agy_obj,
tblpcafl.appro_sym,
tblpcafl.fund_src,
tblpcafl.fund_no,
tblpcafl.method,
tblpcafl.project,
tblappro.[char],
SUM(ROUND(CASE
WHEN tblpcain.obj_det = @Obj_Det AND
tblpcain.agy_obj = @Agy_Obj AND
tblpcain.pca between '05000' AND '05999' THEN CASE
WHEN tblpcafl.fund_src = 'F' THEN tblpcain.allotment * tblpcafl.W_PNCT
ELSE 0
END
ELSE tblpcain.allotment * tblpcafl.pnct
END,0)) as Allotment
FROM tblpcain,
tblpcafl,tblappro
WHERE tblpcain.fy + tblpcain.pca = tblpcafl.fy + tblpcafl.pca AND
tblpcafl.fy + tblpcafl.appro_sym = tblappro.fy + tblappro.appro_sym AND
tblpcain.fy = @Fy AND
tblpcain.archiveid = @ArchiveId AND
tblpcafl.archiveid = @ArchiveId AND
tblappro.archiveid = @ArchiveId
GROUP BY tblpcain.fy,
tblpcain.pca,
tblpcain.index_no,
tblpcain.obj_det,
tblpcain.agy_obj,
tblpcafl.appro_sym,
tblpcafl.fund_src,
tblpcafl.fund_no,
tblpcafl.method,
tblpcafl.project,
tblappro.[char]) as MyData
GROUP BY fy,
pca,
index_no,
obj_det,
agy_obj) as c ON a.fy = c.fy AND a.index_no = c.index_no AND a.pca = c.pca AND a.obj_det = c.obj_det AND a.agy_obj = c.agy_obj) As Data
OPEN @getUplfile
FETCH NEXT FROM @getUplfile
INTO @RecordId,
@CurrentFY,
@CurrentPCA,
@CurrentIndex_No,
@CurrentObj_det,
@CurrentAgy_obj,
@CurrentAppro_sym,
@CurrentFund_src,
@CurrentFund_No,
@CurrentMethod,
@CurrentProject,
@CurrentChar,
@CurrentAllotment,
@CurrentSumAllotment,
@CurrentPcainAllotment
WHILE @@FETCH_STATUS = 0
BEGIN
If @CurrentSumAllotment <> @CurrentPcainallotment AND
(@CurrentFY <> @PreviousFY OR
@CurrentPCA <> @PreviousPCA OR
@CurrentIndex_No <> @PreviousIndex_No OR
@CurrentObj_Det <> @PreviousObj_Det OR
@CurrentAgy_Obj <> @PreviousAgy_Obj)
BEGIN
SET @CurrentAllotment = @CurrentAllotment + (@CurrentPcainAllotment - @CurrentSumAllotment)
END
SET @PreviousFY = @CurrentFY
SET @PreviousPCA = @CurrentPCA
SET @PreviousIndex_No = @CurrentIndex_No
SET @PreviousObj_det = @CurrentObj_Det
SET @PreviousAgy_obj = @Currentagy_obj
INSERT INTO @Temp ( FY,
PCA,
Index_No,
Obj_det,
Agy_obj,
Appro_sym,
Fund_src,
Fund_No,
Method,
Project,
[Char],
Allotment)
SELECT @CurrentFY,
@CurrentPCA,
@CurrentIndex_No,
@CurrentObj_det,
@CurrentAgy_obj,
@CurrentAppro_sym,
@CurrentFund_src,
@CurrentFund_No,
@CurrentMethod,
@CurrentProject,
@CurrentChar,
@CurrentAllotment
FETCH NEXT FROM @getUplfile
INTO @RecordId,
@CurrentFY,
@CurrentPCA,
@CurrentIndex_No,
@CurrentObj_det,
@CurrentAgy_obj,
@CurrentAppro_sym,
@CurrentFund_src,
@CurrentFund_No,
@CurrentMethod,
@CurrentProject,
@CurrentChar,
@CurrentAllotment,
@CurrentSumAllotment,
@CurrentPcainAllotment
END
CLOSE @getUplfile
DEALLOCATE @getUplfile
SELECT FY,
PCA,
Index_No,
Obj_det,
Agy_obj,
Appro_sym,
Fund_src,
Fund_No,
Method,
Project,
[Char],
Allotment
FROM @Temp
ORDER BY FY,
PCA,
Index_no,
Obj_det,
Agy_obj
END