SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Using Built in Functions in User Defined Functions

By Nagabhushanam Ponnapalli, 2003/08/07

Total article views: 5388 | Views in the last 30 days: 25

If you follow the various newsgroups on Microsoft SQL Server and other user groups, you often see people asking, ‘Is there any way to use GETDATE() inside a user defined function?’. The answer to this simple question is NO. But there is way to do this. In this article I will explain how to you built_in functions inside a UDF.

As we know that SQL Server does not allow you to use a Built-in functions that can return different data on each call inside user-defined functions. The built-in functions that are not allowed in user-defined functions are:

GETDATE
GETUTCDATE
NEWID
RAND
TEXTPTR
@@CONNECTIONS
@@CPU_BUSY
@@IDLE
@@IO_BUSY
@@MAX_CONNECTIONS
@@PACK_RECEIVED
@@PACK_SENT
@@PACKET_ERRORS
@@TIMETICKS
@@TOTAL_ERRORS
@@TOTAL_READ
@@TOTAL_WRITE

If you really want to use them inside a UDF, here is the way - create a view called v_Built_in_funs and call the view inside your UDF. Here is the example:

CREATE VIEW v_Built_in_funs AS select getdate() systemdate, @@spid spid
The below UDF returns new objects created on current day:
CREATE FUNCTION fnGetNewObjects()
RETURNS TABLE
AS
RETURN ( 
SELECT name,CASE xtype
WHEN 'C' THEN 'CHECK constraint'
WHEN 'D' THEN 'Default or DEFAULT constraint'
WHEN 'F' THEN 'FOREIGN KEY constraint'
WHEN 'L' THEN 'Log'
WHEN 'FN' THEN 'Scalar function'
WHEN 'IF' THEN 'Inlined table-function'
WHEN 'P' THEN 'Stored procedure'
WHEN 'PK' THEN 'PRIMARY KEY constraint (type is K)'
WHEN 'RF' THEN 'Replication filter stored procedure '
WHEN 'S' THEN 'System table'
WHEN 'TF' THEN 'Table function'
WHEN 'TR' THEN 'Trigger'
WHEN 'U' THEN 'User table'
WHEN 'UQ' THEN 'UNIQUE constraint (type is K)'
WHEN 'V' THEN 'View'
WHEN 'X' THEN 'Extended stored procedure'
ELSE NULL
END OBJECT_TYPE
FROM sysobjects, v_Built_in_Funs 
WHERE CONVERT(VARCHAR(10),crdate,101) = CONVERT(VARCHAR(10),systemdate,101))
Call the UDF to get new objects created for the day:
SELECT * FROM fnGetNewObjects()

By Nagabhushanam Ponnapalli, 2003/08/07

Total article views: 5388 | Views in the last 30 days: 25
Your response
 
 
Related Articles
FORUM

2000 to 2005: "INITCOLVS is not a recognized built-in function name"

Error: "INITCOLVS is not a recognized built-in function name"

FORUM

exec stored proc inside a function

exec stored proc inside a function

FORUM

constraints on columns while creating the table

constraints on columns while creating the table

FORUM

Creating a Check Constraint

Check Constraint

FORUM

Creating Stored Procedure with SELECT ... inside

how to create a SP with select inside

Tags
miscellaneous    
t-sql    
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com