日期:2014-05-18 浏览次数:20879 次
declare @path sysname; set @path='D:\'; --此为服务器的路径 DECLARE @A sysname; DECLARE @cmd nvarchar(4000); DECLARE cur cursor FOR SELECT DISTINCT A FROM [1222]; OPEN cur; FETCH NEXT FROM cur INTO @A; WHILE @@FETCH_STATUS=0 BEGIN SET @cmd = N'BCP "SELECT * FROM 数据库名.dbo.[1222] WHERE A=''' + @A + '''"'; SET @cmd = @cmd+' queryout ' + @path+@A+'.xls -c -Usa -Ppassword'; EXEC xp_cmdshell @cmd; FETCH NEXT FROM cur INTO @A; END CLOSE cur; DEALLOCATE cur;