如何在 SQL Server 2008 中执行函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21358306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute function in SQL Server 2008
提问by user3233650
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.
消息 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”,或名称不明确
回答by Yuriy Galanter
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.
看起来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
回答by jp2code
I have come to this question and the one below several times.
我已经多次谈到这个问题和下面的问题。
how to call scalar function in 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:
对我来说,那是因为我的结果集是表格数据格式。因此,要在 SSMS 中查看结果,我必须这样称呼它:
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 StackOverflow often when they have a problem with a query (like me).
我知道作者的问题涉及一个标量函数,所以这个答案只是为了帮助那些在查询有问题时经常来到 StackOverflow 的其他人(像我一样)。
I hope this helps others.
我希望这对其他人有帮助。
回答by Almostafa
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