循环中导出文本文件的问题
各位大虾,我想利用变量@M为条件,从表A2导出文本文件,可是什么也导不出来,连个空文本文件也没有,请大虾们帮我看看是怎么回事,先谢谢!
DECLARE @M INT,@s NVARCHAR(1000),
SELECT @M=0
WHILE @M<=12
BEGIN
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)
主要检查这一句,你PRINT出来看看先
[解决办法]
--导出文本文件EXEC master..xp_cmdshell 'bcp dbname..tablename out c:\DT.txt -c -Sservername -Usa -Ppassword'--或EXEC master..xp_cmdshell 'bcp "Select * from dbname..tablename" queryout c:\DT.txt -c -Sservername -Usa -Ppassword'--导出到TXT文本,用逗号分开exec master..xp_cmdshell 'bcp "库名..表名" out "d:\tt.txt" -c -t ,-U sa -P password'
[解决办法]
SET @s='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 master..xp_cmdshell @s
bu huan hang