为模式和数据生成脚本 [英] Generate script for both schema and data

查看:26
本文介绍了为模式和数据生成脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SQL Server 数据库,我想为其生成数据脚本和架构.我试过 Tasks ->生成脚本 ->为所选数据库中的所有对象编写脚本,但它不会为数据本身提供 .sql.

I have a SQL Server database for which I want to generate script of data as well as schema both. I tried Tasks -> Generate Scripts -> Script all objects in the selected database but it does not give the .sql for the data itself.

我们如何为数据和模式生成 .sql 数据库?请建议一些简单的方法,例如工具或易于使用的东西

How do we generate the .sql database for both data as well schema? Please suggest some easy method such as a tool or something that can be used easily

推荐答案

备份/导出数据库

有两种使用 SQL Server Management Studio 备份/导出 SQL Server 数据库的方法:

There are two ways to Back up/Export a SQL Server database using SQL Server Management Studio:

  1. 右键单击数据库 → 任务 → 生成脚本 → 选择数据库 → 将脚本数据"选项更改为 true → ...

  1. Right click database → Tasks → Generate Scripts → Choose DB → Change "Script Data" option to true → …

右键单击数据库 → 任务 → 备份 → ...

Right click database → Tasks → Backup → ...

第一种方法创建一个 .sql 文件,然后我们需要运行该文件.此方法的问题在于 .sql 文件可能太大而无法使用 SQL Server Management Studio 打开.在这种情况下,我们需要使用 sqlcmd 实用程序(如果我们有 SQL Server MS,应该已经安装了).说明如下.

The first method creates a .sql file that then we need to run. The problem with this method is that the .sql file can be too big to be opened with SQL Server Management Studio. In that case we need to use the sqlcmd utility (should be already installed if we have SQL Server MS). Instructions below.

第二种方法创建一个 .back 文件,然后可以轻松地将其导入到空数据库中.

The second method creates a .back file that is then easy to import into an empty database.

导入数据库

如果我们有一个 .sql 文件并且它不是太大,我们可以用 SQL Server MS 打开它并运行它.

If we have a .sql file and it’s not too big we can just open it with SQL Server MS and run it.

如果我们有一个 .sql 文件,但它太大而无法用 SQL Server MS 打开,我们必须像这样使用 sqlcmd:

If we have a .sql file but it’s too big to be opened with SQL Server MS we have to use sqlcmd like this:

>sqlcmd -i C:\panels_QA28July11.sql -o C:\PanelsImportResult.txt

-i 后面的参数是要导入的文件.-o 后面的参数是保存输出的位置.如果我们想在屏幕上看到进程,我们可以省略第二个参数.

The parameter after -i is the file to import. The parameter after -o is where to save the output. We can omit the second parameter if we want to see the process on the screen.

默认情况下,它将使用本地机器和本地数据库服务器.如果我们想使用不同的机器和服务器,我们使用 -S 选项.

By default it will use the local machine and local database server. If we want to use a different machine and server we use the -S option.

这篇关于为模式和数据生成脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆