开发者

SQL syntax near WHERE?

I'm a newb here开发者_如何学Go, and it may be because I've been up since yesterday morning, but I can't find my error here in this insert statement. My handler asked me not to parameterize for this training project (it won't be deployed), so no worries for the injection vulnerabilities. Anyway, the query's right, the data types are correct, and the table and field names are spelled correctly. What am I missing here? And is there a better way to find it than just staring at the screen until it comes to you?

protected void BtnSubmit_Click(object sender, EventArgs e)
{
    string x = Request.QueryString["SubId"];
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    string comQuery = "INSERT INTO Submission (Status, StatusComment, StatusValue) VALUES ('" + "decline" + "', '" + TbComments.Text + "', 2) WHERE SubmissionId =" + x;
    using (SqlConnection sqlConn = new SqlConnection(connectionString))
    {
        sqlConn.Open();
        using (SqlCommand comCmd = new SqlCommand(comQuery, sqlConn))
        {
            comCmd.ExecuteNonQuery();
        }
    }
}


An INSERT can't have a WHERE clause. It makes no sense to have one, you're putting data in, not narrowing it down.

If you're trying to change preexisting data, that's an UPDATE, not an INSERT. Here's an example:

"UPDATE Submission
 SET Status='decline', StatusComment='" + TbComments.Text + "', StatusValue = 2
 WHERE SubmissionId = " + x


That is incorrect INSERT syntax. Correct INSERT syntax is:

INSERT INTO tableName (columnList) VALUES (valueList)

columnList and valueList must have same count of items and values must be of type expected by columns.

or

INSERT INTO tableName (columnList)
SELECT columnList2
FROM tableName2
WHERE conditionsFromTable2

columnList and columnList2 must have same count of items of same types. You can use any complicated select joined over multiple tables with condition applied on data from these tables.


You need to use UPDATE, not INSERT INSERT insert new row, therefore WHERE makes no sense


Where clause is not allowed in Insert query. Form your code I guess that you need to use Update query.


You'r trying to INSERT INTO Submission data from TbComments. So you need to SELECT the data from TbComments and then INSERT INTO Submission

string comQuery = 
"INSERT INTO Submission (
     Status, 
     StatusComment, 
     StatusValue) 
 SELECT 
    'decline', 
    TbComments.Text, 
    2) 
 FROM TbComments 
 WHERE SubmissionId =" + x;


So your SQL statement is: "INSERT INTO Submission (Status, StatusComment, StatusValue) VALUES (blah) WHERE SubmissionId =" + x;

The problem is definitely the WHERE. WHERE isn't valid for INSERT - See the MSDN documentation for the Insert command. Since you're filtering by SubmissionId, you probably want to do an UPDATE instead.

As for a better way of finding the problem, learning to use the MSDN documentation is a good step. A quick Google search for "msdn t-sql insert" will give you the page I linked to earlier in this answer. Documentation, experience, Google and Stack Overflow. That's how you find solutions :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜