SQL Server: problem with UDF
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
After
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
精彩评论