分批导出数据库记录

declare @i int
declare @cmd varchar(1000)
declare @st int
declare @xst int
set @i=0
while @i <=530
begin
select top 1 @st=[MailId] From [TGdb].[dbo].[EmailList] order by [MailId]
select top 1 @xst=[MailId] From [TGdb].[dbo].[EmailList] where [MailId]>@st+@i*1000 order by [MailId]
set @cmd='bcp "SELECT TOP 1000 [Email] FROM [TGdb].[dbo].[EmailList] where [MailId]>'+cast(@xst as varchar(10))+' order by [MailId]" queryout D:\导出Email\'+cast(@i as varchar(10))+'.txt -c -S WWW-026C0691FA2 -U sa -P 123'
EXEC master..xp_cmdshell @cmd
set @i=@i+1
end
原文地址:https://www.cnblogs.com/toosuo/p/1783626.html