How to use @ in SQL SERVER?

1

I need to retrieve the quantity of product with price between 0 and 10. There is an error in the @W_NUM variable of the select command.

BEGIN TRANSACTION;
DECLARE @W_P1 int;
DECLARE @W_P2 int;
DECLARE @W_NUM int;
set @W_P1=0;
SET @W_P2=10;
SET @W_NUM=0;

BEGIN 
WHILE (@W_P2<300)
SELECT COUNT(P_CODE)INTO @W_NUM FROM PRODUCTT WHERE P_PRICE>@W_P1 AND P_PRICE<@W_P2;
PRINT('EXISTEM'+@W_NUM+'PRODUTOS COM O PRECO ENTRE'+@W_P1+' E '+@W_P2)
@W_P1=@W_P1+1;
@W_P2=@W_P2+50;
END
COMMIT
    
asked by anonymous 24.11.2016 / 00:43

2 answers

2

Try

-- código #1 v2
DECLARE @W_P1 int;
DECLARE @W_P2 int;
DECLARE @W_NUM int;
set @W_P1= 0;
set @W_P2= 10;
set @W_NUM= 0;

WHILE (@W_P2 < 300)
  BEGIN
  SET @W_NUM= (SELECT COUNT(P_CODE) FROM PRODUCTT 
                 WHERE P_PRICE > @W_P1 AND P_PRICE < @W_P2);
  PRINT 'EXISTEM ' + Cast(@W_NUM as varchar(10)) +
         ' PRODUTOS COM O PREÇO ENTRE ' + Cast(@W_P1 as varchar(10)) +
         ' E ' + Cast(@W_P2 as varchar(10));
  --
  set @W_P1= @W_P1 + 1;
  set @W_P2= @W_P2 + 50;
  END;
    
24.11.2016 / 19:12
3

Try to:

SELECT @W_NUM = COUNT(P_CODE) FROM PRODUCTT WHERE P_PRICE>@W_P1 AND P_PRICE<@W_P2;
    
24.11.2016 / 00:58