Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

I had built a function and when I am attempting to execute it, I am getting some errors:

CREATE FUNCTION dbo.Afisho_rankimin(@emri_rest int)
RETURNS int
AS
   BEGIN
       Declare @rankimi int
       Select @rankimi=dbo.RESTORANTET.Rankimi
       From RESTORANTET
       Where  dbo.RESTORANTET.ID_Rest=@emri_rest
       RETURN @rankimi
  END
  GO
    SELECT dbo.Afisho_rankimin(5)AS Rankimi
  GO

When I am executing I'm getting the below errors:

Msg 2714, Level 16, State 3, Procedure Afisho_rankimin, Line 11
There is already an object named 'Afisho_rankimin' in the database.

and it also stated that:

Can not find column "dbo", or the user defined function, or aggregate "dbo.Afisho_rankimin", or the name is ambiguous

1 Answer

0 votes
by (12.7k points)

It seems like there is something else called Afisho_rankimin in your DB Therefore the function is not getting created. You can try calling your function something else. E.g.

CREATE FUNCTION dbo.Afisho_rankimin1(@emri_rest int)
RETURNS int
AS
   BEGIN
       Declare @rankimi int
       Select @rankimi=dbo.RESTORANTET.Rankimi
       From RESTORANTET
       Where  dbo.RESTORANTET.ID_Rest=@emri_rest
       RETURN @rankimi
  END
  GO

Also note that you have to call this only once, not each time you call the function. After that try calling

SELECT dbo.Afisho_rankimin1(5) AS Rankimi 

Join the SQL Course fast, to Learn SQL concepts in detail and get certified.

Browse Categories

...