SQL Server SELECT 到现有表 [英] SQL Server SELECT into existing table
问题描述
我正在尝试从一个表中选择一些字段并将它们从存储过程插入到现有表中.这是我正在尝试的:
I am trying to select some fields from one table and insert them into an existing table from a stored procedure. Here is what I am trying:
SELECT col1, col2
INTO dbo.TableTwo
FROM dbo.TableOne
WHERE col3 LIKE @search_key
我认为 SELECT ... INTO ...
用于临时表,这就是为什么我收到 dbo.TableTwo
已经存在的错误.
I think SELECT ... INTO ...
is for temporary tables which is why I get an error that dbo.TableTwo
already exists.
如何将 dbo.TableOne
中的多行插入到 dbo.TableTwo
中?
How can I insert multiple rows from dbo.TableOne
into dbo.TableTwo
?
推荐答案
SELECT ... INTO ...
仅当 INTO 子句中指定的表不存在时才有效 - 否则,您有使用:
SELECT ... INTO ...
only works if the table specified in the INTO clause does not exist - otherwise, you have to use:
INSERT INTO dbo.TABLETWO
SELECT col1, col2
FROM dbo.TABLEONE
WHERE col3 LIKE @search_key
这假设 dbo.TABLETWO 中只有两列 - 否则您需要指定列:
This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
INSERT INTO dbo.TABLETWO
(col1, col2)
SELECT col1, col2
FROM dbo.TABLEONE
WHERE col3 LIKE @search_key
这篇关于SQL Server SELECT 到现有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!