I am new to SQL Server, and would like to know what "literal values" means. It was in the context of this sentence:

Notice that all you’ve done is declare three variables for the literal values, and assigned them values before executing the query.


Ok. A literal, not a lateral value... Gotcha.
A literal value in really any programming language is basically just a value that is defined within the code and does not change programatically.
In SQL I might use that by simply defining a varchar variable with a set value.
For example, at the start of a command,

DECLARE @myvariable varchar(30) as 'this value'

Then later in the query I might use it in a WHERE clause for example.

SELECT * FROM [table]
WHERE column1 = @myvariable.  

This is especially useful where you need to use that variable in multiple places in your script. This example uses setting a variable to a literal value. A literal can also be used in the WHERE clause directly.

WHERE column1 = 'this value'

Really, a literal value is just a value that is set in code and not directly updated through programming or by an input parameter. Also, while I used a varchar as an example, this can be any data type.

  • 3
    I like to think of literal values as "not variables", or in other words, "hard-coded values". – Randolph West Oct 29 '16 at 6:52
  • I don't believe I ever said a literal value is a variable. I just used the example of setting a variable to a literal value. A literal and a variable are two different things but can be used together. – Shooter McGavin Oct 30 '16 at 16:54
  • I'm not arguing the fact! Just adding a comment of how I think of them. – Randolph West Oct 31 '16 at 3:34

Literals is a general programming term.

Using the C# definition here

A literal is a source code representation of a value

The BOL entry discussing them in TSQL is Constants.

Some examples of literals are below.

enter image description here

SQL Server infers the datatype of literals from their contents. Some examples are

|                   literal                     |     datatype     |
| 1                                             | int              |
| $1                                            | money            |
| 1e0                                           | float            |
| 1.0000                                        | numeric(5,4)     |
| 2147483648                                    | numeric(10,0)    |
| {ts '2011-09-15 01:23:56.123'}                | datetime         |
| {guid '00000000-0000-0000-0000-000000000000'} | uniqueidentifier |
| 'Foo'                                         | varchar(3)       |
| N'Foo'                                        | nvarchar(3)      |

For values in queries that are variable (rather than constant) parameterisation is preferred to creating a new query containing the literal values. Both for security reasons (SQL injection) and to avoid compiling and caching single use plans.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.