0

我开发了一个非常复杂的查询,现在很难修改它以使用 INSERT INTO 语句。

查询(VS语法):

           @"SELECT s.TagID, se.SessionID, " +
           " @todaysDate " +
           " AS ScanningTime " +
           " FROM (((Student s " +
           " LEFT JOIN [CourseID-ModuleID] cm ON s.CourseID = cm.CourseID) " +
           " LEFT JOIN [ModuleID-SessionID] ms ON cm.ModuleID = ms.ModuleID) " +
           " LEFT JOIN [Session] se ON ms.SessionID = se.SessionID) " +
           " WHERE s.TagID = @tagNo " +
           " AND se.SessionDate = cast(getdate() as date) " +
           " AND se.SessionTimeStart <= @Plus30Min " +
           " AND se.SessionTimeEnd >= @Plus30Min ";

查询运行完美,但我想将查询结果放入Attendance数据库中已经存在的空表中。

Attendance table data types

TagID - int
SessionID - varchar
ScanningTime - datetime.

请注意,考勤表的数据类型与其他表数据类型相匹配。

我尝试在 SELECT 语句之前放置“INSERT INTO Attendance (s.TagID, se.SessionID, ScanningTime)”,但是我的查询没有产生任何结果。

修改我的查询以包含 INSERT INTO 语句的正确方法是什么?谢谢

编辑:

您所有的答案和建议都非常有帮助。谢谢!

4

3 回答 3

1

删除(s.TagID, se.SessionID, ScanningTime)INSERT INTO Attendance在您选择之前添加

@"INSERT INTO Attendance
 SELECT s.TagID, se.SessionID, "
 etc...
于 2013-08-13T13:38:43.707 回答
0
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;
于 2013-08-13T13:38:47.943 回答
0

您可以参考以下代码:

@"INSERT INTO Attendance(
   TagID , 
   SessionID,
   ScanningTime )
SELECT s.TagID, se.SessionID, " +
           " @todaysDate " +
           " AS ScanningTime " +
           " FROM (((Student s " +
           " LEFT JOIN [CourseID-ModuleID] cm ON s.CourseID = cm.CourseID) " +
           " LEFT JOIN [ModuleID-SessionID] ms ON cm.ModuleID = ms.ModuleID) " +
           " LEFT JOIN [Session] se ON ms.SessionID = se.SessionID) " +
           " WHERE s.TagID = @tagNo " +
           " AND se.SessionDate = cast(getdate() as date) " +
           " AND se.SessionTimeStart <= @Plus30Min " +
           " AND se.SessionTimeEnd >= @Plus30Min ";
于 2013-08-13T13:46:32.180 回答