簡體   English   中英

帶WHERE語句的SQL INSERT INTO語句

[英]SQL INSERT INTO statement with WHERE Statement

您可以在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,則使用此鍵為該記錄插入另一個條目

再次道歉

您為什么不只使用IF子句?

IF (AssetTagNumber = @TagNo)
BEGIN
    INSERT INTO AssetComponents(ComponentID, ComponentDescription)
    VALUES (@ComponentType, @CompDescr)
END

對於帶有WHERE腳本的語句,其外觀應類似於:

INSERT INTO AssetComponents(ComponentID, ComponentDescription)
SELECT @ComponentType, @CompDescr
FROM <table>
WHERE (AssetTagNumber = @TagNo)

您不能像這樣“有條件地插入”。 WHERE子句僅可用於SELECTUPDATEDELETE

要檢查是否需要INSERT新記錄,需要使用IF ,如下所示:

IF NOT EXISTS (SELECT ...)
    INSERT INTO ...
if EXISTS (select * from AssetComponents where AssetTagNumber = @TagNo)

Begin

INSERT INTO AssetComponents(ComponentID, ComponentDescription)
(@ComponentType, @CompDescr)

End

用這個:

UPDATE AssetComponents     
Set ComponentID=@ComponentType, ComponentDescription=@CompDesc    
Where AssetTagNumber = @TagNo

WHERE子句有助於過濾記錄,因此最好與SELECTUPDATE 對於INSERT我們通常使用IF NOT EXISTS子句。

參見示例:

  1. http://social.msdn.microsoft.com/Forums/sqlserver/zh-CN/724ab6f3-413f-4c59-9b68-776f3ecfa899/insert-if-not-exists-into

  2. http://msdn.microsoft.com/en-us/library/ms174335.aspx

另外,在查看了文檔之后 ,我們可以看到INSERT語句支持WHERE子句。

如果記錄已經存在,則可以使用INSERT操作執行UPDATEDELETE

您可以嘗試:

IF NOT EXISTS (SELECT * FROM AssetComponents WHERE (AssetTagNumber = @TagNo))
 INSERT INTO AssetComponents(ComponentID, ComponentDescription) VALUES (@ComponentType, @CompDescr)
ELSE
 --UPDATE fields

考慮INSERT SELECT:

INSERT INTO AssetComponents(ComponentID, ComponentDescription)
SELECT [fill out here] AS ComponentID,
       [fill out here] AS ComponentDescription
FROM somesource
WHERE [condition]

這是MS SQL Server的特長,因此無法在其他數據庫中使用。 這就要求您的數據已經在另一個表或其他可查詢的源中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM