SQL Server is very popular in Relational Database, because of its versatility in exporting data in Excel/CSV/JSON formats. This feature helps with the portability of data across multiple databases. In this article, let us see how to import and export SQL Server Data to a CSV file. Azure data studio is a very useful tool for export options and the best part is it can run on Windows/Linux/Mac Operating systems.
Method 1: Exporting data to Excel via Azure DataStudio
Step 1: We should have a database to proceed further. Let us keep ‘GEEKSFORGEEKS’ as the database name.
Query:
-- Check whether 'GEEKSFORGEEKS' exists and if it is there drop it DROP DATABASE IF EXISTS GEEKSFORGEEKS; --Command to create database: - CREATE DATABASE <dbname>; Create Database GEEKSFORGEEKS: Query: --Make the database active USE GEEKSFORGEEKS;
Step 2: Adding tables to the database
Query:
CREATE TABLE [Address]( [AddressID] [int] IDENTITY(1,1) NOT NULL, [AddressLine1] [nvarchar](60) NOT NULL, [AddressLine2] [nvarchar](60) NULL, [City] [nvarchar](30) NOT NULL, [StateProvinceID] [int] NOT NULL, [PostalCode] [nvarchar](15) NOT NULL, PRIMARY KEY (AddressID)) GO
Step 3: Insertion of records to table Address. It shows two different ways of insertion of data.
Query:
INSERT into Address(AddressLine1,City,StateProvinceID,PostalCode) values ('Address1,','Chennai',1,600028), -- 1st row of data ('Address2','Mumbai',2,400029), -- 2nd row of data ('Address3','Kolkata',3,700027), --3rd row ('Address4','Delhi',4,110999) -- 4th row GO --This will insert 1 row INSERT into Address(AddressLine1,City,StateProvinceID,PostalCode) values ('Address5,','Madurai',1,625010); GO --Next row insertion INSERT into Address(AddressLine1,City,StateProvinceID,PostalCode) values ('Address6','Pune',2,411062) GO --Next row insertion INSERT into Address(AddressLine1,City,StateProvinceID,PostalCode) values ('Address7','Hoogly',3,712501) GO SELECT * from Address -- Display the inserted records
Output:
Now the above query can be exported to a CSV file using azure studio easily in the below way:
- First select option save as CSV.
- This will easily provide the option to save the file as CSV and the contents are shown in the below image.
- We can see that the output got exported to CSV by means of a comma-separated manner.
Azure datastudio makes the ways so easier. We have to fetch the data by means of Select query and easily it can be viewed as a CSV file.
Method 2: Using SQL Server Management Studio :
Step 1: Select database>>Tools>> options in SQL Server Management Studio.
Step 2: Next, under the Options, we can select the output format.
Output:
Hence exporting of data in CSV is done. By default, it will show the output in a grid pattern. So. both Azure data studio and SQL Server Management studio help in best to export data to CSV.