Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Drop a Column
- How to Rename a Column
- How to Add a Default Value to a Column
- How to Remove a Default Value to a Column
- How to Add a NOT NULL Constraint
- How to Remove a NOT NULL Constraint
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Alter Sequence
- How to Create an Index
Dates and Times
Analysis
- How to use SQL Pivot
- How to Query JSON Object
- How to Calculate Cumulative Sum/Running Total
- How to Have Multiple Counts
- How to Write a Case Statement
- How to Use Coalesce
- How to Avoid Gaps in Data
- How to Import a CSV
- How to Get First Row Per Group
- How to Compare Two Values When One is NULL
- How to Write a Common Table Expression
- How to Calculate Percentiles
- How to Do Type Casting
How to Import a CSV in SQL Server
CSV files are a common format for storing and exchanging data. In this tutorial, we’ll explore how to efficiently import CSV files into SQL Server using various methods, including T-SQL, SQL Server Management Studio (SSMS), and SQL Server Integration Services (SSIS).
Method 1: Using SQL Server Management Studio (SSMS)
SQL Server Management Studio (SSMS) offers an easy way to import CSV files into a database using its import wizard. Here's how you can do it:
- Open SSMS and connect to your SQL Server instance.
- Right-click on the database where you want to import the CSV file.
- Select Tasks >Import Data... to launch the Import Data wizard.
- Choose Flat File Source as your data source and browse to the CSV file.
- Configure your destination (SQL Server Database) and select the appropriate table or create a new one.
- Follow the wizard steps to map the columns and finish the import process.
Method 2: Using T-SQL with BULK INSERT
If you prefer a script-based approach, you can use the BULK INSERT
command in T-SQL to load data from a CSV file directly into a SQL Server table. Here’s the syntax:
BULK INSERT your_table_name
FROM 'C:\path_to_your_file.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2
);
In this example:
FIELDTERMINATOR
specifies the delimiter (comma for CSV).ROWTERMINATOR
indicates the end of each row (new line character).FIRSTROW
skips the header row in the CSV file.
Method 3: Using SQL Server Integration Services (SSIS)
SQL Server Integration Services (SSIS) is a more powerful tool for managing complex data import tasks. It provides a GUI to create ETL (Extract, Transform, Load) workflows that can handle large data imports with advanced features. To use SSIS, follow these steps:
- Launch SQL Server Data Tools.
- Create a new SSIS project.
- Drag a Data Flow Task into the control flow area.
- Configure the source to read from your CSV file and the destination to write to your SQL Server database.
- Run the SSIS package to import the data.
Conclusion
Importing a CSV file into SQL Server is straightforward and can be accomplished using multiple methods depending on your needs and preferences. Whether you're using the SSMS import wizard for simplicity, T-SQL for scripting, or SSIS for more complex workflows, you now have the tools to efficiently load your data into SQL Server.