如何使用SQL Server 2014导出部分表记录为INSERT脚本?
Generate Partial Data INSERT Scripts in SQL Server 2014
Absolutely feasible! You don't have to wrestle with those huge, unopenable export files—here are two reliable, straightforward ways to generate INSERT scripts for only the data you need in SQL Server 2014:
Method 1: Use SSMS's Built-in Script Generator (Graphical Interface)
This is the easiest option for most users, with a point-and-click workflow:
- Open SQL Server Management Studio (SSMS) and connect to your 2014 instance. Navigate to your target table in the Object Explorer.
- Right-click the table → Tasks → Generate Scripts.
- In the wizard, select Specific database objects, check your table, then click Next.
- On the "Set Scripting Options" page, click the Advanced button.
- Find Types of data to script and choose either Data only (if you just need INSERT statements) or Schema and data (if you also need the table structure).
- Locate Script data range and select Specific rows.
- Click the ellipsis (
...) next to it, enter your filter condition (e.g.,WHERE CreatedDate >= '2023-01-01' AND Status = 'Active'), then click OK.
- Finish the wizard by choosing to save the script to a local file or open it in a new query window. The resulting script will only include the rows you filtered!
Method 2: Write Custom T-SQL for Full Control
If you need more flexibility in formatting or want to generate scripts on the fly, use a T-SQL query to build INSERT statements directly:
For example, if you want the first 1000 rows from dbo.Customer where Country = 'USA':
SELECT 'INSERT INTO dbo.Customer (CustomerID, Name, Email, SignupDate) VALUES (' + QUOTENAME(CustomerID, '''') + ', ' + QUOTENAME(Name, '''') + ', ' + QUOTENAME(Email, '''') + ', ' + QUOTENAME(CONVERT(VARCHAR(20), SignupDate, 120), '''') + ');' AS InsertScript FROM dbo.Customer WHERE Country = 'USA' ORDER BY SignupDate OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY;
- Run this query, then copy the results from the "InsertScript" column—each row is a ready-to-execute INSERT statement.
- Note: Adjust the column list and data type conversions (like the
CONVERTfor dates) to match your table's schema. For identity columns, addSET IDENTITY_INSERT dbo.Customer ONbefore running the INSERTs, thenSET IDENTITY_INSERT dbo.Customer OFFafterward.
Quick Tips
- If your table has large text columns (e.g.,
VARCHAR(MAX)), the SSMS graphical method is more reliable than manual T-SQL, as it handles big data chunks better. - The partial data script will be small enough to easily open in Notepad++—no more struggling with oversized export files!
内容的提问来源于stack exchange,提问作者LAffair
相关产品推荐
相关产品推荐

