BEGIN
Example
Section titled “Example”BEGIN;SELECT * FROM Employees;ROLLBACK;Output
Section titled “Output”Empty setExplanation
Section titled “Explanation”In this example, a transaction is initiated with BEGIN;. A query is then made to SELECT * FROM Employees; where all records from the ‘Employees’ table are requested. However, due to ROLLBACK;, all changes are reverted and the transaction nullified, hence the output is an Empty set.
Example
Section titled “Example”BEGIN;CREATE TEMPORARY TABLE temp_user( user_id INT GENERATED ALWAYS AS IDENTITY, username TEXT NOT NULL);INSERT INTO temp_user(username) VALUES('Anna');ROLLBACK;Output
Section titled “Output”ROLLBACKExplanation
Section titled “Explanation”In the example, a transaction is initiated with BEGIN, followed by the creation of a temporary table temp_user and insertion of a user ‘Anna’. However, the ROLLBACK statement in the end discards all the operations within this transaction, meaning that the table temp_user and the inserted data ‘Anna’ will not exist post transaction.
Example
Section titled “Example”BEGIN PRINT 'Hello, SQL Server!'ENDOutput
Section titled “Output”Hello, SQL Server!Explanation
Section titled “Explanation”In SQL Server, the BEGIN and END keywords define a block of code. This block is treated as a single unit. The PRINT statement within the code block outputs a string message. This example serves to illustrate the use of the BEGIN END block as well as output a simple greeting message to the console.
Example
Section titled “Example”DECLARE x NUMBER := 10;BEGIN x := x + 10; dbms_output.put_line('Value of x is: ' || TO_CHAR(x));END;Output
Section titled “Output”Value of x is: 20Explanation
Section titled “Explanation”In this example, we initialize variable x with a value of 10. We then increase the value by 10. The dbms_output.put_line function then prints out the new value of x, which is 20.
Example
Section titled “Example”BEGIN;CREATE TABLE Seasons ( id INT, name TEXT);INSERT INTO Seasons (id, name) VALUES (1, "Summer");COMMIT;Output
Section titled “Output”Query OK, 0 rows affected (0.01 sec)Explanation
Section titled “Explanation”The example demonstrates a transaction where a new table Seasons is created, and a row is inserted into it. This sequence of commands is wrapped within BEGIN; and COMMIT;, indicating that these commands need to be treated as a single transaction. If any command fails, the entire transaction should be rolled back, leaving the database unchanged.