If you need to import data from a file, this can be achieved by using SQL Bulk Insert command. Recently I did use this method to import some 500 000 data from few comma separated value (CSV) files and thought to share it with you. What you need to remember is, if you are importing data from more than one file the data should have the same format through out the files.
This is a sample set of data I imported into my table from the file named File1.csv.
Login,Name,Date,Result,Pass
U0001,Roman Silva,1/10/2010 17:23,100,TRUE
U0002,Anthony Don,28/09/2010 10:01,70,TRUE
U0003,Saman Perera,16/09/2010 11:31,90,TRUE
U0004,Silvia Raz,26/09/2010 22:11,40,FALSE
U0005,Rebecca Maine,18/09/2010 11:30,100,TRUE
I used the following script to create a temporary table for my imported data.
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[TABLENAME_ImportedData](
- [LoginId] [NVARCHAR](50) NOT NULL,
- [Name] [NVARCHAR](200) NULL,
- [Date] [DATETIME] NULL,
- [Result] [INT] NULL,
- [Pass] [NVARCHAR](8) NULL
- ) ON [PRIMARY]
- GO
To fetch data from the file I used the following script.
- BULK INSERT [TABLENAME_ImportedData]
- FROM 'D:\DataFiles\File1.csv'
- WITH (
- FIELDTERMINATOR =',', -- Since my columns are seperated using commas (,).
- ROWTERMINATOR ='\n', -- Since each data row is in its own line.
- FIRSTROW = 2 -- Since my first row is having column names. Please note FIRSTROW is not recommended to skip the column names.
- )
While running the script I faced an issue with the date since my server was set to US English as the default language. In US English the dates should be in MDY format. So I had 3 choices, either to change the date formats on my data files, change the server default language to another language which has its date format as DMY or change the date format of the server. I used the easy way to change the date format of the server by running the following command.
- SET DATEFORMAT dmy
If you like to change SQL server default language and need help please read my article on that.
1 comment:
bookmarked!!, I love your site!
Also visit my web site ... home equity loans in
Post a Comment