DECLARE @U_CHK TABLE (
ACCOUNT_ID NUMERIC
,START_NO NUMERIC
,END_NO NUMERIC
,SEQ_NO NUMERIC
,USED INT
)
DECLARE @ST_NO NUMERIC
,@END_NO NUMERIC
,@ACID NUMERIC
,@SEQ_NO NUMERIC
DECLARE cCurChk Scroll CURSOR
FOR
SELECT DISTINCT AC_ID
,ST_DOC_NO
,ST_DOC_NO
,END_DOC_NO
FROM AC_LVL_INVEN
GROUP BY
ST_DOC_NO
,END_DOC_NO
,AC_ID
OPEN cCurChk
FETCH NEXT FROM cCurChk INTO @ACID,@ST_NO,@SEQ_NO,@END_NO
WHILE @@Fetch_Status=0
BEGIN
WHILE (@SEQ_NO<=@END_NO)
BEGIN
INSERT INTO @U_CHK
VALUES
(
@ACID
,@ST_NO
,@END_NO
,@SEQ_NO
,0
)
SET @SEQ_NO = @SEQ_NO+1
END FETCH NEXT FROM cCurChk
INTO @ACID,@ST_NO,@SEQ_NO,@END_NO
END CLOSE cCurChk DEALLOCATE cCurChk
UpDate @U_CHK
SET USED = 1 FROM @U_CHK Ch INNER JOIN FA_Trans FA(NOLOCK) ON Fa.
Account_ID = Ch.Account_ID AND CONVERT(VARCHAR(10) ,Ch.Seq_No) = Fa.Instrument_No
WHERE FA.Status>4 AND STATUS<>12 AND Ac_Head_Type<>1 AND Trans_type = 'Debit'
答案 0 :(得分:1)
您可以使用merge语句来执行插入/更新来代替游标。这是一个例子
MERGE dbo.FactBuyingHabits AS Target
USING (SELECT CustomerID, ProductID, PurchaseDate FROM dbo.Purchases) AS Source
ON (Target.ProductID = Source.ProductID AND Target.CustomerID = Source.CustomerID)
WHEN MATCHED THEN
UPDATE SET Target.LastPurchaseDate = Source.PurchaseDate
WHEN NOT MATCHED BY TARGET THEN
INSERT (CustomerID, ProductID, LastPurchaseDate)
VALUES (Source.CustomerID, Source.ProductID, Source.PurchaseDate)
查看Technet - Inserting, Updating, and Deleting Data by Using MERGE