开发者

SQL Server: problem with UDF

开发者 https://www.devze.com 2023-02-19 17:39 出处:网络
I have a function and select statement CREATE FUNCTION dbo.fun_currentday ( @dt DATETIME) RETURNS DATETIME

I have a function and select statement

CREATE FUNCTION dbo.fun_currentday ( @dt DATETIME)
RETURNS DATETIME
AS
BEGIN
    DECLARE @currentday DATETIME
    SET @currentday = DATEADD(dd, DATEDIFF(dd, 0, @dt), 0)
    RETURN (@currentday)
END
GO

DECLARE @pvm AS DATETIME
SET @pvm = GETDATE()

SELECT     'Last 7 days' AS Range, dbo.fun_currentday(@pvm) - 6 AS Stday, dbo.fun_currentday(@pvm) AS Endday

All works fine but when I hover over dbo.fun_currentday at the select statement, I get an error saying:

开发者_运维问答
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fun_currentday", or the name is ambiguous.

Where's the problem?


Intellisense / Error highlighting always does this for newly created objects. Use Ctrl+Shift+R to refresh the local cache.

Before

SQL Server: problem with UDF

After

SQL Server: problem with UDF


Everything runs fine here with SQL Server 2008 Express.

Have you tried running your query on a different database? You can try to create a new schema and create you UDF in it.

Make sure you have the necessary permissions and that the dbo schema configuration is correct.


Your stored procedure have to be stored somewhere so when you don't specify the location it goes to default database (master). So you should call it like

SELECT 'Last 7 days' AS Range,master.dbo.fun_currentday(GETDATE()) - 6 AS Stday, master.dbo.fun_currentday(GETDATE()) AS Endday

EDITED

I've checked that and I wasn't right, it not always goes to mater schema. It goes to database in contex of you were woring, so if your create procedure was created in query on root folder it goes to master, but if you created it in query of test databse you should use test.dbo.fun_currentday(GETDATE()). To avoid it always specify the databse like USE database_name GO CREATE FUNCTION dbo.fun_currentday

0

精彩评论

暂无评论...
验证码 换一张
取 消