简体   繁体   English

使用sql server 2008中数据库中的数据备份单个表

[英]Backup a single table with its data from a database in sql server 2008

I want to get a backup of a single table with its data from a database in SQL Server using a script.我想使用脚本从 SQL Server 中的数据库中获取单个表及其数据的备份。

How can I do that?我该怎么做?

select * into mytable_backup from mytable

制作表mytable的副本,以及其中的每一行,称为mytable_backup

You can use the "Generate script for database objects" feature on SSMS.您可以使用 SSMS 上的“为数据库对象生成脚本”功能。

  1. Right click on the target database右键单击目标数据库
  2. Select Tasks > Generate Scripts选择任务 > 生成脚本
  3. Choose desired table or specific object选择所需的表格或特定对象
  4. Hit the Advanced button点击高级按钮
  5. Under General, choose value on the Types of data to script .在 General 下,在Types of data to script上选择值。 You can select Data only, Schema only, and Schema and data .您可以选择仅数据、仅架构和架构和数据 Schema and data includes both table creation and actual data on the generated script.模式和数据包括表创建和生成的脚本上的实际数据。
  6. Click Next until wizard is done单击下一步直到向导完成

This one solved my challenge.这个解决了我的挑战。
Hope this will help you as well.希望这也能帮助你。

There are many ways you can take back of table.您可以通过多种方式收回餐桌。

  1. BCP (BULK COPY PROGRAM) BCP(批量复制程序)
  2. Generate Table Script with data用数据生成表格脚本
  3. Make a copy of table using SELECT INTO, example here使用 SELECT INTO 制作表的副本,示例在这里
  4. SAVE Table Data Directly in a Flat file将表数据直接保存在平面文件中
  5. Export Data using SSIS to any destination使用 SSIS 将数据导出到任何目的地

You can create table script along with its data using following steps:您可以使用以下步骤创建表脚本及其数据:

  1. Right click on the database.右键单击数据库。
  2. Select Tasks > Generate scripts ...选择任务 > 生成脚本...
  3. Click next.点击下一步。
  4. Click next.点击下一步。
  5. In Table/View Options, set Script Data to True;在 Table/View Options 中,将 Script Data 设置为 True; then click next.然后点击下一步。
  6. Select the Tables checkbox and click next.选中表格复选框,然后单击下一步。
  7. Select your table name and click next.选择您的表名,然后单击下一步。
  8. Click next until the wizard is done.单击下一步直到向导完成。

For more information, see Eric Johnson's blog.有关更多信息,请参阅Eric Johnson 的博客。

尝试使用以下查询,它将在相同或其他数据库(“数据库”)中创建相应的表。

SELECT * INTO DataBase.dbo.BackUpTable FROM SourceDataBase.dbo.SourceTable

Backup a single table with its data from a database in sql server 2008使用sql server 2008中数据库中的数据备份单个表

SELECT * INTO  [dbo].[tbl_NewTable] 
FROM [dbo].[tbl_OldTable]

Put the table in its own filegroup.将表放在其自己的文件组中。 You can then use regular SQL Server built in backup to backup the filegroup in which in effect backs up the table.然后,您可以使用常规 SQL Server 内置备份来备份实际上备份表的文件组。

To backup a filegroup see: https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-files-and-filegroups-sql-server要备份文件组,请参阅: https : //docs.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-files-and-filegroups-sql-server

To create a table on a non-default filegroup (its easy) see: Create a table on a filegroup other than the default要在非默认文件组上创建表(很容易),请参阅:在非默认文件组上创建表

This query run for me ( for MySQL).这个查询为我运行(对于 MySQL)。 mytable_backup must be present before this query run. mytable_backup 在此查询运行之前必须存在。

insert into mytable_backup select * from mytable

Another approach you can take if you need to back up a single table out of multiple tables in a database is:如果您需要从数据库中的多个表中备份单个表,您可以采用的另一种方法是:

  1. Generate script of specific table(s) from a database (Right-click database, click Task > Generate Scripts...从数据库生成特定表的脚本(右键单击数据库,单击任务 > 生成脚本...

  2. Run the script in the query editor.在查询编辑器中运行脚本。 You must change/add the first line (USE DatabaseName) in the script to a new database, to avoid getting the "Database already exists" error.您必须将脚本中的第一行 (USE DatabaseName) 更改/添加到新数据库,以避免出现“数据库已存在”错误。

  3. Right-click on the newly created database, and click on Task > Back Up... The backup will contain the selected table(s) from the original database.右键单击新创建的数据库,然后单击任务 > 备份...备份将包含来自原始数据库的选定表。

为了在本地文件系统上的文件中获取副本,Windows 开始按钮菜单中的这个摇摇欲坠的实用程序起作用了:“C:\\Program Files (x86)\\Microsoft SQL Server\\110\\DTS\\Binn\\DTSWizard.exe”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM