網路城邦
上一篇 回創作列表 下一篇   字體:
根據輸入的變數 Loop 分批刪除資料的 SQL
2016/06/30 13:38:13瀏覽586|回應0|推薦0

use AS4_SQ;

Declare @from INT = 0;

Declare @to INT = 0;

select @from = [ZZDAT] from [AS4_SQ].[dbo].[SALZZ] where ZZNAM = 'SALZ1';

select @to = MAX(Z1001) from [AS4_SQ].[dbo].[SALZ1];


if @from < 1000101

BEGIN

SET @from = 1000101;

select @from,count(*) FROM [AS4_SQ].[dbo].[SALZ1] where [Z1001] < @from;

DELETE FROM [AS4_SQ].[dbo].[SALZ1] where [Z1001] < @from;

END;



if @to > 1251299

BEGIN

SET @to = 1251299;

select @to,count(*) FROM [AS4_SQ].[dbo].[SALZ1] where [Z1001] > @to;

DELETE FROM [AS4_SQ].[dbo].[SALZ1] where [Z1001] > @to;

END;


select @to = MAX(Z1001) from [AS4_SQ].[dbo].[SALZ1];

if @from > @to

BEGIN

SET @from = @to;

END; 



select @from,@to;

Declare @rcdcnt INT = 0;

Declare @cnt INT = @from;

WHILE @cnt <= @to

BEGIN

select @rcdcnt=count(*) FROM [AS4_SQ].[dbo].[SALZ1] where [Z1001] = @cnt;

select @cnt,@rcdcnt;

if @rcdcnt > 0

BEGIN

DELETE FROM [AS4_SQ].[dbo].[SALZ1] where [Z1001] = @cnt;

DBCC SHRINKFILE (AS4_SQ_LOG, 1);

END;

SET @cnt = @cnt + 1;

END;


( 知識學習其他 )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=shfm1969&aid=64135162