Results 1 to 2 of 2

Thread: gedate() in a function

  1. #1
    Join Date
    Nov 2002
    Posts
    261

    gedate() in a function

    I have the function below, can you not use getdate() in a function. It tells me Invalid use of 'getdate' within a function.

    alter function [dbo].[fnGetOrderAlertsandMessages](@orderid int )
    returns varchar(350)
    as
    begin
    declare @ret varchar(350)
    select @ret = coalesce(@ret+', ', '') +
    case t.FieldValue
    when '1' then oa.comments
    else t.longdescr
    end
    from OrderAlerts oa inner join
    Translate t on t.TranslateID = oa.TranslateID and t.MaxEffDtFlag = 1 and t.IsActive = 1
    where oa.OrderID = @orderid
    and t.FieldName in ('Alerts','Messages')
    and oa.created < getdate() + 1
    group by t.LongDescr, oa.comments, t.fieldvalue
    return @ret
    end

  2. #2
    Join Date
    Dec 2004
    Posts
    502
    This is from BOL, under "Create Function":

    Function Determinism and Side Effects
    Functions are either deterministic or nondeterministic. They are deterministic when they always return the same result any time they are called with a specific set of input values. They are nondeterministic when they could return different result values each time they are called with the same specific set of input values.

    Nondeterministic functions can cause side effects. Side effects are changes to some global state of the database, such as an update to a database table, or to some external resource, such as a file or the network (for example, modify a file or send an e-mail message).

    Built-in nondeterministic functions are not allowed in the body of user-defined functions; they are as follows:

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


    Although nondeterministic functions are not allowed in the body of user-defined functions, these user-defined functions still can cause side effects if they call extended stored procedures.

    Functions that call extended stored procedures are considered nondeterministic because extended stored procedures can cause side effects on the database. When user defined functions call extended stored procedures that can have side effects on the database, do not rely on a consistent result set or execution of the function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •