Search this insane blog:

Sunday, August 3, 2008

rapidly extract CSV file information into SQL Server



-- in the end,
i will be able to query a file straight from sql server:



-- ** select * from
txt_delimited...book1#csv ** if the
file name is book1.csv i saved a text delimited file in a folder txt_del on my
desktop. you can drag and drop any file into that particular folder and do a
quick-query.



-- note: if you
super-tweaked your database collation, your collation must be case insensitive!



declare


-------------------------------------------------------------------








@nameoflinkedfiletype sysname,

@describemyfile nvarchar(256),
@provider nvarchar(256),
@myfilefolder
nvarchar(4000),

@extrafileinfo
nvarchar(4000),

@sqlserverloginame sysname



-------------------------------------------------------------------------------------



set @nameoflinkedfiletype =
'txt_del'

set @describemyfile
= 'text'


set @provider
= 'microsoft.jet.oledb.4.0'


set @myfilefolder
= 'c:\users\[username]\desktop\txt_del'


set @extrafileinfo
= 'text;hdr=yes;fmt=delimited'


set @sqlserverloginame =
'machine name\login'

exec
master.dbo.sp_addlinkedserver


@server
= @nameoflinkedfiletype,

@srvproduct = @describemyfile,

@provider = @provider,

@datasrc
= @myfilefolder,

@provstr
= @extrafileinfo


-- add a linked server login.... etc..

exec
master.dbo.sp_addlinkedsrvlogin
@rmtsrvname = @nameoflinkedfiletype,
@locallogin = @sqlserverloginame,
@useself = 'true'


-- then from
now on, you can simply type a select on whatever file you have on that folder!!!
likek ..

SELECT verse
INTO poetry
FROM txt_del...Book1#csv AS Book1#csv_1


No comments:

Post a Comment