SQL Server - 创建数据库表的副本并将其放在同一个数据库中? [英] SQL Server - Create a copy of a database table and place it in the same database?

查看:34
本文介绍了SQL Server - 创建数据库表的副本并将其放在同一个数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库 DB 中有一个表 ABC.我想在同一个数据库中创建名称为 ABC_1、ABC_2、ABC_3 的 ABC 副本.我如何使用 Management Studio(最好)或 SQL 查询来做到这一点?

I have a table ABC in a database DB. I want to create copies of ABC with names ABC_1, ABC_2, ABC_3 in the same DB. How can I do that using either Management Studio (preferably) or SQL queries ?

这适用于 SQL Server 2008 R2.

This is for SQL Server 2008 R2.

推荐答案

使用SELECT ... INTO:

SELECT *
INTO ABC_1
FROM ABC;

这将创建一个新表 ABC_1,它具有与 ABC 相同的列结构并包含相同的数据.但是,约束(例如键、默认值)不会被复制.

This will create a new table ABC_1 that has the same column structure as ABC and contains the same data. Constraints (e.g. keys, default values), however, are -not- copied.

您可以每次使用不同的表名多次运行此查询.

You can run this query multiple times with a different table name each time.

如果不需要复制数据,只是新建一个列结构相同的空表,添加一个带有falsy表达式的WHERE子句:

If you don't need to copy the data, only to create a new empty table with the same column structure, add a WHERE clause with a falsy expression:

SELECT *
INTO ABC_1
FROM ABC
WHERE 1 <> 1;

这篇关于SQL Server - 创建数据库表的副本并将其放在同一个数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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