DECLARE @SQL_BULK VARCHAR(MAX)
SET @SQL_BULK = 'BULK INSERT [dbo].[Table]
FROM ''' + @BatchFileName + '''
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ''\t'',
ROWTERMINATOR = ''0x0a''
)'
EXEC (@SQL_BULK)
我有这个代码来进行批量加载。工作正常,但我希望@BatchFileName
也作为列(每行包含相同的值)。
这是否可以在批量加载期间?或者我如何在以后的单独功能中添加它?
提前致谢
答案 0 :(得分:2)
我使用此脚本在目录中迭代和bulkinsert文件:
create table #x (name varchar(200))
DECLARE @query varchar (1000),@conta int ,@query2 varchar (1000),@NOME varchar(50)
set @conta=1
set @query ='master.dbo.xp_cmdshell "dir '+'C:\directoryname'+'*.csv' +' /b"'
insert #x exec (@query)
delete from #x where name is NULL
select identity(int,1,1) as ID, name into #y from #x
drop table #x
WHILE @conta<221 --number of files
BEGIN
SELECT @NOME=name FROM #y WHERE ID=@conta
set @Query2 ='BULK INSERT [dbo].[tablename] FROM '+ '''C:\directoryname'+@NOME+'''
WITH (
FIELDTERMINATOR = '','',ROWTERMINATOR = ''0x0a'')'
SELECT @query2
exec (@Query2)
set @conta=@conta+1
END
drop table #y