跳到正文
IntoSQL 首页

将 SQL Server 脚本迁移到更旧或更新的 SQL Server

拖入 SSMS 生成脚本功能写出的包含架构和数据的脚本,格式为 .sql 或 .sql.gz,UTF-16 或 UTF-8 均可,无论来自哪个版本,都能下载可在 SQL Server 2017 到 2025 导入的脚本。标识列、排序规则以及 xml 和 hierarchyid 等类型保持不变,每项更改都会列在报告中。

你的文件
sqlshop_2024_sqlserver.sql示例文件,11 kB
你的数据库

已转换 18 行,1 处修正,无拒绝

显示

shop_2024_sqlserver.sql 中的表SQL Server 转储 · 由 SQL Server Management Objects 写出 · utf-8

  • orders10 行
    11 列
    idint not null
    customer_idint not null
    statusnvarchar(10) not null
    totaldecimal(19,4) not null
    itemstinyint not null
    ordered_atdatetime2(0) not null
    shipped_atdatetime2(7)
    notenvarchar(max)
    detailsnvarchar(max)
    order_refuniqueidentifier not null
    total_with_taxdecimal(12,2)
  • customers8 行1
    8 列
    idint not null
    namenvarchar(100) not null
    emailnvarchar(200) not null
    countrynchar(2) not null
    is_activebit not null
    signed_update
    created_atdatetimeoffset(0) not null
    row_versionbinary(8)

未转换

  • 视图 paid_orders
  • 触发器 orders_no_negative

每种数据库都用自己的语言编写这些对象。请为 SQL Server 重写它们。

-- Written by IntoSQL from a SQL Server dump. Load it with: sqlcmd -b -f 65001 -d <database> -i <file>
SET NOCOUNT ON;
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET NUMERIC_ROUNDABORT OFF;
GO

CREATE TABLE [dbo].[orders] (
    [id] int IDENTITY(1, 1) NOT NULL,
    [customer_id] int NOT NULL,
    [status] nvarchar(10) COLLATE SQL_Latin1_General_CP1_CI_AS DEFAULT N'pending' NOT NULL,
    [total] decimal(19,4) NOT NULL,
    [items] tinyint DEFAULT 1 NOT NULL,
    [ordered_at] datetime2(0) NOT NULL,
    [shipped_at] datetime2(7) NULL,
    [note] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [details] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [order_ref] uniqueidentifier DEFAULT (NEWID()) NOT NULL,
    [total_with_tax] AS CAST((CAST([total] * (1.16) AS decimal(12,2))) AS decimal(12,2)) PERSISTED,
    CONSTRAINT [check2] CHECK ([status] = N'cancelled' OR [status] = N'shipped' OR [status] = N'paid' OR [status] = N'pending'),
    CONSTRAINT [check3] CHECK ([total] >= (0)),
    CONSTRAINT [PK_orders] PRIMARY KEY ([id])
);
GO

CREATE TABLE [dbo].[customers] (
    [id] int IDENTITY(1, 1) NOT NULL,
    [name] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [email] nvarchar(200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [country] nchar(2) COLLATE SQL_Latin1_General_CP1_CI_AS DEFAULT N'US' NOT NULL,
    [is_active] bit DEFAULT 1 NOT NULL,
    [signed_up] date NULL,
    [created_at] datetimeoffset(0) DEFAULT SYSDATETIMEOFFSET() NOT NULL,
    [row_version] binary(8) NULL,
    CONSTRAINT [PK_customers] PRIMARY KEY ([id])
);
GO

SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(1,N'Ana Martínez',N'ana@example.com',N'MX',1,'2023-01-15','2023-01-15T04:24:00.0000000-06:00');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(2,N'Zoë Müller',N'zoe@example.de',N'DE',1,'2023-02-03','2023-02-03T09:11:42.0000000+01:00');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(3,N'José Peña',N'jose@example.es',N'ES',0,'2023-03-21','2023-03-21T20:05:17.0000000+01:00');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(4,N'李雷',N'lilei@example.cn',N'CN',1,'2023-05-09','2023-05-09T10:40:00.0000000+08:00');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(5,N'Noah O''Brien',N'noah@example.ie',N'IE',1,NULL,'2023-06-30T15:00:00.0000000+01:00');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(6,N'Amélie Laurent',N'amelie@example.fr',N'FR',1,'2024-01-02','2024-01-02T10:30:00.0000000+01:00');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(7,N'Ravi Patel',N'ravi@example.in',N'IN',0,'2024-02-29','2024-03-01T05:29:59.0000000+05:30');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[customers] ON;
INSERT INTO [dbo].[customers] ([id], [name], [email], [country], [is_active], [signed_up], [created_at]) VALUES
(8,N'Sofia Rossi 🌻',N'sofia@example.it',N'IT',1,'2024-04-12','2024-04-12T14:12:12.0000000+02:00');
SET IDENTITY_INSERT [dbo].[customers] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(1,1,N'shipped',129.9000,3,'2024-03-01T10:00:00.0000000','2024-03-02T16:30:00.1234567',NULL,N'{"gift": false}',N'0b7a4c2e-3f1d-4e8a-9c55-1a2b3c4d5e6f');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(2,2,N'paid',54.0000,1,'2024-03-02T11:15:00.0000000',NULL,N'Gift wrap 🎁',N'{"gift": true, "wrap": "red"}',N'1c8b5d3f-4a2e-4f9b-8d66-2b3c4d5e6f70');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(3,1,N'cancelled',12.5000,1,'2024-03-03T09:45:00.0000000',NULL,N'Customer changed their mind',NULL,N'2d9c6e4a-5b3f-4a0c-9e77-3c4d5e6f7081');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(4,4,N'shipped',980.0000,12,'2024-03-05T02:10:00.0000000','2024-03-07T08:00:00.0000000',N'请小心轻放',N'{"fragile": true}',N'3e0d7f5b-6c4a-4b1d-8f88-4d5e6f708192');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(5,5,N'pending',45.7500,2,'2024-03-08T17:20:00.0000000',NULL,N'Leave at the door; ring twice',NULL,N'4f1e806c-7d5b-4c2e-9a99-5e6f708192a3');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(6,6,N'shipped',310.4000,5,'2024-03-10T13:00:00.0000000','2024-03-11T09:00:00.0000000',N'Fragile: "glass"',N'{"fragile": true}',N'50f2917d-8e6c-4d3f-8baa-6f708192a3b4');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(7,8,N'paid',76.2000,2,'2024-03-11T18:45:00.0000000',NULL,NULL,NULL,N'6103a28e-9f7d-4e40-9cbb-708192a3b4c5');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(8,2,N'shipped',19.9900,1,'2024-03-12T08:00:00.0000000','9999-12-31T23:59:59.9999999',N'Shipped date set to the maximum by an old import',NULL,N'7214b39f-a08e-4f51-8dcc-8192a3b4c5d6');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(9,3,N'shipped',64.0000,2,'2024-03-14T15:30:00.0000000','2024-03-15T10:00:00.0000000',N'Two parcels',N'{"parcels": [1, 2]}',N'8325c4a0-b19f-4062-9edd-92a3b4c5d6e7');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO
SET IDENTITY_INSERT [dbo].[orders] ON;
INSERT INTO [dbo].[orders] ([id], [customer_id], [status], [total], [items], [ordered_at], [shipped_at], [note], [details], [order_ref]) VALUES
(10,7,N'paid',1499.0000,1,'2024-03-15T21:05:00.0000000',NULL,N'C:\Users\ravi\invoice.pdf',NULL,N'9436d5b1-c2a0-4173-8fee-a3b4c5d6e7f8');
SET IDENTITY_INSERT [dbo].[orders] OFF;
GO

-- Indexes, foreign keys, counters and triggers, after the rows.
CREATE INDEX [idx_orders_customer] ON [dbo].[orders] ([customer_id]);
CREATE INDEX [idx_orders_status_date] ON [dbo].[orders] ([status], [ordered_at]);
CREATE UNIQUE INDEX [email] ON [dbo].[customers] ([email]);
ALTER TABLE [dbo].[orders] WITH NOCHECK ADD CONSTRAINT [FK_orders] FOREIGN KEY ([customer_id]) REFERENCES [dbo].[customers] ([id]);
BEGIN TRY ALTER TABLE [dbo].[orders] WITH CHECK CHECK CONSTRAINT [FK_orders]; END TRY BEGIN CATCH PRINT N'Foreign key ' + N'[FK_orders]' + N' has rows without a parent: it stays unchecked, and new rows are checked.'; END CATCH;
DECLARE @next decimal(38,0) = (SELECT MAX([id]) FROM [dbo].[orders]); IF @next IS NOT NULL BEGIN IF @next < 0 SET @next = 0; DBCC CHECKIDENT (N'[dbo].[orders]', RESEED, @next) WITH NO_INFOMSGS; END;
GO
DECLARE @next decimal(38,0) = (SELECT MAX([id]) FROM [dbo].[customers]); IF @next IS NOT NULL BEGIN IF @next < 0 SET @next = 0; DBCC CHECKIDENT (N'[dbo].[customers]', RESEED, @next) WITH NO_INFOMSGS; END;
GO
EXEC sys.sp_addextendedproperty @name = N'MS_Description', @value = N'People who bought at least once', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'customers';
GO

修正

  1. customers 的 "row_version" 是 SQL Server 自动设置的行版本;转储中没有它的值,因此改为 binary(8),并保持为空。

需要检查

  • 2 个视图、触发器、例程或事件未转换;报告逐一列出了它们。 请在 SQL Server 中手动重新创建它们。
  • orders 上的检查约束 check1 无法为 SQL Server 写出(function isjson),因此未创建。 如果它重要,请手动编写该检查约束。
  • orders 上的索引 idx_orders_pending 使用了无法为 SQL Server 写出的表达式(an index on an expression),因此未创建。 如果查询需要,请手动创建该索引。

文件在这台电脑上转换,永远不会上传。

其他示例:

文件与脚本之间的变化

每一行是转换器应用的一条规则,以及该规则更改你的数据时在报告中写入的记录。

  • bit

    bit

    类型相同。

  • char(n), varchar(n), nchar(n), nvarchar(n), varchar(max), text, ntext

    nchar(n)、nvarchar(n) 和 nvarchar(max),保留每列的排序规则

    无论 varchar 列原来的代码页是什么,nvarchar 都能容纳每个字符。SQL Server 2017 不支持的 UTF-8 排序规则会变为对应的 Unicode 排序规则。

  • column collations

    每列使用相同的排序规则

    列的比较和排序方式不变,唯一键也区分相同的行。

  • decimal(p,s), money, smallmoney

    decimal(p,s);money 为 decimal(19,4)

    保留每一位数字,money 保留四位小数。

  • datetime, datetime2, smalldatetime

    datetime2(p)

    datetime2 能以相同精度容纳每个 datetime 和 smalldatetime 值,从公元 1 年起。

  • datetimeoffset

    datetimeoffset,保留每个值的偏移量

    类型和值都相同。

  • time

    time(p)

    类型相同。

  • dates that aren't valid

    NULL

    不是日期的值变为 NULL,报告会统计其数量。

    报告记录: date_out_of_range

  • json (SQL Server 2025)

    nvarchar(max)

    JSON 值相同,SQL Server 的 JSON 函数可以读取。

  • uniqueidentifier

    uniqueidentifier

    类型相同。

  • binary, varbinary, image

    binary(n)、varbinary(n) 和 varbinary(max)

    字节仍然是字节。

  • xml, sql_variant, hierarchyid, geometry, geography

    相同的类型

    保留 SQL Server 自己的类型;sql_variant 值保留其文本,因此其基础类型变为 nvarchar。

  • rowversion, timestamp

    binary(8),空

    rowversion 列无法接收复制的行,SSMS 也不会为其写出值;如有需要,请在导入后重新添加。

    报告记录: rowversion_empty

  • IDENTITY

    IDENTITY,从最大的 id 之后继续

    复制的 id 通过 IDENTITY_INSERT 导入,计数器从最大的 id 之后继续。

  • CREATE SEQUENCE

    CREATE SEQUENCE,选项相同

    SQL Server 序列的工作方式相同。SSMS 不会写出序列当前到达的位置,因此它从创建时的值开始。

  • computed columns

    相同的计算列,与源数据一样为 PERSISTED

    公式相同,由目标数据库计算。

  • computed columns with functions IntoSQL doesn't write

    普通列

    无法写出的公式会留下一个普通列和一条报告记录;SSMS 不会为计算列写出值。

    报告记录: generated_not_translated

  • defaults with functions IntoSQL doesn't write, and NEXT VALUE FOR

    无默认值

    无法写出的默认值会被省略,并在报告中列出。

    报告记录: default_not_translated

  • CHECK constraints

    已转换

    检查约束相同,在导入每一行时检查。

  • CHECK constraints with functions IntoSQL doesn't write

    不创建

    无法写出的检查约束会被省略,并在报告中列出。

    报告记录: check_skipped

  • CHECK constraints added WITH NOCHECK or disabled

    不创建

    SQL Server 不执行这些检查约束,因此行可能违反它们。

    报告记录: check_not_enforced

  • filtered and columnstore indexes

    不创建

    IntoSQL 在整列上写出 b-tree 索引;其他索引请在导入后重新创建。

    报告记录: index_skipped

  • foreign keys

    在数据之后以 WITH NOCHECK 添加,然后检查,有孤立行时给出警告

    外键在行之后创建,最后检查;有孤立行时外键保持未检查状态并给出警告,而不是中止导入。

  • ON DELETE or ON UPDATE that cascades in a cycle or along a second path

    NO ACTION

    SQL Server 拒绝这些级联操作,因此转储中不会出现;这一行是为手动编辑的脚本准备的。

    报告记录: foreign_key_action_dropped

  • foreign keys to tables the dump doesn't create

    不创建

    外键无法引用不存在的表。

    报告记录: foreign_key_skipped

  • tables on a partition scheme

    单个表,包含每个分区的行

    行保持不变;如有需要,请在 SQL Server 中重新对该表分区。

    报告记录: partitions_dropped

  • temporal tables (SYSTEM_VERSIONING)

    两个表:当前行和历史记录

    表及其历史表作为普通表复制;重新开启 SYSTEM_VERSIONING 即可将它们关联起来。

    报告记录: system_versioning_dropped

  • NOT NULL columns with values that become NULL

    去除 NOT NULL

    值必须变为 NULL 的列无法保留 NOT NULL,否则导入会中止。

    报告记录: not_null_relaxed

  • views, functions, procedures, triggers, synonyms, types and partition functions

    在报告中列出

    IntoSQL 转换表及其行;其余对象会列出来,由你根据原始脚本重新创建。

    报告记录: not_converted_objects

  • scripts without CREATE TABLE (data only)

    根据值推断类型

    只含数据的脚本只有行,因此类型来自值。

    报告记录: no_schema

  • expressions in INSERT values

    NULL

    不运行数据库的转换器无法对它们求值。

    报告记录: expression_value

  • types IntoSQL doesn't know

    nvarchar(max)

    文本可以容纳任何值;请在导入后更改类型。

    报告记录: unknown_type

  • NUL characters in text

    保留

    nvarchar 可以包含 NUL 字符,因此值按原样复制。

  • text that isn't valid in its encoding

    错误的字节变为替换字符

    脚本是 UTF-8,每个值都必须是有效文本。

    报告记录: invalid_utf8_replaced

  • text decoded twice (ñ)

    按原样复制

    脚本中存储的正是这些字符,因此按原样复制;报告会指出它们。

    报告记录: mojibake_suspected

文件中脚本中报告记录
bitbit类型相同。
char(n), varchar(n), nchar(n), nvarchar(n), varchar(max), text, ntextnchar(n)、nvarchar(n) 和 nvarchar(max),保留每列的排序规则无论 varchar 列原来的代码页是什么,nvarchar 都能容纳每个字符。SQL Server 2017 不支持的 UTF-8 排序规则会变为对应的 Unicode 排序规则。
column collations每列使用相同的排序规则列的比较和排序方式不变,唯一键也区分相同的行。
decimal(p,s), money, smallmoneydecimal(p,s);money 为 decimal(19,4)保留每一位数字,money 保留四位小数。
datetime, datetime2, smalldatetimedatetime2(p)datetime2 能以相同精度容纳每个 datetime 和 smalldatetime 值,从公元 1 年起。
datetimeoffsetdatetimeoffset,保留每个值的偏移量类型和值都相同。
timetime(p)类型相同。
dates that aren't validNULL不是日期的值变为 NULL,报告会统计其数量。date_out_of_range
json (SQL Server 2025)nvarchar(max)JSON 值相同,SQL Server 的 JSON 函数可以读取。
uniqueidentifieruniqueidentifier类型相同。
binary, varbinary, imagebinary(n)、varbinary(n) 和 varbinary(max)字节仍然是字节。
xml, sql_variant, hierarchyid, geometry, geography相同的类型保留 SQL Server 自己的类型;sql_variant 值保留其文本,因此其基础类型变为 nvarchar。
rowversion, timestampbinary(8),空rowversion 列无法接收复制的行,SSMS 也不会为其写出值;如有需要,请在导入后重新添加。rowversion_empty
IDENTITYIDENTITY,从最大的 id 之后继续复制的 id 通过 IDENTITY_INSERT 导入,计数器从最大的 id 之后继续。
CREATE SEQUENCECREATE SEQUENCE,选项相同SQL Server 序列的工作方式相同。SSMS 不会写出序列当前到达的位置,因此它从创建时的值开始。
computed columns相同的计算列,与源数据一样为 PERSISTED公式相同,由目标数据库计算。
computed columns with functions IntoSQL doesn't write普通列无法写出的公式会留下一个普通列和一条报告记录;SSMS 不会为计算列写出值。generated_not_translated
defaults with functions IntoSQL doesn't write, and NEXT VALUE FOR无默认值无法写出的默认值会被省略,并在报告中列出。default_not_translated
CHECK constraints已转换检查约束相同,在导入每一行时检查。
CHECK constraints with functions IntoSQL doesn't write不创建无法写出的检查约束会被省略,并在报告中列出。check_skipped
CHECK constraints added WITH NOCHECK or disabled不创建SQL Server 不执行这些检查约束,因此行可能违反它们。check_not_enforced
filtered and columnstore indexes不创建IntoSQL 在整列上写出 b-tree 索引;其他索引请在导入后重新创建。index_skipped
foreign keys在数据之后以 WITH NOCHECK 添加,然后检查,有孤立行时给出警告外键在行之后创建,最后检查;有孤立行时外键保持未检查状态并给出警告,而不是中止导入。
ON DELETE or ON UPDATE that cascades in a cycle or along a second pathNO ACTIONSQL Server 拒绝这些级联操作,因此转储中不会出现;这一行是为手动编辑的脚本准备的。foreign_key_action_dropped
foreign keys to tables the dump doesn't create不创建外键无法引用不存在的表。foreign_key_skipped
tables on a partition scheme单个表,包含每个分区的行行保持不变;如有需要,请在 SQL Server 中重新对该表分区。partitions_dropped
temporal tables (SYSTEM_VERSIONING)两个表:当前行和历史记录表及其历史表作为普通表复制;重新开启 SYSTEM_VERSIONING 即可将它们关联起来。system_versioning_dropped
NOT NULL columns with values that become NULL去除 NOT NULL值必须变为 NULL 的列无法保留 NOT NULL,否则导入会中止。not_null_relaxed
views, functions, procedures, triggers, synonyms, types and partition functions在报告中列出IntoSQL 转换表及其行;其余对象会列出来,由你根据原始脚本重新创建。not_converted_objects
scripts without CREATE TABLE (data only)根据值推断类型只含数据的脚本只有行,因此类型来自值。no_schema
expressions in INSERT valuesNULL不运行数据库的转换器无法对它们求值。expression_value
types IntoSQL doesn't knownvarchar(max)文本可以容纳任何值;请在导入后更改类型。unknown_type
NUL characters in text保留nvarchar 可以包含 NUL 字符,因此值按原样复制。
text that isn't valid in its encoding错误的字节变为替换字符脚本是 UTF-8,每个值都必须是有效文本。invalid_utf8_replaced
text decoded twice (ñ)按原样复制脚本中存储的正是这些字符,因此按原样复制;报告会指出它们。mojibake_suspected

将脚本导入 SQL Server

脚本先创建表、复制行,再添加键、索引和外键,因此导入速度快,并会在遇到第一个错误时停止。

sqlcmd -b -f 65001 -S localhost -d mydb -i sql_server.sql_server.sql

外键先以 WITH NOCHECK 添加,然后再检查。如果某行指向不存在的行,脚本会说明是哪个约束,并让它保持未检查状态,而不是中途失败。

对于较大的转储,请在服务器所在的机器上运行 sqlcmd。每批最多 1000 行单独提交,因此事务日志保持较小。

如何转储 SQL Server 以获得最佳结果

转换器读取转储中的内容。以下选项能让转储保留更多数据:

  • 在 SSMS 中右键单击数据库,依次选择"任务"、"生成脚本",然后选择表。dbatools、mssql-scripter 以及其他基于同一库的工具也会写出相同的脚本。
  • 在"高级"中,将"要编写脚本的数据的类型"设为"架构和数据"。只含架构的脚本中,表没有行。
  • 以 SSMS 默认的 Unicode 或 UTF-8 保存:两者的读取方式相同,也支持 gzip。

哪些内容不会转换

视图、触发器、存储过程、函数等用 SQL Server 自己的语言编写的对象,会按名称列在报告中,绝不会被悄悄丢弃,你可以先在要导入的 SQL Server 版本上逐一检查再重建。

关于在 SQL Server 版本之间迁移的问题

这里没有你要的答案?请通过联系页面给我们留言,会有专人回复。 联系页面

我的转储会被上传吗?

不会。转换器在你的浏览器中运行,分块从磁盘读取转储,因此转储不会离开你的电脑。桌面应用和命令行也在你的电脑上转换。

转储可以多大?

转换器从不把整个转储放进内存:它按范围读取文件两次,边读边写出脚本。几 GB 的转储可以在浏览器、桌面应用和命令行中转换。

它能读取 SSMS 保存的 UTF-16 文件吗?

能。SSMS 默认以 UTF-16 保存脚本,而转换器同样能读取 UTF-16 和 UTF-8,无论是否经过 gzip 压缩,批处理之间的 GO 也能识别。

脚本可以来自哪些 SQL Server 版本?

由 SSMS、dbatools 或 mssql-scripter 从 SQL Server 2017 及更高版本写出的脚本。每个测试脚本都来自 SQL Server 2017、2022 和 2025,并与其源数据逐个单元格比较。

IDENTITY 值会保留吗?

会。IDENTITY 列通过 IDENTITY_INSERT 导入,每一行都保留其 id,计数器从最大的 id 之后继续。序列以相同的选项创建。

money、datetimeoffset 和其他 SQL Server 类型会怎样处理?

保持不变:包括 xml、sql_variant、hierarchyid、geometry 和 geography,每个文本列都保留其排序规则。datetimeoffset 值保留其偏移量。rowversion 列为 binary(8),因为脚本中没有它们的值。

存储过程、触发器和视图怎么处理?

它们会按名称在报告中列出,但不会转换,因此在表导入之后,你可以运行原始脚本中的相应部分。

脚本可以导入哪些 SQL Server 版本?

SQL Server 2017 到 2025,无论脚本由哪个版本写出。同一个脚本可以在其中每个版本上导入,如有问题会在第一个错误处停止。