我有一笔交易:
BEGIN TRANSACTION
OPEN cursor
FETCH NEXT FROM cursor INTO @i
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO BigTable () VALUES ()
WAITFOR DELAY '00:01:00';
FETCH NEXT FROM cursor INTO @i
END
COMMIT;
问题是,该事务锁定了BigTable
整个循环和WAITFOR DELAY
时间。
如何防止锁定,表格仅在INSERT
期间被锁定?
答案 0 :(得分:1)
请在光标处添加“FOR LOCAL STATIC READ_ONLY FORWARD_ONLY”:
DECLARE MY_CURSOR CURSOR FOR LOCAL STATIC READ_ONLY FORWARD_ONLY
SELECT columnname...
FROM table-name
BEGIN TRANSACTION
OPEN cursor
FETCH NEXT FROM cursor INTO @i
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO BigTable () VALUES ()
WAITFOR DELAY '00:01:00';
FETCH NEXT FROM cursor INTO @i
END
COMMIT;
答案 1 :(得分:1)