How To Generate Script Of Table With Data In SQL Server

Recently, we were working on an SQL Server database migration project. For the critical tables, we generated scripts, including their data, which made our job easy during migration activities. In this article, I will walk you through all the steps to generate the script for the table with data in SQL Server in real-time.

How To Generate Script Of Table With Data In SQL Server

To generate script of table with data in SQL server, Follow the below steps.

1. Connect to the SQL server management studio (SSMS).

2. Expand the database folder and select the database that your table belongs to.

3. Right-click on the Database name –> Tasks –> then click on the Generate Scripts option, as shown in the screenshot below.

Generate Script Of Table With Data In SQL Server

4. Click on the Next button.

generate table script with data in sql server 2019

Check out: How To Create A Table With Identity Column In SQL Server

5. Choose the Select specific database objects option, select the particular table, and click the Next button.

generate database script with table data from sql server 2019

6. Select Open in the new query window option and then click on the Advanced option, as shown in the screenshot below.

how to generate script of table with data in sql server

7. In the Advanced scripting options, select the Types of data to the script as Schema and data, and then click on the OK button and click the Next button on the main window, as shown in the screenshot below.

how to generate table script with data in sql server using query

Check out How To Create A User In SQL Server Database

8. Click on the Next button on the Summary page.

generate table script with data in sql server using query

9. Finally, click on the Finish button.

How To Generate Script Of Table With Data In SQL

Check out How to Alter Table Column to Allow NULL in SQL Server

Now, as shown in the screenshot below, the new query was generated in the new query window.

Generate Database Script with Table Data From SQL Server
USE [Test]
GO
/****** Object:  Table [dbo].[MyProduct]    Script Date: 19-11-2024 13:57:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MyProduct](
	[SalesID] [int] IDENTITY(1,1) NOT NULL,
	[ProductSaleDate] [datetime] NOT NULL,
	[CustomerID] [int] NULL,
	[ProductID] [int] NULL,
	[TotalQuantity] [int] NULL,
	[TotalAmount] [decimal](10, 2) NULL
) ON [ps_MyProductSalePerYear]([ProductSaleDate])
GO
SET IDENTITY_INSERT [dbo].[MyProduct] ON 
GO
INSERT [dbo].[MyProduct] ([SalesID], [ProductSaleDate], [CustomerID], [ProductID], [TotalQuantity], [TotalAmount]) VALUES (1, CAST(N'2022-04-15T00:00:00.000' AS DateTime), 1, 501, 3, CAST(101.90 AS Decimal(10, 2)))
GO
INSERT [dbo].[MyProduct] ([SalesID], [ProductSaleDate], [CustomerID], [ProductID], [TotalQuantity], [TotalAmount]) VALUES (2, CAST(N'2023-05-22T00:00:00.000' AS DateTime), 2, 301, 2, CAST(32.45 AS Decimal(10, 2)))
GO
INSERT [dbo].[MyProduct] ([SalesID], [ProductSaleDate], [CustomerID], [ProductID], [TotalQuantity], [TotalAmount]) VALUES (3, CAST(N'2024-06-30T00:00:00.000' AS DateTime), 3, 701, 1, CAST(103.78 AS Decimal(10, 2)))
GO
SET IDENTITY_INSERT [dbo].[MyProduct] OFF
GO
/****** Object:  Index [PK_Sales]    Script Date: 19-11-2024 13:57:14 ******/
ALTER TABLE [dbo].[MyProduct] ADD  CONSTRAINT [PK_Sales] PRIMARY KEY NONCLUSTERED 
(
	[SalesID] ASC,
	[ProductSaleDate] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [ps_MyProductSalePerYear]([ProductSaleDate])
GO

Conclusion

Generating scripts for tables with data in SQL Server is crucial when working on a SQL server migration project or creating a test environment. The above steps can help you achieve this.

You may also like following the articles below.