Incorrect syntax error near the keyword 'FROM'

1

This code generates the following error:

  

Msg 156, Level 15, State 1, Total ProcedureOrder Order, Line 7 Incorrect syntax near the keyword 'FROM'.

CREATE FUNCTION dbo.totalGastoPedido(@id_mesa int)
RETURNS int
AS
BEGIN
       DECLARE @Total money
       SET @Total = (SELECT SUM(ped.ValorPagar))
       FROM Pedido ped
       where @id_mesa = IdMesa
       and ped.Data>'2016-02-10' and ped.Data<'2016-02-10'   

    RETURN @Total
END
    
asked by anonymous 29.06.2016 / 01:10

1 answer

1

Your select has a syntax error, when you make a select to set the value of the select in some variable ( SET @Total =) , you need to wrap all the select in SET @Total = (select campo from tabela) , in your case you are closing the relationship before from (SELECT SUM(ped.ValorPagar)) , this last parent has to be removed and inserted at the end of the select.

create FUNCTION dbo.totalGastoPedido(@id_mesa int)
RETURNS int
AS
BEGIN  

DECLARE @Total money

 declare @Pedido table
       (
         IdMesa int,
         ValorPagar numeric(18,2)
       )

       insert into @Pedido values
       (1,122.32),
       (1,12.32),
       (1,2.32),
       (4,1.32),
       (5,2.32)

       SET @Total = (SELECT SUM(ped.ValorPagar) -- removar o parenteses daqui e coloque no final da query
       FROM @Pedido ped
       where @id_mesa = IdMesa
       --and ped.Data>'2016-02-10' and ped.Data<'2016-02-10' --- comentado para facilita na buscar
       ) -- Aqui....

    RETURN @Total
END
    
29.06.2016 / 14:09