如何在 SQL Server 2008 中创建具有新名称的重复表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3241801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create duplicate table with new name in SQL Server 2008
提问by Eyla
How do I create a duplicate table with only the structure duplicated with a new name in SQL server 2008?
如何在 SQL Server 2008 中使用新名称创建仅具有重复结构的重复表?
I have table with 45 fields so I want to create new with same structure but new name.
我有 45 个字段的表,所以我想创建具有相同结构但新名称的新表。
回答by Avalanchis
Right click on the table in SQL Management Studio.
右键单击 SQL Management Studio 中的表。
Select Script... Create to... New Query Window.
选择脚本...创建到...新建查询窗口。
This will generate a script to recreate the table in a new query window.
这将生成一个脚本以在新查询窗口中重新创建表。
Change the name of the table in the script to whatever you want the new table to be named.
将脚本中表的名称更改为您希望新表命名的任何名称。
Execute the script.
执行脚本。
回答by Conrad Frix
SELECT *
INTO target
FROM source
WHERE 1 = 2
回答by Rousonur Jaman
Here, I will show you 2 different implementation:
在这里,我将向您展示 2 种不同的实现:
First:
第一的:
If you just need to create a duplicate table then just run the command:
如果您只需要创建一个重复表,那么只需运行以下命令:
SELECT top 0 * INTO [dbo].[DuplicateTable]
FROM [dbo].[MainTable]
Of course, it doesn't work completely. constraints don't get copied, nor do primary keys, or default values. The command only creates a new table with the same column structure and if you want to insert data into the new table.
当然,它并不完全有效。约束不会被复制,主键或默认值也不会被复制。该命令仅创建具有相同列结构的新表,并且如果要将数据插入到新表中。
Second (recommended):
第二个(推荐):
But If you want to duplicate the table with all its constraints & keys follows this below steps:
但是,如果您想复制带有所有约束和键的表,请按照以下步骤操作:
- Open the database in SQL Management Studio.
- Right-click on the table that you want to duplicate.
- Select Script Table as -> Create to -> New Query Editor Window. This will generate a script to recreate the table in a new query window.
- Change the table name and relative keys & constraints in the script.
- Execute the script.
- 在 SQL Management Studio 中打开数据库。
- 右键单击要复制的表。
- 选择 Script Table as -> Create to -> New Query Editor Window。这将生成一个脚本以在新查询窗口中重新创建表。
- 更改脚本中的表名和相关键和约束。
- 执行脚本。
回答by Akhil Singh
SELECT * INTO newtable FROM oldtable where 1=2
where 1=2, this statement is use when only Copy complete structure of a
table in sql without Copying data of table
SELECT * INTO newtable FROM oldtable
create table with data you can use this statement
回答by user8068654
For creating a new table from existing table
用于从现有表创建新表
SELECT * INTO New_table FROM Old_Table
SELECT * INTO New_table FROM Old_Table
For Inserting Data from one table another table
用于从一个表插入另一个表中的数据
Insert into Table_Name2 select top 1 * from Table_Name1
插入到 Table_Name2 中 select top 1 * from Table_Name1
回答by Harshal SG
SELECT * INTO table2 FROM table1;
回答by murali krishna
I have used this query it is created new table with existing data.
我使用了这个查询,它使用现有数据创建了新表。
Query : select * into [newtablename] from [existingtable]
查询:select * into [newtablename] from [existingtable]
Here is the link Microsoft instructions. https://docs.microsoft.com/en-us/sql/relational-databases/tables/duplicate-tables?view=sql-server-2017
这是 Microsoft 说明的链接。 https://docs.microsoft.com/en-us/sql/relational-databases/tables/duplicate-tables?view=sql-server-2017
回答by Bojangles
My SQL Server Management Studio keeps asking me how I can make it better, I have an idea! The ability to highlight a table and then, ctrl C, ctrl V! would be great and the answer to this question at the same time!
我的 SQL Server Management Studio 一直问我如何使它变得更好,我有一个想法!能够突出显示表格,然后按 ctrl C,按 ctrl V!会很棒,同时也是这个问题的答案!