In this article I will show you how you can create a table and import data from a csv file into sql server. In this process first we create a csv file with data and then use that file for creating 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.
Step 2
Open your SQL Server Management Studio. Here 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.
Step 5
Now you see a wizard with some information; click on Next button.
Step 6
In this step you have three fields. Fill these fields and then click on Next button.
- Location of your File: Select your CSV file from your computer
- New Table Name: Give your new table name which must be different from other existing tables
- Schema: Select your schema.
Step 7
In the next step you will see a preview of your file data. As you see in the below image we have same preview as our file data. Now click on Next button.
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 nameyou can change it here. You can also give primary key constraint and null constraint from here. After all changes are made, click on next button.
Step 9
Now you can see imported details. Verify details and then click on Finish button.
- Name: This is owner's name of your computer
- Database name: Database name in which you import this file.
- Table Name: Your new table name with schema
- File to be imported: Your file path
Step 10
In this step you see a message that your data is imported. Click on close button and close the wizard.
Below is the image of created table design and imported data.
Table Design
Table Data
I hope this article helped you. If so, please share with your friends.