Incorrect syntax error near the keyword 'FROM'

Asked

Viewed 2,356 times

1

This code generates the following error:

Msg 156, Level 15, State 1, Procedure totalGastoPedido, 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
  • 1

    Can you explain the question better? What’s the problem, what’s the mistake? What are you trying to do?

  • This is in sql server?

  • I’m sorry I didn’t write in full, I plan to make a Function that receives the Id from the table and returns the total spent from that table in a given period

  • And yes it is SQL SERVER, I am begginer in sql server

  • When having compile the error appears in FROM

  • Edit the question, scan the error message, sql server tag tbm.

  • Msg 156, Level 15, State 1, Procedure totalGastoPedido, Line 7 Incorrect syntax near the keyword 'FROM'.

  • @Alexandru can [Dit] the question and gather this information?

Show 3 more comments

1 answer

1

Your select this with a syntax error, when you select to set the value of select in some variable ( SET @Total =) , you need to involve all select between parentheses SET @Total = (select campo from tabela), in your case you are closing the relationship before the from (SELECT SUM(ped.ValorPagar)), this last parenthesis has to be removed and inserted at the end of 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
  • 1

    Thank you very much, Marco, I discovered yesterday the mistake, Hug

Browser other questions tagged

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