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.
- 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.
![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.
- Name: This is the owner 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
![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.