PreviousSQL Engine Reference (v9 SP2 (9.5) revision 1) Next

System Functions

Chapter contents

System functions provide information at a system level.

Table 5-4 System Functions
Function
Description
DATABASE ( )
Returns the current database name.
NEWID ( )
Creates a unique value for data type uniqueidentifier
USER ( )
Returns the login name of the current user.

Examples

The following examples show how to obtain the name of the current user and database:

SELECT USER()  
SELECT DATABASE() 


If you want to obtain this information for every record in a table, use the following (the example uses the Person table in DEMODATA):

SELECT USER() FROM person 
SELECT DATABASE() FROM person 
SELECT USER(), DATABASE() FROM person 


The following example creates a column of data type UNIQUEIDENTIFIER as the first column in new table "table1." Setting a default value with the NEWID function provides a unique value for "col1" in each new row within the table.

CREATE TABLE table1 (col1 UNIQUEIDENTIFIER DEFAULT 
NEWID() NOT NULL, col2 INTEGER) 
    INSERT INTO table1 (col2) VALUES (1) 
    INSERT INTO table1 (col2) VALUES (2) 
    INSERT INTO table1 (col2) VALUES (3) 

Chapter contents
Book contents

Prev topic: Time and Date Functions
Next topic: Logical Functions