Make Calculations in Sql Server Database

Asked

Viewed 625 times

0

I am making an application with Sql Server database and I want the database to work the calculations and not the application, but I’m having difficulties to do, I hope someone can help me in this issue.

inserir a descrição da imagem aqui

These are the columns of the Product table, the data were entered manually, the columns Totalbuy and Totalsell are computed (Stock * Purchase price) and (Stock * Sale price), respectively. Now comes the difficulty, I want the value in the column Profit is also automatic being (Totalsale -Totalpurchase)

I made the following Insert Procedure: inserir a descrição da imagem aqui

  • I don’t quite understand what you want?

  • I want to calculate value in the Profit column also automatically being (Totalvenda -Totalcompra). Let the calculation be done in the table. And not in the application.

  • 1

    Wouldn’t just create another calculated column !?

  • No Motta, because the columns Totalbuy and Totalsell are already Calculated Columns, I’ve tried this and it didn’t work.

  • Do not just make a column calculated as follows: Lucro = (ValorVenda - ValorCompra) * Estoque?

2 answers

1


ALTER TABLE Produto
ADD Lucro AS ((ValorVenda - ValorCompra) * Estoque);
  • Thanks Marcelo Uchimura, it worked!

0

I don’t quite understand what you want, but you can use SUM to display this information whenever you need, without having to store the total.

SELECT Sum(VALOR_COMPRA - VALOR_VENDA) FROM ... ;

Or you can insert it simply and quickly, just like in this example:

INSERT INTO TESTE (valorcompra, valorvenda, lucro) VALUES (155,195, valorvenda - valorcompra)

I believe I can also use inside the Procedure!

values {
...
...
@VALOR_COMPRA, 
@VALOR_VENDA,
Lucro = @VALOR_COMPRA - @VALOR_VENDA

}
  • Marcos Vinicius is almost that, but I want to calculate this difference between Totalbuy and Totalsell that are not in the Last, this information is only found in the table.

  • Have you tried this? Without passing the two columns you want by parameters Profit = TOTAL_COMPRA - TOTAL_VENDA

Browser other questions tagged

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