日期:2014-05-18 浏览次数:20549 次
DECLARE @dir sysname,@cmd nvarchar(4000); SET @dir = 'C:\'; CREATE TABLE #tmp(filename nvarchar(1024)); SET @cmd = N'dir "' + @dir + '*.csv" /B' INSERT #tmp EXEC master.dbo.xp_cmdshell @cmd; DELETE #tmp WHERE filename IS NULL; IF EXISTS(SELECT * FROM #tmp WHERE filename LIKE '%找不到文件%') BEGIN RAISERROR('找不到文件',16,1) END ELSE BEGIN DECLARE @SQL nvarchar(MAX); SET @SQL = ''; SELECT @SQL = @SQL + 'EXEC xp_cmdshell N''bcp 数据库.dbo.表 in "' + @dir + filename + '" -w -T -t, -r\n'';' + CHAR(13) + CHAR(10) FROM #tmp; EXEC(@SQL); END DROP TABLE #tmp;