Is there a way to use a function on a Microsoft SQL Server Query without using "dbo." before the function?

alexzm1 picture alexzm1 · Mar 5, 2011 · Viewed 7.8k times · Source

Is there a way to call a User defined function without using "dbo." before the function name and parameters?

Using:

SELECT USERFUNCTION(PARAM1, PARAM2, PARAM3, PARAMN)

instead of:

SELECT dbo.USERFUNCTION(PARAM1, PARAM2, PARAM3, PARAMN)

Answer

Martin Smith picture Martin Smith · Mar 5, 2011

This isn't possible for the SELECT syntax. BOL States: "Scalar-valued functions must be invoked by using at least the two-part name of the function"

This syntax works however.

CREATE FUNCTION USERFUNCTION
(@p INT)
RETURNS INT
AS
BEGIN
RETURN (2)
END

GO

DECLARE @rc INT

EXEC @rc = USERFUNCTION 1

SELECT @rc

It is best practice to always explicitly schema qualify objects you are referencing anyway though to avoid some overhead for resolving the schema (and avoid the possibility that the schema cannot be resolved implicitly or is resolved in a way that is undesired)