Must declare the scalar variable SQL

0

Hi, I'm getting this error and I do not understand why? according to me I am already declaring previously @PTI_R

This is my current code

declare @Iva decimal(5,2) = (.16),
@tasaInteres decimal(10,2) ,
@fechaDispIni date,
@sueldoMensual decimal (20,2),
@dispIni decimal(20,2),
@PTI_R decimal(3,2)

set @PTI_R = 23

declare @PTI decimal(8,2) = @PTI_R * 100;
select @PTI
    
asked by E.Rawrdríguez.Ophanim 25.09.2018 в 17:52
source

1 answer

2

The way you have it written (a single DECLARE with each variable separated by a comma) is a single line of declaration of variables. For this reason, in that line, the variable @PTI_R does not yet exist.

To solve your error, simply declare the variable @PTI in a new DECLARE :

declare @Iva decimal(5,2) = (.16),
        ...las otras variables acá,
        @PTI_R decimal(3,2);

declare @PTI decimal(8,2) = @PTI_R * 100;

Now, here is another problem, and that is that you are assigning the calculation ( @PTI_R * 100 ) before giving a value to @PTI_R , so the result of that will be NULL . You should do:

declare @Iva decimal(5,2) = (.16),
        ...las otras variables acá,
        @PTI_R decimal(3,2);

SET @PTI_R = 23;
declare @PTI decimal(8,2) = @PTI_R * 100;

Finally, a third problem (based on your comment). decimal(3,2) means that it is a data type that has 3 digits in total , 2 of which are for the decimal part (after the comma). Therefore, the maximum number you can store in that type of data is 9,99 , so it throws you an error when trying to assign it the value 23 .

    
answered by 25.09.2018 / 18:03
source