UPDATE in a query with multiple entries

Asked

Viewed 90 times

3

I have a query where I have a SELECT from a client ID, and an order ID. Later I perform an UPDATE in a table, changing the tax rate on the selected order.

The problem is that a customer may have multiple orders. When I place several order Ids.... But how can I actually do a query to update these multiple entries?

Here I have the SELECT:

SELECT
  p.idPedido,
  c.nome,
  p.dataPagamento,
  p.valor,
  p.taxaImposto
FROM
  Pedidos p
JOIN Clientes c ON (c.id = p.idCliente)
WHERE
  c.id IN ({txtClienteId.Text})
AND
  p.idPedido IN ({txtPedidoId.Text})

And here the update:

UPDATE
  Pedidos
SET
  taxaImposto = @taxa
WHERE
  idPedido = @idPedido
AND
  idCliente = @idCliente;

cmd.Parameters.Add("@taxa", SqlDbType.Float).Value = Convert.ToDouble(txtTaxaImposto.Text);
cmd.Parameters.Add("@idPedido", SqlDbType.BigInt).Value = Convert.ToInt32(txtPedidoId.Text);
cmd.Parameters.Add("@idCliente", SqlDbType.BigInt).Value = Convert.ToInt32(txtClienteId.Text);

UPDATE works normally when I only have one entry in the Orders ID, but I want to make it possible to update to multiple entries, from the same customer, that is, insert a customer ID, a Fee, and n Orders ID’s.

  • All records of the interval WHERE c.id IN ({txtClienteId.Text}) AND p.idPedido IN ({txtPedidoId.Text}) will have the same rate?

  • Yes, the fee will be the same for all selected orders

  • There is selection of customers and/or orders to make the UPDATE? Or all who were returned by SELECT will be updated?

  • The user will enter the customer ID, the fee, and 1 or multiple orders, and the orders will always be for the same customer.

2 answers

3


Taking into account the information on the issue and comments, maybe this way you can solve the problem:

string strSql = string.Format(@"
                UPDATE  Pedidos
                SET     taxaImposto = @taxa
                WHERE   idPedido    IN ({0})
                    AND idCliente   = @idCliente", txtPedidoId.Text);

// A variável "strSql" será utilizada para criar o "SqlCommand"

cmd.Parameters.Add("@taxa", SqlDbType.Float).Value = Convert.ToDouble(txtTaxaImposto.Text);
cmd.Parameters.Add("@idCliente", SqlDbType.BigInt).Value = Convert.ToInt32(txtClienteId.Text);

Basically, the UPDATE will be executed for a specific client (as it indicated that the ID the customer is indicated and the orders will be for that same customer) but for several orders (those indicated in txtPedidoId.Text).

  • 1

    It worked! Thank you so much for the solution John!

0

Good morning, you are doing the update and select separately, to perform all the updates at the same time you could perform the update and select in a single query

UPDATE
  Pedidos
SET
  Pedidos.taxaImposto = {txtTaxaImposto.Text}
FROM
  Pedidos p
  INNER JOIN Clientes c ON (c.id = p.idCliente)
WHERE
  c.id IN ({txtClienteId.Text})
AND
  p.idPedido IN ({txtPedidoId.Text})

In this case you can put the order list in the field {txtPedidoId.Text} and he would be responsible for updating all orders on this list.

  • In fact, the process is being done in two parts. A START button, which makes the SELECT and displays the table data on the screen, and a second RUN button, which actually performs the UPDATE. For this reason they are separated...

  • The problem happens when I place more than 1 request, for example the textbox filled with 10, 15, 22. When I have only 10, it works normally, but when it has more than one input, it returns an error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.