我认为我的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);
答案 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
子句应包含AND
或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列。