22

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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
alexzm1
  • 563
  • 2
  • 7
  • 14
  • 1
    No. Why bother?? Just use the schema prefix and get on with your programming life... – marc_s Mar 05 '11 at 22:19
  • 2
    @marc_s A good reason to avoid "dbo." prefix is writing SQL code that is valid for different DBMS'. – bluish Sep 23 '16 at 06:50
  • @bluish: OK - but stored functions typically cannot be called without the schema prefix, in T-SQL/SQL Server – marc_s Sep 23 '16 at 21:10

3 Answers3

13

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)

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • well it seems that this is the answer to why I can't use a function like i could do it in MySQL, thanks. – alexzm1 Mar 06 '11 at 06:29
  • 1
    yes, for plan cache reasons. UserA and UserB won't use the same plan without schema qualification because there could be userA.table and OtherSchema.table – gbn Mar 06 '11 at 08:47
2

There are various ways to do this, if we take it that you have a negative reaction to seeing "dbo.".

In SQL Server 2000, there is a way to turn UDFs into system functions by toggling a bit. This "feature" has been removed from SQL Server 2005 onwards, so I won't go into detail unless you really are still using 2000.

You can use OPENQUERY with PROC syntax similar to what Martin has shown.

You can turn the Scalar function into a Table Valued Function, either by rewriting it, or by wrapping it in a TVF. The syntax changes however, so

select dbo.udf(a,b) from c

--becomes
select d
from c
cross apply tvf(a,b) e(d)  -- look, no "dbo"!

But none of the above looks simpler than just tacking a simple "dbo." prefix to the function name, so why would you do it?

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
  • I'm using Microsoft SQL Server 2005, and i'm trying to pass user defined functions directly like I could do it with MySQL 5.0+ but I'm starting to see the differences between thos DBMS. – alexzm1 Mar 06 '11 at 06:03
  • 1
    @alex then there're plenty more differences to encounter. happy transition – RichardTheKiwi Mar 06 '11 at 06:28
-1

Yes Possible, Actually when function returning scalar value you must call with schema name like dbo.yourfunction , If you don't want to call function without schema name you should create function as follows.

Sample Code:

CREATE FUNCTION [dbo].[FN_MAPCOUNT] 
(

    @countValue int
)
RETURNS @TEMPTABLE Table(cntValue int)
as
begin
  DECLARE @countValueint
  @countValue= select count(*) from mappings;

   INSERT @TEMPTABLE (cntValue) VALUES (@countValue) 
    RETURN
end
Go

select * from FN_MAPCOUNT(1); 

The reason is you are returning the value as table .

Karthik
  • 747
  • 4
  • 21
  • 48
Jagadeesh G
  • 272
  • 1
  • 3
  • 10