INSERT INTO A2 SELECT DISTINCT e,f,g FROM (SELECT e,f,g,COUNT(*) fc FROM A1 GROUP BY e,f,g) a WHERE fc>(SELECT COUNT(*) FROM A1 WHERE e=15 GROUP BY e)*0.1 TRUNCATE TABLE A1
IF @M%2=0 INSERT INTO A3 SELECT DISTINCT e,f,g FROM (SELECT e,f,g,COUNT(*) fc FROM A2 GROUP BY e,f,g) a WHERE fc>2
SET @s='EXEC master..xp_cmdshell '' bcp " IF @M%2=0 SELECT e,f,g FROM AM..A2 WHERE e!=150 " queryout D:\SCMd'+ltrim(@M)+'.txt -c -t, -U -Ppassword''' EXEC(@s)
IF @M%2=0 TRUNCATE TABLE A2
SET @M=@M+1 END
------解决方案-------------------- SET @s='EXEC master..xp_cmdshell '' bcp " IF @M%2=0 SELECT e,f,g FROM AM..A2 WHERE e!=150 " queryout D:\SCMd'+ltrim(@M)+'.txt -c -t, -U -Ppassword''' EXEC(@s)