SQL Server 2005 - 以编程方式导出表(运行 .sql 文件来重建它)

发布于 2024-07-05 06:00:14 字数 261 浏览 12 评论 0原文

我有一个数据库,其中有一个包含一些数据的客户表

我办公室里有另一个数据库,一切都一样,但我的表 Customers 是空的

如何在 SQL Server 2005 (T-SQL) 中创建一个 sql 文件,该文件从第一个数据库中获取 Customers 表上的所有内容,创建一个 buildcustomers.sql,我压缩该文件,通过网络复制它,执行它在我的 SQL Server 中,瞧! 我的桌子顾客已满

我怎样才能对整个数据库做同样的事情?

I have a database with a table Customers that have some data

I have another database in the office that everything is the same, but my table Customers is empty

How can I create a sql file in SQL Server 2005 (T-SQL) that takes everything on the table Customers from the first database, creates a, let's say, buildcustomers.sql, I zip that file, copy it across the network, execute it in my SQL Server and voila! my table Customers is full

How can I do the same for a whole database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不…忘初心 2024-07-12 06:00:15

我只是想为 Sql Server Management Studio 2008 添加一些屏幕截图。使用前面描述的步骤是正确的。 当您有“生成并发布脚本”时 -> “设置脚本选项”,然后按“高级”查看脚本选项:

![在哪里可以找到高级脚本选项]:图像丢失,因为我没有正确的声誉:(

对于 Sql Server Management Studio 2008,包含数据的选项是“类型”数据到脚本'

![脚本数据类型]:图像丢失,因为我没有正确的声誉:(

I just like to add some screen shoots for Sql Server Management Studio 2008. It is correct to use the steps describe previously. When you have the 'Generate and Publish Script' -> 'Set Script Options' then press Advance to see script options:

![Where to find Advanced script options]: image missing because I do not have the right reputation :(

For Sql Server Management Studio 2008 the option to included data is 'Types of data to script'

![Types of data to script]: image missing because I do not have the right reputation :(

_畞蕅 2024-07-12 06:00:14

使用bcp(从命令行)连接到网络文件,然后将其恢复。

例如

bcp "SELECT * FROM CustomerTable" queryout "c:\temp\CustomerTable.bcp" 
     -N -S SOURCESERVERNAME -T 

bcp TargetDatabaseTable in "c:\temp\CustomerTable.bcp" -N -S TARGETSERVERNAME -T 
  • -N 使用本机类型
  • -T 使用可信连接
  • -S ServerName

非常快速且轻松地嵌入代码中。 (我已经围绕这个命令构建了一个数据库备份(恢复)系统。

Use bcp (from the command line) to a networked file and then restore it.

e.g.

bcp "SELECT * FROM CustomerTable" queryout "c:\temp\CustomerTable.bcp" 
     -N -S SOURCESERVERNAME -T 

bcp TargetDatabaseTable in "c:\temp\CustomerTable.bcp" -N -S TARGETSERVERNAME -T 
  • -N use native types
  • -T use the trusted connection
  • -S ServerName

Very quick and easy to embed within code. (I've built a database backup(restore) system around this very command.

薄荷梦 2024-07-12 06:00:14

此功能已内置于 Sql Server Management Studio 2008 中。

只需下载 试用并仅安装客户端工具(不应过期)。 使用 Management Studio 2008 连接到 2005 数据库(向后兼容)。

  1. 右键单击您的数据库
  2. 选择任务> 生成脚本
  3. 按下一步,再次选择您的数据库
  4. 在“选择脚本选项”屏幕上,有一个名为脚本数据的选项,它将为您的所有数据生成 SQL 插入语句。

(注意:对于 SQL Server Management Studio 2008 R2,该选项称为“要编写脚本的数据类型”,是“常规”部分的最后一个选项。选项包括“仅数据”、“架构和数据”和“仅架构” ")

替代文本
替代文字

This functionality is already built in to Sql Server Management Studio 2008.

Just download the trial and only install the client tools (which shouldn't expire). Use Management Studio 2008 to connect to your 2005 database (its backwards compatible).

  1. Right click your database
  2. Choose Tasks > Generate Scripts
  3. Press Next, select your database again
  4. On the 'Choose Script Options' screen, there is an option called Script Data which will generate SQL insert statements for all your data.

(Note: for SQL Server Management Studio 2008 R2, the option is called "Types of data to script" and is the last one in the General section. The choices are "data only", "schema and data", and "schema only")

alt text
alt text

一刻暧昧 2024-07-12 06:00:14

您可以查看以下文章,了解如何使用 SQL Server 本机工具和第三方工具来执行此操作:SQL Server 批量复制和批量导入导出技术

免责声明:我在 ApexSQL 担任支持工程师

希望这有帮助

You can check the following article to see how you can do this by using both SQL Server native tools and the third party tools: SQL Server bulk copy and bulk import and export techniques

Disclaimer: I work for ApexSQL as a Support Engineer

Hope this helps

戈亓 2024-07-12 06:00:14

如果两个数据库驻留在同一 SQL Server 实例中,即使用相同的连接,则此 SQL 可能会有所帮助:

INSERT INTO [DestinationDB].[schema].[table] ([column])
SELECT [column] FROM [OriginDB].[schema].[table]
GO

If both databases resides in the same instance of SQL Server, ie use same connection, this SQL might be helpful:

INSERT INTO [DestinationDB].[schema].[table] ([column])
SELECT [column] FROM [OriginDB].[schema].[table]
GO
霓裳挽歌倾城醉 2024-07-12 06:00:14

您始终可以将数据从“客户”表导出到 Excel 文件,然后将该数据导入到“客户”表中。

导入/导出数据:

  1. 右键单击数据库
  2. 转到任务
  3. 转到导入数据导出数据
  4. 将数据源更改为Microsoft Excel
  5. 按照向导操作

You could always export the data from the Customers table to an Excel file and import that data into your Customers table.

To import/export data:

  1. Right click on database
  2. Go to Tasks
  3. Go to Import Data or Export Data
  4. Change the data source to Microsoft Excel
  5. Follow the wizard
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文