INSERT语句中的错误消息

时间:2012-07-12 14:10:33

标签: c# sql sql-server tsql

我认为我的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 个答案:

答案 0 :(得分: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语句中指定其他三列。

答案 1 :(得分: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四列,而不仅仅是一列。

答案 2 :(得分:1)

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

你试图从表Appointment传入INSERT的哪些字段,哪些字段应该来自变量?

答案 3 :(得分:0)

您的WHERE子句应包含ANDOR

例如:

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列。