classic писал(а) 23. Сентября 2009 :: 14:54:Поделись, если не жалко. А то я совсем в лоб задачу решил. В целом надежно, но долго работает.
Пожалуйста
BEGIN TRAN
SET NOCOUNT ON
SET LOCK_TIMEOUT 500
DECLARE @РезБло smallint
SELECT @РезБло = 1
FROM dbo._1SBKTTLC AS ТабБло WITH (TABLOCKX, HOLDLOCK)
IF @РезБло <> 1
ROLLBACK TRAN
DECLARE @DATE_Ins DATETIME
DECLARE @ACCDTID_Ins CHAR(9)
DECLARE @ACCKTID_Ins CHAR(9)
DECLARE @CURRID_Ins CHAR(9)
DECLARE @KIND_Ins CHAR(1)
DECLARE @OB1_Ins NUMERIC(14, 4)
DECLARE @OB2_Ins NUMERIC(14, 4)
DECLARE @OB3_Ins NUMERIC(14, 4)
DECLARE @DTFLAGS_Ins TINYINT
DECLARE @KTFLAGS_Ins TINYINT
DECLARE TabCur CURSOR FOR
SELECT
DATE
, ACCDTID
, ACCKTID
, CURRID
, KIND
, SUM(OB1) AS OB1
, SUM(OB2) AS OB2
, SUM(OB3) AS OB3
, DTFLAGS
, KTFLAGS
FROM dbo._1SBKTTLC_Ins AS _1SBKTTLC_Ins WITH (TABLOCKX, HOLDLOCK)
GROUP BY
DATE
, ACCDTID
, ACCKTID
, CURRID
, KIND
, DTFLAGS
, KTFLAGS
HAVING
(SUM(OB1) <> 0) OR
(SUM(OB2) <> 0) OR
(SUM(OB3) <> 0)
OPEN TabCur
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM TabCur INTO
@DATE_Ins
, @ACCDTID_Ins
, @ACCKTID_Ins
, @CURRID_Ins
, @KIND_Ins
, @OB1_Ins
, @OB2_Ins
, @OB3_Ins
, @DTFLAGS_Ins
, @KTFLAGS_Ins
UPDATE dbo._1SBKTTLC
SET
OB1 = OB1 + @OB1_Ins,
OB2 = OB2 + @OB2_Ins,
OB3 = OB3 + @OB3_Ins,
DTFLAGS = @DTFLAGS_Ins,
KTFLAGS = @KTFLAGS_Ins
WHERE
(DATE = @DATE_Ins) AND
(KIND = @KIND_Ins) AND
(ACCKTID = @ACCKTID_Ins) AND
(ACCDTID = @ACCDTID_Ins) AND
(CURRID = @CURRID_Ins)
IF @@ROWCOUNT = 0 BEGIN
INSERT INTO dbo._1SBKTTLC VALUES
(
@DATE_Ins, @ACCDTID_Ins, @ACCKTID_Ins, @CURRID_Ins, @KIND_Ins, @OB1_Ins, @OB2_Ins, @OB3_Ins, @DTFLAGS_Ins, @KTFLAGS_Ins
)
END
END
CLOSE TabCur
DEALLOCATE TabCur
DELETE FROM dbo._1SBKTTLC_Ins
COMMIT TRAN
SET NOCOUNT OFF
Никаких гарантий, что это правильно и будет быстрее.