Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有WHERE语句的SQL INSERT INTO语句_C#_Asp.net_Sql_Sql Server - Fatal编程技术网

C# 带有WHERE语句的SQL INSERT INTO语句

C# 带有WHERE语句的SQL INSERT INTO语句,c#,asp.net,sql,sql-server,C#,Asp.net,Sql,Sql Server,您可以在SQL中的INSERT INTO语句中使用WHERE语句吗 以下是我目前正在尝试做的事情 INSERT INTO AssetComponents(ComponentID, ComponentDescription) VALUES (@ComponentType, @CompDescr) WHERE (AssetTagNumber = @TagNo) 但是编译器对WHERE语句有问题 谢谢 ***更新*** 这是我目前使用的完整代码,并进行了修改 protected void Ad

您可以在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)
插入到AssetComponents中(ComponentID,ComponentDescription)
选择@ComponentType、@CompDescr
从…起
其中(AssetTagNumber=@TagNo)
您不能像那样“有条件地插入”。
WHERE
子句仅适用于
SELECT
UPDATE
DELETE

要检查是否需要插入新记录,需要使用
IF
,如下所示:

IF NOT EXISTS (SELECT ...)
    INSERT INTO ...
使用以下命令:

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

WHERE
子句有助于筛选记录,因此它最好与
SELECT
UPDATE
一起使用。对于
插入
我们通常使用
如果不存在
子句

见示例:

  • 另外,在之后,我们可以看到
    INSERT
    语句对
    WHERE
    子句有NO支持

    如果记录已经存在,您可以使用
    INSERT
    操作执行所有
    UPDATE
    DELETE

    您可以尝试以下方式:

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

    考虑插入选择:

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

    这是MS SQL Server的一个特性,因此在其他数据库中不起作用。它要求您的数据已经存在于另一个表或其他您可以查询的源中。

    为什么不使用Update代替您忘记了选择…您的插入不知道从何处获取数据。WHERE无法使用插入。如果在插入之前无法使用更新,因为它是一个新记录,在这种情况下,选择在哪里有用?是什么让你们都认为他确实想要更新?如果他想有条件地插入呢?谢谢,我不知道这个子句,在多年的忽略之后,我对SQL还是新手。。这更有意义!这是行不通的。AssetTagnNumber是我的数据库中导致此错误的列--->“无效列名'AssetTagnNumber'。请尝试编辑的答案,希望它能工作,如果存在或不存在,根据您的要求。谢谢,我也会尝试此功能。这似乎更符合我的需要,谢谢!
    INSERT INTO AssetComponents(ComponentID, ComponentDescription)
    SELECT [fill out here] AS ComponentID,
           [fill out here] AS ComponentDescription
    FROM somesource
    WHERE [condition]