I use SQL Server 2008 with Borland Delphi in order to develop my applications. Since recently I'm getting a very weird error. I have created several scalar functions that I use in my application, but I'm having a problem with a customer, in his company my software returns the following error when I call my scalar functions:
Cannot find either column “dbo” or the user-defined function or aggregate “dbo.FunctionName”, or the name is ambiguous."
I've already searched a lot, even here, so keep in mind that:
The function exists;
I'm quering the correct database;
There's no typos;
Owner schema is dbo;
This problem occurs with ALL MY FUNCTIONS;
And the weirdest...
It only happens when I call them from my application, if i run the EXACTLY SAME code at the Query Analyzer using the same user, it will run just fine.
I have this same functions in several other customers, and they don't have any problem. Could it be a SQL Server problem?
Ps: Sorry for my poor English, first question here.
I don't know how QueryAnalyzer calls your functions, but I know this error.
Usually, when you have user-defined functions, you need to prefix the function with the schema name.
So if your function is in schema "dbo", and the name is "fnPadLeft", you need to call the function in code like this:
SELECT
id
,some_field
,dbo.fnPadLeft(some_other_field)
FROM YOUR_TABLE_NAME
If you call it like this:
SELECT
id
,some_field
,fnPadLeft(some_other_field) -- lacks dbo.
FROM YOUR_TABLE_NAME
Then you'll get "no such function".
This only happens to scalar functions btw. (you specifically mentioned this), table-valued functions (and all other non-function things) are not affected by this "feature".
It might also be that you have the same functionname in two schemas (also take a look at the functions in the master database). Maybe your "other functions" are table valued functions.
Related
I am building an application with which users can create their own user-defined functions in a SQL Server database. I have to validate in some way that the function name picked by the user does not equal to the name of a built-in function (such as avg, concat, ...) since this would break the application.
Is there a way to query the names of all built-in functions (not the UDFs), such as a system view or any the like?
I think it is YOUR rule of naming functions, as long as YOU are the developer. Which means your functions will have always your prefix in database, as mf_ which could mean myfunction_ and your user's function name. In your code always put that mf_ prefix in front of your user's function call, check if it exists and use it.
I wrote a CLR stored procedure (to send emails, and not depend from DatabaseMail, as I was told it was more safer).
It works as expected. The thing I want to learn now is how to get the definition from a CLR object (CLR SP, CLR scalar function, etc).
I already tried with:
sys.sql_modules,
sys.system_sql_modules,
OBJECT_DEFINITION()
But it returns a null definition.
Is there any other way to get the definition of a CLR object?
SQLCLR objects have no T-SQL code so there is nothing to store that could be retrieved using the sys.* _sql_modules DMVs or the OBJECT_DEFINITION() built-in function.
If you want the underlying .NET code, that is in the Assembly, which is found in: sys.assembly_files.
SELECT *
FROM sys.assembly_files
WHERE [file_id] = 1;
If you don't have the source code, that can be extracted by disassembling the Assembly.
If you want the CREATE PROCEDURE ... AS EXTERNAL NAME ... ; statement then that would need to be pieced together from several DMVs: sys.assembly_modules, sys.types, sys.parameters, sys.columns. You would also need to use OBJECT_NAME and OBJECT_SCHEMA_NAME built-in functions. If you are trying to recreate a CREATE TYPE statement, then you will need to check the sys.assembly_types DMV.
Also, to learn more about working with SQLCLR in general, please see the series of articles I am writing on this topic on SQL Server Central: Stairway to SQLCLR (free registration is required to read their content).
I have created two functions one is table-valued function and second one is scalar-valued function. while calling the table-valued function I don't need to use the Schema name but for the Scalar-valued function I need schema. my question is why? as both are the user defined functions.
Table-valued Function
SELECT * FROM split ('1,2,3',',')
Scalar-valued Function
SELECT dbo.GetESTDate()
Yours is an interesting question. I'm actually not sure what the official reason is but I can point you to Microsoft's CREATE FUNCTION documentation which does in fact indicate that you must use at least two-part naming when calling scalar functions.
Scalar-valued functions must be invoked by using at least the two-part name of the function. (Reference)
I'll dig around a bit more and see if I can't get more information. You've got me interested too.
This is Sql Server rule.
Even if you don't need to use schema for TVF, PLEASE use it always! Imagine that someone implements built-in "split" function in SQL Server and your code will break because it will be in collision with built-in TVF. Even if you don't use schema add some prefix like ufn.
Why calling a user defined function need the owner name when calling a stored procedure doesn't ?
Please help!
I assume you mean the schema name? Owner is the user that created it.
It distinguishes the function from a built-in function. "System" stored procedures and functions live in the master database (so it can be searched), while built-in functions (things like DATEADD) reside in the database engine themselves.
I guess it's more difficult when you specify SELECT MyFunction() for the database engine to work out whether you mean a function that lives in a database, or a built-in function.
This is actually only the case for scalar-valued functions.
After discussing with Andy above, I answer my own question here:
Stored procedures are stored in the current database of the connection when created. In the mean time, functions are stored two locations: in the database engine (the scalar-value built-in ones) and in the current database of the connection (the user-defined ones).
So the when calling the user-defined scalar-value functions, we need the schema name prefix to distinguish them vs the built-in ones. We don't need this prefix for the other types of functions and for the stored procedures.
I'm using MS SQL 2005 and when I create a function I need to put the schema name to call it:
select dbo.MyFunc
All my tables are also in "dbo" schema and I don't need the schema name to reference it, so I'd like to know if I'm missing some configuration that could do the same to functions.
Short answer, no it isn't.
You should consider to prefix all your database objects with the schema owner to avoid having sql server to "look it up".
It makes your statements more readable and gives a slight increase in performance (although you'd probably won't notice it).
Regards,
Lieven
Unlike all the other database objects (tables, views and stored procedures), user defined functions always need the schema name when they are referenced. It's a quirk of SQL Server.
*Scalar-valued functions must be invoked by using at least the two-part name of the function
http://msdn.microsoft.com/en-us/library/ms186755.aspx
+1 Parent
Borik