T-SQL

Declaring Variables and Retrieving Variables

A variable can be best described as being a place holder for information which you then fill in with relevant information which you will want to retrieve later.

The below query shows the result from the CURRENT_TIMESTAMP which returns the current date and time.

PRINT CURRENT_TIMESTAMP

Jan 15 2015 11:44AM

1

The CURRENT_TIMESTAMP result can be made into a variable which can be retrieved later.

DECLARE @ThisIsTheCurrentDateandTime Datetime = CURRENT_TIMESTAMP

PRINT @ThisIsTheCurrentDateandTime

2

The DECLARE command creates the variable, assigns the datatype and the information I would like to put into the variable.

After I have created the variable I then retrieve it using the PRINT command to show the value stored.

Leave a Reply