如果存在则更新数据

时间:2018-02-01 13:12:34

标签: sql-server vb.net

如果产品尺寸存在,我可以更新数据。假设表中有一个product_sstock(大小为7),其中product_stocks为15,我想添加大小。例如,我在product_size(尺寸7)上添加了产品库存(15个),因此7号产品库存将为30个。我的问题是如果产品尺寸不存在,我该如何添加数据?

   Public Sub addProductsExtension()
        Try
            dbConnection()
            search_query = "SELECT * FROM tblproducts_extension WHERE product_code = @product_code;"
            command = New SqlCommand
            With command
                .Connection = connection
                .CommandText = search_query
                .Parameters.Clear()
                .Parameters.AddWithValue("@product_code", productsView.txtPCode.Text)
                dataReader = .ExecuteReader()
                If dataReader.HasRows Then
                    While dataReader.Read
                        updateExtension()
                        MsgBox("Updated stocks.")
                    End While
                Else
                    addExtension()
                End If
            End With
        Catch ex As SqlException
            MsgBox("Error : " + ex.Message)
        Finally
            connection.Close()
            command.Dispose()
        End Try
    End Sub 

    'these are the codes for adding size and stocks
    Public Sub addExtension()
        Try
            dbConnection()
            insert_query = "INSERT INTO tblproducts_extension(product_size, product_stocks, product_code) VALUES(@product_size, @product_stocks, @product_code);"
            command = New SqlCommand
            With command
                .Connection = connection
                .CommandText = insert_query
                .Parameters.Clear()
                .Parameters.AddWithValue("@product_size", productsView.comboSize.SelectedItem)
                .Parameters.AddWithValue("@product_stocks", productsView.txtProductStocks.Text)
                .Parameters.AddWithValue("@product_code", productsView.txtPCode.Text)
                result = .ExecuteNonQuery()
                If result = 0 Then
                    MsgBox("Error in adding data.")
                Else
                    MsgBox("Successfully added data.")
                End If
            End With
        Catch ex As SqlException
            MsgBox("Error : " + ex.Message)
        Finally
            connection.Close()
            command.Dispose()
        End Try
    End Sub

    'these are the codes for updating stocks if the product size is exists
Public Sub updateExtension()
        Try
            dbConnection()
            update_query = "UPDATE tblproducts_extension SET product_stocks = product_stocks + @product_stocks WHERE product_size = @product_size AND product_code = @product_code;"
            command = New SqlCommand
            With command
                .Connection = connection
                .CommandText = update_query
                .Parameters.Clear()
                .Parameters.AddWithValue("@product_size", productsView.comboSize.SelectedItem)
                .Parameters.AddWithValue("@product_stocks", productsView.txtProductStocks.Text)
                .Parameters.AddWithValue("@product_code", productsView.txtPCode.Text)
                .ExecuteNonQuery()
            End With
        Catch ex As SqlException
            MsgBox("Error : " + ex.Message)
        Finally
            connection.Close()
            command.Dispose()
        End Try
    End Sub

1 个答案:

答案 0 :(得分:4)

您当前的代码无法正常工作,因为在初始SELECT中,您只搜索product_code。这还不足以决定是否要添加或更新记录,因为您需要将新数量添加到具有特定大小的产品。因此,您只需将此其他参数添加到您的选择查询中(就像您在UPDATE中所做的那样),您的代码应该按原样运行。

但是,我们可以使用MERGE关键字,只需一次调用即可更新或插入数据。

string query = @"MERGE tblproducts_extension T
                 USING (1 as dummy) as S 
                 ON T.product_size = @product_size AND T.product_code = @product_code
                 WHEN NOT MATCHED THEN 
                     INSERT (product_code, product_size, product_stocks)
                     VALUES (@product_code, @product_size, @newQuantity)
                 WHEN MATCHED THEN 
                     UPDATE SET product_stocks = T.product_stocks + @newQuantity;";