Showing posts with label Inserting Data in SQL Server from Text file. Show all posts
Showing posts with label Inserting Data in SQL Server from Text file. Show all posts

Inserting Data in SQL Server from Text file


To insert data from a text file, we have to keep a fully formatted text file with appropriate data’s which are to be inserted.

The query for creating table,
CREATE TABLE Employee
(
   FirstName varchar (100) NOT NULL,
LastName varchar (100) NOT NULL,
Email varchar (100) NOT NULL
)

Then we have a text file with the bulk data’s like as follows,

sibin,thomas,sibinthomask@gmail.com
emp1FirstName,emp1LastName,emp1@company.com
emp2FirstName,emp2LastName,emp2@company.com
emp3FirstName,emp3LastName,emp3@company.com
emp4FirstName,emp4LastName,emp4@company.com
emp5FirstName,emp5LastName,emp5@company.com
emp6FirstName,emp6LastName,emp6@company.com
emp7FirstName,emp7LastName,emp7@company.com
emp8FirstName,emp8LastName,emp8@company.com
emp9FirstName,emp9LastName,emp9@company.com
emp10FirstName,emp10LastName,emp10@company.com
emp11FirstName,emp11LastName,emp11@company.com
emp12FirstName,emp12LastName,emp12@company.com

Put the name of the file as 'EmployeeList.txt'

Now the query for inserting data is,
BULK INSERT Employee FROM 'c:\EmployeeList.txt' WITH (FIELDTERMINATOR = ',')

View the inserted data from table 'Employee',
select * from Employee