SQL の INSERT INTO ステートメント内で WHERE ステートメントを使用できますか?
これが私が現在やろうとしていることです。
INSERT INTO AssetComponents(ComponentID, ComponentDescription)
VALUES (@ComponentType, @CompDescr)
WHERE (AssetTagNumber = @TagNo)
しかし、コンパイラは WHERE ステートメントに問題があります。
ありがとう
** *更新* ***
これは、私がこれまでに修正を加えて使用している完全なコードです
protected void AddBut_Click(object sender, EventArgs e)
{
//still passing the Asset tag number forward here
var ID = Request.QueryString["Id"];
string sql = "";
using (SqlConnection con = new SqlConnection("Data Source: *******************)
{
sql = "IF (AssetTagNumber = @TagNo) " +
"BEGIN " +
"INSERT INTO AssetComponents(ComponentID, ComponentDescription) " +
"VALUES (@ComponentType, @CompDescr) " +
"END ";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
// try
// {
cmd.Parameters.AddWithValue("@TagNo", ID);
cmd.Parameters.AddWithValue("@ComponentType", TypeDDL.Text.Trim());
cmd.Parameters.AddWithValue("@CompDescr", DescrTB.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("ComponentDetails.aspx");
// }
// catch (SqlException ex) { MessageBox.Show(" "); }
// catch (Exception ex) { MessageBox.Show(" "); }
}
}
}
申し訳ありませんが、最初は十分に明確ではありませんでした。
私がやりたいことは、このレコードに既存の PK があるかどうかを示す句を含む新しいレコードを挿入し、このキーを使用してそのレコードの別のエントリを挿入することです
もう一度お詫び