With BULK INSERT you can upload text, csv data files to the database in any format. Users must have insert privileges in the database and ADMINISTER BULK OPERATIONS privileges on the server level to perform BULK INSERT .
Below you can see the sample usage of BULK INSERT.
First, we create a text file as below.
Then we create a table with the help of the following script to save this text file into the database.
1 2 3 4 | CREATE TABLE [dbo].[Names]( [Name] [varchar](250) NULL, [Surname] [varchar](250) NULL ) ON [PRIMARY] |
Then, with the help of the following script, we transfer the data in this text file to our table in the database.
Explanation of parameters in the script:
DATAFILETYPE is the type of data,
FIELDTERMINATOR is column separator,
ROWTERMINATOR is row separator
1 2 3 4 5 6 7 8 9 10 11 | USE AdventureWorks2012 GO BULK INSERT Names FROM 'C:\Names.txt' WITH ( DATAFILETYPE = 'char', FIELDTERMINATOR = ' ', ROWTERMINATOR = ',', CODEPAGE='1254' ); |
Then we will see the records in the table as follows.
You can use the following article for importing data to SQL Server and exporting data from SQL Server
“How To Import Excel To SQL Server Table“,
“How To Export SQL Server Table To Excel“,
“How To Export SQL Server Table to CSV File“,
“How to Import Images to SQL Server with TSQL Codes“,
“How To Import/Export from SQL Server Database to SQL Server Database“,