reference field generated in stored procedure sql server

0

I'm doing a stored procedure, to add some data to a report in crystal reports. I'm doing the following:

(df.valor_unitario * df.cantidad) as vtotality, /*realiza la operacion y el resultado lo muestra en el campo vtotality */
(select sum(vtotality)) as valor_totalist, /*requiero sumar los valores que toma este campo*/

The field that receives the values does not exist in any table in the database, only in the stored procedure, but I have not been able to reference it so that the values it takes can be added.

the present is to add the prices of several articles and generate a total

    
asked by Andrex_11 24.04.2018 в 18:13
source

1 answer

0

I understand that you are not talking about a field but a variable within the stored procedure, so the first thing is to declare it, every variable is defined with the @ at the beginning of the name and we must indicate the type of data. The next thing, if you're looking to save the product value of the sum of certain columns in a query, is to do this:

DECLARE @vtotality NUMERIC(15,2)

SELECT @vtotality = SUM(valor_unitario * cantidad) 
       FROM tu_tabla 
       where ariculo in ('Art1', 'Art2')

SELECT  @vtotality

Once declared, @vtotality can be used as part of any SELECT even of a query with some other table.

    
answered by 24.04.2018 в 18:40