The SET keyword in SQL is used to assign values to variables or change the configuration of the database session. It has several uses, depending on the context in which it is applied. Here are the main uses of the SET keyword in SQL:
1. Assigning Values to Variables
You can use SET to assign values to user-defined variables in SQL.
Example:
DECLARE @TotalSales INT;
SET @TotalSales = 1000;
SELECT @TotalSales;
2. Modifying Session Settings
The SET keyword is often used to configure session-level settings like language, date format, and transaction isolation level.
Example:
SET DATEFORMAT dmy;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
3. Updating Data in a Table
The SET keyword is used in UPDATE statements to modify column values in a table.
Example:
UPDATE Employees
SET Salary = 50000
WHERE EmployeeID = 123;
4. Setting Database Options
Some databases allow you to configure session options or properties using the SET keyword.
Example in MySQL (enabling autocommit):
SET autocommit = 1;
5. Changing System Variables (MySQL)
You can modify system variables (like connection settings) in MySQL using the SET keyword.
Example:
SET GLOBAL max_connections = 200;
6. Assigning Multiple Variables
In SQL Server, you can assign multiple variables in a single SET statement.
Example:
DECLARE @FirstName VARCHAR(50), @LastName VARCHAR(50);
SET @FirstName = 'John', @LastName = 'Doe';
The specific behavior of SET can vary slightly between different database systems (e.g., MySQL, SQL Server, PostgreSQL), but these are the common uses of the SET keyword.
No comments:
Post a Comment