SQL Server function invocation syntax question
-
16-10-2019 - |
Question
The following excerpt is extract from this page.
CURRENT_USER (Transact-SQL)
Returns the name of the current user. This function is equivalent to USER_NAME().
When I execute the two function with the following TSQL scripts (the questionable ones were commented), I'm confused about the invocation syntax. I want to know when to use the parenthesis and when not. Thanks!
SELECT CURRENT_USER
-- SELECT CURRENT_USER() -- Error!
SELECT USER_NAME()
-- SELECT USER_NAME -- Error!
Solution
The ones without parentheses such as CURRENT_USER
and CURRENT_TIMESTAMP
are there for ANSI 92
compliance. There are only five such Niladic Functions and the list hasn't changed since SQL Server 6.0.
Most other SQL Server system functions require parentheses.
The exception to this are those functions that used to be known as global variables that are prefixed with @@
such as @@SPID
.
This last group of functions can be passed directly in a parameter list to a stored procedure. Neither of the other two variants can.