SQL server: how to use Getdate () in functions ()

Source: Internet
Author: User
Tags datetime getdate
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 resolution --------
The function cannot be used internally.
The function can be definite or uncertain. If the returned results are always the same when a function is called with a specific set of input values, these functions are definite. If the same set of specific input values are used for each function call and the returned results are always different, these functions are uncertain.
Uncertain functions produce side effects. The side effect is to change the global status of the database, such as updating the database table or some external resources, such as files or networks (such as modifying files or sending email messages ).
Built-in uncertain functions are not allowed in the user-defined function body. These uncertain functions 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 uncertain functions are not allowed in the user-defined function body, these user-defined functions still produce side effects when calling the extended stored procedure.
Because the extended stored procedure has a side effect on the database, it is uncertain to call the extended stored procedure function. When you define a function call to extend the stored procedure that has Database side effects, do not expect the result set to be consistent or execute the function.


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.