---
title: "在浏览器中将 SQLite 迁移到 SQL Server"
description: "将 SQLite 数据库迁移到 SQL Server：把 .db 文件或其转储转换为表、键和数据相同的脚本，并报告每项更改。"
canonical: https://intosql.com/zh-cn/sqlite-to-sql-server
last_updated: 2026-10-15
---

# 在浏览器中将 SQLite 迁移到 SQL Server

拖入 SQLite 数据库文件（.db、.sqlite、.sqlite3）或 sqlite3 的 .dump 写出的 SQL，即可下载包含相同表、键、索引和数据的 SQL Server 脚本。每列获得其值都符合的类型，SQL Server 无法原样存储的每个值都会在报告中列出。

[SQLite 到 SQL Server 迁移指南](https://intosql.com/docs/migrations/sqlite-to-sql-server)

[SQLite 到 SQL Server 的数据类型](https://intosql.com/docs/types/sqlite-to-sql-server)

## 从 SQLite 到 SQL Server 的变化

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

| 在 SQLite 中 | 在 SQL Server 中 | 报告记录 |
| --- | --- | --- |
| declared types (any text: VARCHAR(255), DATETIME, BOOLEAN, JSON or none) | 列中每个值都符合的类型 |  |
| BOOLEAN, BOOL | bit |  |
| TEXT, VARCHAR(n), CHAR(n), CLOB | nvarchar(n) 或 nvarchar(max) |  |
| DECIMAL(p,s), NUMERIC | 每个值都符合时为精确类型，否则为 double |  |
| BLOB, and columns holding bytes | varbinary(max) |  |
| DATE, DATETIME, TIMESTAMP holding ISO 8601 text | date、datetime2(p) |  |
| JSON holding JSON text | nvarchar(max) |  |
| Infinity in REAL columns | NULL | `invalid_value_null` |
| INTEGER PRIMARY KEY, AUTOINCREMENT | IDENTITY，从最大的 id 或 sqlite_sequence 的值之后继续 |  |
| COLLATE NOCASE | 不区分大小写的排序规则 |  |
| generated columns | 计算列，STORED 时为 PERSISTED |  |
| generated columns with functions IntoSQL doesn't write, and the || operator | 普通列 | `generated_not_translated` |
| defaults with functions IntoSQL doesn't write (strftime, randomblob) | 无默认值 | `default_not_translated` |
| CHECK constraints | 已转换 |  |
| CHECK constraints with functions IntoSQL doesn't write | 不创建 | `check_skipped` |
| partial and expression indexes | 不创建 | `index_skipped` |
| foreign keys (unenforced unless PRAGMA foreign_keys=ON) | 在数据之后以 WITH NOCHECK 添加，然后检查 |  |
| ON DELETE or ON UPDATE that cascades in a cycle or along a second path | NO ACTION | `foreign_key_action_dropped` |
| VARCHAR(n) holding characters past U+FFFF (emoji) | nvarchar(2n)，超过 4000 时为 nvarchar(max) | `text_length_widened` |
| foreign keys to tables the dump doesn't create | 不创建 | `foreign_key_skipped` |
| primary keys holding NULL, and NOT NULL columns with values that become NULL | 去除 NOT NULL | `not_null_relaxed` |
| views, triggers and virtual tables (FTS, R*Tree) | 在报告中列出 | `not_converted_objects` |
| dumps without CREATE TABLE (--data-only) | 根据值推断类型 | `no_schema` |
| expressions in INSERT values other than the text functions dumps write | NULL | `expression_value` |
| NUL characters in text | 保留 |  |
| text that isn't valid UTF-8 | 错误的字节变为替换字符 | `invalid_utf8_replaced` |
| text decoded twice (Ã±) | 按原样复制 | `mojibake_suspected` |

## 将脚本导入 SQL Server

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

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

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

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

## 如何转储 SQLite 以获得最佳结果

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

- 数据库文件本身会原样转换，在浏览器中逐页读取。请先关闭正在写入它的应用，以免有更改遗留在其 -wal 文件中。
- sqlite3 app.db .dump > app.sql 会将同一个数据库写为 SQL，它同样可以转换，格式为 .sql 或 .sql.gz。
- DB Browser for SQLite 通过 File、Export、Database to SQL file 也能写出该 SQL，Python 的 iterdump() 也一样。

## 哪些内容不会转换

视图和触发器是用 SQLite 自己的语言编写的。它们会按名称在报告中列出，绝不会被悄悄丢弃，以便你为 SQL Server 重写。FTS5 和 R*Tree 等虚拟表也会列出，其影子表则不包含在内。

## 关于 SQLite 转 SQL Server 的问题

### 我的转储会被上传吗？

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

### 转储可以多大？

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

### 可以直接转换 .db 文件吗？

可以。拖入 .db、.sqlite 或 .sqlite3 文件：它在浏览器中读取，绝不会上传。含 NUL 字符的文本和 double 值的每一位数字都会按存储原样保留，而 .dump 可能会将其截断。

### 转储可以来自哪些 SQLite 版本？

SQLite 3.0 及更高版本的 UTF-8 或 UTF-16 数据库文件，以及由 sqlite3 shell 的 .dump 或 Python 的 iterdump() 写出的转储。每个测试文件和转储都来自 sqlite3 3.31 到 3.54，包括 STRICT 和 WITHOUT ROWID 表，并与其源数据逐个单元格比较。

### SQLite 不强制类型时，列类型如何确定？

声明的类型只是提示，由值决定：当每个值都符合时，列会变为 bigint、decimal、float、date、datetime2、bit、varbinary 或 nvarchar。文本像在 SQLite 中一样精确比较，COLLATE NOCASE 列则忽略大小写比较。

### INTEGER PRIMARY KEY 和 AUTOINCREMENT 会保留吗？

会。INTEGER PRIMARY KEY 变为 IDENTITY 列，每一行都保留其 id，下一个 id 从最大的 id 之后继续；使用 AUTOINCREMENT 时则从 sqlite_sequence 的值之后继续。

### 视图和触发器怎么处理？

它们会按名称在报告中列出，但不会转换，因为每种数据库都用自己的语言编写它们。

### 脚本可以导入哪些 SQL Server 版本？

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

## 从 SQLite

- [SQLite 到 PostgreSQL](https://intosql.com/zh-cn/sqlite-to-postgresql)
- [SQLite 到 MySQL](https://intosql.com/zh-cn/sqlite-to-mysql)
- [SQLite 到 MariaDB](https://intosql.com/zh-cn/sqlite-to-mariadb)
- [SQLite 到 SQLite](https://intosql.com/zh-cn/sqlite-to-sqlite)
- [SQLite 到 Oracle](https://intosql.com/zh-cn/sqlite-to-oracle)

## 到 SQL Server

- [MySQL 到 SQL Server](https://intosql.com/zh-cn/mysql-to-sql-server)
- [MariaDB 到 SQL Server](https://intosql.com/zh-cn/mariadb-to-sql-server)
- [PostgreSQL 到 SQL Server](https://intosql.com/zh-cn/postgresql-to-sql-server)
- [SQL Server 到 SQL Server](https://intosql.com/zh-cn/sql-server-to-sql-server)
- [Oracle 到 SQL Server](https://intosql.com/zh-cn/oracle-to-sql-server)

## 指南和所有组合

- [SQL Server 到 SQLite](https://intosql.com/zh-cn/sql-server-to-sqlite)
- [SQLite 到 SQL Server 迁移指南](https://intosql.com/docs/migrations/sqlite-to-sql-server)
- [SQLite 到 SQL Server 的数据类型](https://intosql.com/docs/types/sqlite-to-sql-server)
- [所有数据库组合](https://intosql.com/zh-cn/database-migration)
