您能否帮我解释批量导入此数据的语法:
startIpNum,endIpNum,country,region,city,postalCode,latitude,longitude,dmaCode,areaCode
1.0.0.0,1.7.255.255,"AU","","","",-27.0000,133.0000,,
1.9.0.0,1.9.255.255,"MY","","","",2.5000,112.5000,,
1.10.10.0,1.10.10.255,"AU","","","",-27.0000,133.0000,,
1.11.0.0,1.11.255.255,"KR","","","",37.0000,127.5000,,
1.12.0.0,1.15.255.255,"CN","","","",35.0000,105.0000,,
1.16.0.0,1.19.255.255,"KR","","","",37.0000,127.5000,,
1.21.0.0,1.21.255.255,"JP","","","",36.0000,138.0000,,
1.22.0.0,1.23.255.255,"IN","","","",20.0000,77.0000,,
1.24.0.0,1.31.255.255,"CN","","","",35.0000,105.0000,,
1.33.0.0,1.33.255.255,"JP","","","",36.0000,138.0000,,
我正在使用它:
set quoted_identifier OFF
drop table #y
drop table #x
DECLARE @servername varchar(128),
@DatabaseName varchar(128), @filepath varchar(500), @pattern varchar(100),
@TableName varchar(128),@query varchar(1000),@max1 int,@count1 int,@filename varchar(100),@fieldTerminator VARCHAR(100),@RowTerminator VARCHAR(100)
SELECT @servername ='SQL',
@DatabaseName ='Test',
@filepath ='C:\test',
@pattern ='*.*',
@TableName ='WeeklyImport'
,@fieldTerminator='''","'''
,@RowTerminator='''"\n"'''
set @count1 =0
create table #x (name varchar(200))
set @query ='master.dbo.xp_cmdshell "dir '+@filepath+@pattern +' /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
set @max1 = (select max(ID) from #y)
--print @max1
--print @count1
While @count1 <= @max1
begin
set @count1=@count1+1
set @filename = (select name from #y where [id] = @count1)
set @Query ='BULK INSERT '+ @Tablename + ' FROM "'+ @Filepath+@Filename+'"
WITH ( FIELDTERMINATOR = ' +@fieldterminator + ',ROWTERMINATOR = ' + @rowterminator + ')'
print @query
exec (@query)
end
drop table #y
答案 0 :(得分:0)
您是否可以访问SQL Server中的SQL导入/导出工具?
对我来说,它在"C:\Program Files\Microsoft SQL Server\100\DTS\Binn\DTSWizard.exe"
。还有其他SO页面,包括Questions about exporting and importing flatfiles (txt, csv) in SQL Server 2005
答案 1 :(得分:0)
我尝试了以下格式文件...
...针对csv文件的前3列:
查询如下:
您现在只需添加所有其他列并在格式文件中指定其格式。如果在插入之前需要修改某些值,可以使用REPLACE
函数。