Sql Server脚本继续运行

时间:2016-06-02 13:43:23

标签: sql-server loops while-loop sql-insert

我正在编写一个sql server迁移脚本,将所有剪切的日历复制到sd日历,并进行一些小的修改。我写了以下脚本

WHILE (SELECT COUNT(*) FROM CUT_CALENDAR) > 0
BEGIN
    DECLARE @ID INT

    SELECT TOP 1 @ID = ID FROM CUT_CALENDAR

    INSERT INTO SD_CALENDAR  (NAME, CALENDAR_ROLE_ID,DESCRIPTION,USE_IN_CUTOFF,PTY_ID,LOCK_CODE,DATA_OWNER_ID,CNT_ID,NON_BUSINESS_DAYS_MONDAY,NON_BUSINESS_DAYS_TUESDAY,NON_BUSINESS_DAYS_WEDNESDAY,NON_BUSINESS_DAYS_THURSDAY,NON_BUSINESS_DAYS_FRIDAY,NON_BUSINESS_DAYS_SATURDAY,NON_BUSINESS_DAYS_SUNDAY,CCY_ID,CODE,VERSION,SEAL,LST_UPD_TS)
    SELECT NAME, CALENDAR_TYP_ID,DESCRIPTION,'Y',PTY_ID,LOCK_CODE,DATA_OWNER_ID,CNT_ID,NON_BUSINESS_DAYS_MONDAY,NON_BUSINESS_DAYS_TUESDAY,NON_BUSINESS_DAYS_WEDNESDAY,NON_BUSINESS_DAYS_THURSDAY,NON_BUSINESS_DAYS_FRIDAY,NON_BUSINESS_DAYS_SATURDAY,NON_BUSINESS_DAYS_SUNDAY,CCY_ID,CODE,VERSION,SEAL,LST_UPD_TS
    FROM CUT_CALENDAR
    WHERE ID = @ID;

    DELETE FROM CUT_CALENDAR WHERE ID = @ID
END
GO

CUT_CALENDAR包含+ - 30条记录

但是当我运行这个脚本时,它会继续运行并且我会收到: java.lang.OutOfMemoryError:超出GC开销限制

我认为这表明我已经无限循环,但我看不出如何。 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

对于“基于设置”的方法,请尝试以下方法:

INSERT INTO sd_calendar (Columns)
SELECT
   (Columns)
FROM
   cut_calendar

DELETE FROM cut_calendar

没有必要一个接一个地做,只会减慢SQL速度。你正在做一个直接复制,然后从原始表中删除,所以使用批量操作,如上所述,你可以减少循环。如果您试图影响所有内容,请不要添加where子句。