CRUD Statements  «Prev  Next»
Lesson 5Using Literal Values
ObjectiveUse literal values in INSERT, UPDATE, and DELETE statements in SQL Server.

Using Literal Values in SQL Server

In SQL Server, a literal value is a fixed value directly specified in a Transact-SQL (T-SQL) query, such as a string, number, or date, as opposed to a variable or column reference. Literal values are commonly used in INSERT, UPDATE, and DELETE statements to add, modify, or filter data. String literals (e.g., 'Anthony') and date literals (e.g., '2025-08-03') must be enclosed in single quotes, while numeric literals (e.g., 101) do not require quotes. Literal values must match the target column’s data type to avoid errors.

INSERT Statement

To add a new employee, Anthony Mann, hired on August 3, 2025, to the Employees table, use the INSERT statement with literal values:

INSERT INTO Employees (FirstName, LastName, HireDate)
VALUES ('Anthony', 'Mann', '2025-08-03');
Here, 'Anthony' and 'Mann' are string literals for the FirstName and LastName columns (typically VARCHAR), and '2025-08-03' is a date literal for the HireDate column (e.g., DATE). Ensure the literal values match the column data types to prevent errors.

UPDATE Statement

To correct a misspelled record for an employee with EmployeeID = 101 (e.g., from “Tony Man” to “Anthony Mann”), use the UPDATE statement with literal values:

UPDATE Employees
SET FirstName = 'Anthony',
    LastName = 'Mann'
WHERE EmployeeID = 101;
The WHERE clause uses the numeric literal 101 to target the specific row. If EmployeeID is a primary key or has a unique constraint, only one row is updated, ensuring precision and avoiding unintended changes to other records.

DELETE Statement

To remove the record for an employee with EmployeeID = 101 who no longer works for the company, use the DELETE statement:

DELETE FROM Employees
WHERE EmployeeID = 101;
The literal value 101 in the WHERE clause ensures only the specified row is deleted. Caution: Omitting the WHERE clause deletes all rows in the table, so always verify the condition before executing a DELETE statement.

Best Practices for Literal Values

  • Data Type Compatibility: Match literal values to the column’s data type (e.g., use '2025-08-03' for DATE, not '08/03/2025', which may cause errors depending on the server’s date format settings).
  • Use Single Quotes for Strings and Dates: String and date literals must be enclosed in single quotes (e.g., 'Anthony', '2025-08-03'). Double quotes are used for identifiers (e.g., column names with spaces).
  • Precision in WHERE Clauses: Use unique identifiers like primary keys in WHERE clauses to avoid affecting unintended rows.
  • Testing Changes: Before executing UPDATE or DELETE, use a SELECT statement to preview affected rows:
    
    SELECT * FROM Employees WHERE EmployeeID = 101;
            
In the next lesson, you will explore advanced techniques for using literal values, such as combining them with expressions and variables.

SEMrush Software 5 SEMrush Banner 5