SQL Server How to use getdate in a function ()

Source: Internet
Author: User
Tags datetime getdate
server| function

CREATE VIEW V_getdate
As
Select GETDATE () [Output]
Go
---------------------------------------------------------------
Create function F_getdate ()
returns datetime
As
Begin
DECLARE @n datetime
Select @n = output from v_getdate
Return (@n)
End
Go
---------------------------------------------------------------

Call: Select Dbo.f_getdate ()

======================================================================
-----The following is the parsing--------
Should not be allowed inside function
The function can be either determined or indeterminate. If the result is always the same when the function is invoked with a specific set of input values, the functions are determined. If you use the same set of specific input values each time the function is invoked, and the results returned are always different, the functions are indeterminate.

An indeterminate function can have a side effect. Side effects are changing some of the global state of the database, such as updating a database table or some external resources, such as a file or network (for example, modifying a file or sending an e-mail message).

Built-in indeterminate functions in the body of a user-defined function are not allowed;

@ @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 indeterminate functions are not allowed in the body of a user-defined function, these user-defined functions can still have side effects when calling an extended stored procedure.

Because extended stored procedures can have side effects on the database, the function that invokes the extended stored procedure is indeterminate. When a user-defined function call extends stored procedures that have side effects on the database, do not expect the result set to remain consistent or perform functions.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.