如何将一行从一个 SQL Server 表复制到另一个表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57168/
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 copy a row from one SQL Server table to another
提问by rp.
I have two identical tables and need to copy rows from table to another. What is the best way to do that? (I need to programmatically copy just a few rows, I don't need to use the bulk copy utility).
我有两个相同的表,需要将行从表复制到另一个表。最好的方法是什么?(我只需要以编程方式复制几行,我不需要使用批量复制实用程序)。
回答by Scott Nichols
As long as there are no identity columns you can just
只要没有标识列,您就可以
INSERT INTO TableNew
SELECT * FROM TableOld
WHERE [Conditions]
回答by Michael Haren
Alternative syntax:
替代语法:
INSERT tbl (Col1, Col2, ..., ColN)
SELECT Col1, Col2, ..., ColN
FROM Tbl2
WHERE ...
The select query can (of course) include expressions, case statements, constants/literals, etc.
select 查询可以(当然)包括表达式、case 语句、常量/文字等。
回答by ScottStonehouse
Jarrett's answer creates a new table.
Jarrett 的回答创建了一个新表。
Scott's answer inserts into an existing table with the same structure.
Scott 的答案插入到具有相同结构的现有表中。
You can also insert into a table with different structure:
您还可以插入具有不同结构的表:
INSERT Table2
(columnX, columnY)
SELECT column1, column2 FROM Table1
WHERE [Conditions]
回答by Kaniu
INSERT INTO DestTable
SELECT * FROM SourceTable
WHERE ...
works in SQL Server
在 SQL Server 中工作
回答by Jarrett Meyer
SELECT * INTO < new_table > FROM < existing_table > WHERE < clause >