You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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 CONVERT for dates) to match your table's schema. For identity columns, add SET IDENTITY_INSERT dbo.Customer ON before running the INSERTs, then SET IDENTITY_INSERT dbo.Customer OFF afterward.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:08:27