[英]Insert Statement Based on Select Statement with Multiple Columns
I need to create a static "copy" of values from one table and store them in another. 我需要从一个表中创建值的静态“副本”,并将其存储在另一个表中。
The best way I know how to do this is to get the values from the db to my application, loop through them and concatenate multiple INSERT
statements, then execute the query. 我知道如何执行此操作的最好方法是将值从db获取到我的应用程序,遍历它们并连接多个
INSERT
语句,然后执行查询。
Is there a way to do this without the round trip between application and database? 有没有办法在应用程序和数据库之间不进行往返操作?
I was hoping something like this would work: 我希望这样的事情可以工作:
INSERT INTO dbo.StudentProject
VALUES (SELECT StudentID, ProjectID
FROM dbo.StudentProjectSimulation
WHERE SimulationID = 1)
but it doesn't. 但事实并非如此。
(everything is an int
so no worries there) (一切都是
int
因此不必担心)
You're very close: 您非常接近:
INSERT INTO dbo.StudentProject
SELECT StudentID, ProjectID
FROM dbo.StudentProjectSimulation
WHERE SimulationID = 1
Just drop values
from the insert into
clause. 只需从
insert into
子句中删除values
。
Insert Into dbo.StudentProject
Select StudentID, ProjectID
From dbo.StudentProjectSimulation
Where SimulationID = 1;
Alternatively to explicitly define the columns to insert into use the following where you substitute Student_Col
and ProjectID_Col
for appropriate column names in dbo.StudentProject
或者明确定义列,你代替插入到使用以下
Student_Col
和ProjectID_Col
在适当的列名dbo.StudentProject
Insert Into dbo.StudentProject
(
Student_Col,
ProjectID_Col
)
Select
StudentID,
ProjectID
From dbo.StudentProjectSimulation
Where SimulationID = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.