create function must be the only statement in the batch

Make sure that this statement is the only the only sql in your query window before you execute it.

Or you can highlight the function declaration and execute


The function needs to be either the only function in the query window OR the only statement in the batch. If there are more statements in the query window, you can make it the only one "in the batch" by surrounding it with GO's.

e.g.

GO
CREATE FUNCTION getLavel(@id int ,@lavel char)
RETURNS date
BEGIN
 DECLARE @date date
    select @date = (select authorization_date from Authorized WHERE     diver_number = @id and @lavel =level_name)
    return @date
END
GO

You need to add RETURN before the END statement

That should fix your issue, that's what fixed mine. :D


Turn this into an inline table valued function. This will perform better than the scalar function. Also, you should NOT use the default sizes for character datatypes. Do you know what the default length for a char is? Did you know that it can vary based on usage?

CREATE FUNCTION getLavel
(
    @id int
    , @lavel char --You need to define the length instead of the default length
) 
RETURNS table
return 
    select authorization_date 
    from Authorized 
    WHERE diver_number = @id 
        and @lavel = level_name

GO

Tags:

Sql Server