Create Table And Put Data In SQL Server Using CSV File

In this article, I will show you how you can create a table and import data from a CSV file into an SQL server. In this process first, we create a CSV file with data and then use that file to create a table.

Points to remember

  • This functionality uses only CSV and TXT files.
  • When creating a CSV file do not leave any blank cells; try to delete them.

Step 1. Create your CSV file with data. Your Header name will use a Table Field Name. You can see in the below image I created a file with the following data.

Employee details

Step 2. Open your SQL Server Management Studio. Here is SQL Server Management Studio V18.6.

Step 3. Create a database if you do not have any.

Step 4. Right-click on your database name then click on Tasks and then click on Import Flat Files.

Tasks

Step 5. Now you see a wizard with some information; click on the Next button.

Introduction

Step 6. In this step, you have three fields. Fill in these fields and then click on the Next button.

  1. Location of your file: Select your CSV file from your computer
  2. New table name: Give your new table name which must be different from other existing tables
  3. Schema: Select your schema.

Specify input table

Step 7. In the next step, you will see a preview of your file data. As you see in the below image we have the same preview as our file data. Now click on the Next button.

Preview data

Step 8. In the next step, you see field names which are the headers of your CSV file. If you want to change a field name you can change it here. You can also give primary key constraint and null constraint from here. After all changes are made, click on the next button.

Modify columns

Step 9. Now you can see imported details. Verify details and then click on the Finish button.

  1. Name: This is the owner name of your computer
  2. Database name: Database name in which you import this file.
  3. Table Name: Your new table name with schema
  4. File to be imported: Your file path

Summary

Step 10. In this step, you see a message that your data is imported. Click on the close button and close the wizard.

Operation complete

Below is the image of the created table design and imported data.

Table design

Table design

Table data

Table data

I hope this article helps you. If so, please share it with your friends.


Similar Articles