kelly
kelly

Reputation: 117

Error message in INSERT Statement

I believe there is something wrong with my INSERT statement here. When I run my code, the error message shows:

Line: 5
Error: Sys.WebForms.PageRequestManagerServerErrorException: Incorrect syntax near ','.

Is is something wrong in my where clause?

Here is my INSERT statement:

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

This is how I call it from 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);

Upvotes: 1

Views: 478

Answers (4)

SNH
SNH

Reputation: 115

Your WHERE clause should contain AND or OR.

For example:

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";

Also, your INSERT statement contains 4 columns, but you are only returning 1 column in your SELECT statement.

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

Use AND instead of , to separate between the conditions in the WHERE clause. You have to write it this way:

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";

However you will got a new exception after that, since you only select one column to insert into these four columns: SelectedApptDate, SelectedApptStartTime, SelectedApptEndTime, SelectedWeddingPlanner. You have to specify the other three columns as well in the SELECT statement.

Upvotes: 5

Ry-
Ry-

Reputation: 224904

You don't separate WHERE clauses with ,; did you mean 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

You'll also need to SELECT four columns, not just one.

Upvotes: 2

sybkar
sybkar

Reputation: 386

You only have one column in your SELECT statement, yet the INSERT expects four columns. What does the schema look like?

What fields are you trying to pass into your INSERT from the table Appointment, and which are supposed to be coming from variables?

Upvotes: 1

Related Questions