如何在SQL Server 2008中执行函数

时间:2022-04-22 16:01:48

I build a function and I am trying to execute it...but some errors are occurring

我构建了一个函数,并试图执行它……但有些错误正在发生

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

The errors when I execute it are:

执行时的错误为:

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

Msg 2714, 16级,状态3,过程Afisho_rankimin,第11行数据库中已经有一个名为Afisho_rankimin的对象。

and also it is said that:

据说

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

无法找到列“dbo”,或用户定义函数,或聚合“dbo”。Afisho_rankimin,或者名字是不明确的

3 个解决方案

#1


13  

It looks like there's something else called Afisho_rankimin in your DB so the function is not being created. Try calling your function something else. E.g.

看起来在你的DB中还有一个叫Afisho_rankimin的东西,所以函数没有被创建。尝试调用你的函数。如。

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

Note that you need to call this only once, not every time you call the function. After that try calling

注意,您只需要调用这个函数一次,而不是每次调用这个函数。在那之后试着打电话

SELECT dbo.Afisho_rankimin1(5) AS Rankimi 

#2


8  

I have come to this question and the one below several times.

我已经问过这个问题和下面这个问题好几次了。

how to call scalar function in sql server 2008

如何在sql server 2008中调用标量函数

Each time, I try entering the Function using the syntax shown here in SQL Server Management Studio, or SSMS, to see the results, and each time I get the errors.

每次,我都尝试使用SQL Server Management Studio或SSMS中显示的语法输入这个函数,以查看结果,每次都得到错误。

For me, that is because my result set is in tabular data format. Therefore, to see the results in SSMS, I have to call it like this:

对我来说,这是因为我的结果集是表格数据格式的。因此,要在ssm中看到结果,我必须这样称呼它:

SELECT * FROM dbo.Afisho_rankimin_TABLE(5);

I understand that the author's question involved a scalar function, so this answer is only to help others who come to * often when they have a problem with a query (like me).

我知道作者的问题涉及一个标量函数,所以这个答案只是帮助那些在查询(如我)遇到问题时经常遇到*的人。

I hope this helps others.

我希望这能帮助别人。

#3


1  

you may be create function before so, update your function again using.

您可能在创建函数之前,所以,再次更新您的函数使用。

Alter 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

#1


13  

It looks like there's something else called Afisho_rankimin in your DB so the function is not being created. Try calling your function something else. E.g.

看起来在你的DB中还有一个叫Afisho_rankimin的东西,所以函数没有被创建。尝试调用你的函数。如。

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

Note that you need to call this only once, not every time you call the function. After that try calling

注意,您只需要调用这个函数一次,而不是每次调用这个函数。在那之后试着打电话

SELECT dbo.Afisho_rankimin1(5) AS Rankimi 

#2


8  

I have come to this question and the one below several times.

我已经问过这个问题和下面这个问题好几次了。

how to call scalar function in sql server 2008

如何在sql server 2008中调用标量函数

Each time, I try entering the Function using the syntax shown here in SQL Server Management Studio, or SSMS, to see the results, and each time I get the errors.

每次,我都尝试使用SQL Server Management Studio或SSMS中显示的语法输入这个函数,以查看结果,每次都得到错误。

For me, that is because my result set is in tabular data format. Therefore, to see the results in SSMS, I have to call it like this:

对我来说,这是因为我的结果集是表格数据格式的。因此,要在ssm中看到结果,我必须这样称呼它:

SELECT * FROM dbo.Afisho_rankimin_TABLE(5);

I understand that the author's question involved a scalar function, so this answer is only to help others who come to * often when they have a problem with a query (like me).

我知道作者的问题涉及一个标量函数,所以这个答案只是帮助那些在查询(如我)遇到问题时经常遇到*的人。

I hope this helps others.

我希望这能帮助别人。

#3


1  

you may be create function before so, update your function again using.

您可能在创建函数之前,所以,再次更新您的函数使用。

Alter 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