Cogniza | Business-Intelligence Specialists

May/06

25

Sql Server: GetNow View

The Sql Server GETDATE() function not allowed in the body of user-defined functions, as it is non-deterministic. The GetNow view provides a means to generate a system date/time that can be used in Sql Server user-defined functions.

DROP VIEW dbo.GetNow
GO
CREATE VIEW dbo.GetNow AS
SELECT GetDate() Now
GO
GRANT SELECT ON dbo.GetNow TO PUBLIC
GO

· ·

1 comment

  • Al Wood · June 29, 2006 at 3:45 am

    So you implement it ike this:
    – Returns getDate() by cheating
    – Very naughty. Makes a non-deterministic function. Be aware of non-deterministic problems when you use this
    CREATE FUNCTION dbo.getNowDatetime()
    RETURNS datetime
    BEGIN

    /*
    –If you need to re-create the view…
    DROP VIEW dbo.GetNow
    GO
    CREATE VIEW dbo.GetNow AS
    SELECT GetDate() Now
    GO
    GRANT SELECT ON dbo.GetNow TO PUBLIC
    GO
    */

    RETURN (select top 1 Now from GetNow)
    END

Leave a Reply

 

<<

>>

Theme Design by devolux.nh2.me