1

我相信我的 INSERT 语句有问题。当我运行我的代码时,错误消息显示:

行:5
错误:Sys.WebForms.PageRequestManagerServerErrorException:“,”附近的语法不正确。

我的 where 子句有问题吗?

这是我的INSERT声明:

INSERT into AppointmentDetails (SelectedApptDate, SelectedApptStartTime, 
                                SelectedApptEndTime, SelectedWeddingPlanner) 
   SELECT 
       ApptID 
   from 
       Appointment 
   INNER JOIN 
       Appointment ON Appointment.ApptID = AppointmentDetails.ApptID 
   WHERE 
       SelectedApptDate = @SelectedApptDate, 
       SelectedApptStartTime = @SelectedApptStartTime, 
       SelectedApptEndTime = @SelectedApptEndTime, 
       SelectedWeddingPlanner = @SelectedWeddingPlanner

这就是我从 C# 中调用它的方式:

    command.CommandText = "INSERT into AppointmentDetails (SelectedApptDate, SelectedApptStartTime, SelectedApptEndTime, SelectedWeddingPlanner) SELECT ApptID from Appointment INNER JOIN Appointment ON Appointment.ApptID = AppointmentDetails.ApptID WHERE SelectedApptDate = @SelectedApptDate, SelectedApptStartTime = @SelectedApptStartTime, SelectedApptEndTime = @SelectedApptEndTime, SelectedWeddingPlanner = @SelectedWeddingPlanner";

    command.Connection = connection;
    command.Parameters.AddWithValue("@SelectedApptDate", dateSelected);
    command.Parameters.AddWithValue("@SelectedApptStartTime", timeStart);
    command.Parameters.AddWithValue("@SelectedApptEndTime", timeEnd);
    command.Parameters.AddWithValue("@SelectedWeddingPlanner", weddingplanner);
4

4 回答 4

5

使用AND而不是,分隔WHERE子句中的条件。你必须这样写:

INSERT into AppointmentDetails (SelectedApptDate, SelectedApptStartTime,
           SelectedApptEndTime, SelectedWeddingPlanner) 
SELECT ApptID 
from Appointment 
INNER JOIN Appointment ON Appointment.ApptID = AppointmentDetails.ApptID 
WHERE SelectedApptDate = @SelectedApptDate
   AND  SelectedApptStartTime = @SelectedApptStartTime
AND SelectedApptEndTime = @SelectedApptEndTime 
AND  SelectedWeddingPlanner = @SelectedWeddingPlanner";

然而,在那之后你会得到一个新的例外,因为你只选择一列插入到这四列中:SelectedApptDate, SelectedApptStartTime, SelectedApptEndTime, SelectedWeddingPlanner. SELECT您还必须在语句中指定其他三列。

于 2012-07-12T14:13:10.123 回答
2

您不要用;分隔WHERE子句 ,你的意思是AND

INSERT INTO
    AppointmentDetails (SelectedApptDate, SelectedApptStartTime, SelectedApptEndTime, SelectedWeddingPlanner)
    SELECT ApptID FROM Appointment INNER JOIN Appointment ON Appointment.ApptID = AppointmentDetails.ApptID
    WHERE SelectedApptDate = @SelectedApptDate
      AND SelectedApptStartTime = @SelectedApptStartTime
      AND SelectedApptEndTime = @SelectedApptEndTime
      AND SelectedWeddingPlanner = @SelectedWeddingPlanner

您还需要SELECT四列,而不仅仅是一列。

于 2012-07-12T14:12:45.073 回答
1

您的 SELECT 语句中只有一列,但 INSERT 需要四列。架构是什么样的?

您试图从 Appointment 表中将哪些字段传递到您的 INSERT 中,哪些字段应该来自变量?

于 2012-07-12T14:17:10.330 回答
0

您的WHERE子句应包含ANDor OR

例如:

command.CommandText = "INSERT into AppointmentDetails (SelectedApptDate,   
                                                       SelectedApptStartTime,  
                                                       SelectedApptEndTime,  
                                                       SelectedWeddingPlanner)

SELECT ApptID from Appointment 
INNER JOIN Appointment ON Appointment.ApptID = AppointmentDetails.ApptID
WHERE  SelectedApptDate = @SelectedApptDate 
AND SelectedApptStartTime = @SelectedApptStartTime
AND SelectedApptEndTime = @SelectedApptEndTime
AND SelectedWeddingPlanner = @SelectedWeddingPlanner";

此外,您的INSERT语句包含 4 列,但您在SELECT语句中只返回 1 列。

于 2012-07-12T14:19:02.837 回答