SQL Server的多个where子句

本文关键字:where 子句 Server SQL | 更新日期: 2023-09-27 18:26:52

我编写这个查询是为了检查重复项,但无法获得多个工作位置。","附近的语法错误就是我得到的。

private static bool duplicate(Dictionary<TableKey, string> entry)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        // TODO: Make changes to this command when new additions/modifications to the DB structure occur  
        string commandText = @"Select Barcode, FullName, Location, Notes, Category From Inventory " +
                             @"Where Barcode = @barcode, FullName = @fullname, Location = @location, " +
                             @"Notes = @notes, Category = @category";
        SqlCommand command = new SqlCommand(commandText, connection);
        command.Parameters.AddWithValue("@barcode", entry[TableKey.Barcode]);
        command.Parameters.AddWithValue("@fullname", entry[TableKey.FullName]);
        command.Parameters.AddWithValue("@location", entry[TableKey.Location]);
        command.Parameters.AddWithValue("@notes", entry[TableKey.Notes]);
        command.Parameters.AddWithValue("@category", entry[TableKey.Category]);
        command.Connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        if (reader.HasRows)
            return true;
        else
            return false;
    }

这是我的问题:

@"Where Barcode = @barcode, FullName = @fullname, Location = @location, " +
                             @"Notes = @notes, Category = @category";

如何获得多个where?我尝试过"and"answers"and"

SQL Server的多个where子句

这应该可以正常工作。

@"Where Barcode = @barcode AND FullName = @fullname AND Location = @location AND " +
      @"Notes = @notes AND Category = @category";

查找重复

SELECT COUNT(*) as cnt, * 
From Inventory
GROUP  BY Barcode, FullName, Location, Notes, Category
HAVING COUNT(*) > 1

您应该使用

条件

下面是使用AND 的示例

"Where Barcode = @barcode AND FullName = @fullname AND Location = @location AND " +
                             @"Notes = @notes AND Category = @category";