diff --git a/Lab10/Task1.sql b/Lab10/Task1.sql new file mode 100644 index 0000000..fd6fb89 --- /dev/null +++ b/Lab10/Task1.sql @@ -0,0 +1,19 @@ +/* +Starts a transaction to read the record of +Linda Gonzales and update her first name. +Second select shows the uncommitted update. +@@trancount shows the number of open transactions. +*/ + +USE AdventureWorks + +-- START TRANSACTION HERE +BEGIN TRANSACTION + SELECT @@trancount AS 'Transaction Count' + SELECT FirstName, MiddleName, LastName FROM Person.Contact WHERE ContactID = 342 + UPDATE Person.Contact SET FirstName = 'Lin' WHERE ContactID = 342 +-- END TRANSACTION HERE +COMMIT TRANSACTION + +SELECT FirstName, MiddleName, LastName FROM Person.Contact WHERE ContactID = 342 +SELECT @@trancount AS 'Transaction Count' \ No newline at end of file diff --git a/Lab10/Task2.sql b/Lab10/Task2.sql new file mode 100644 index 0000000..806e1b7 --- /dev/null +++ b/Lab10/Task2.sql @@ -0,0 +1,21 @@ +/* +Starts a transaction to read the record of +Dominic Gonzalez and update his first name. +Second SELECT shows the uncommitted update. +@@trancount showS the number of open transactions. +Then the transaction is rolled back and the record read again. +*/ + +USE AdventureWorks + +BEGIN TRANSACTION + SELECT @@trancount AS 'Transaction Count' + SELECT FirstName, MiddleName, LastName FROM Person.Contact WHERE ContactID = 7454 + UPDATE Person.Contact SET FirstName = 'Dom' WHERE ContactID = 7454 + SELECT FirstName, MiddleName, LastName FROM Person.Contact WHERE ContactID = 7454 + SELECT @@trancount AS 'Transaction Count' +-- END TRANSACTION HERE +ROLLBACK TRANSACTION + + SELECT FirstName, MiddleName, LastName FROM Person.Contact WHERE ContactID = 7454 + SELECT @@trancount AS 'Transaction Count' diff --git a/Lab10/Task3.sql b/Lab10/Task3.sql new file mode 100644 index 0000000..9bb489f --- /dev/null +++ b/Lab10/Task3.sql @@ -0,0 +1,20 @@ +SELECT resource_type, request_mode, request_type, request_status, +request_session_id +FROM sys.dm_tran_locks + +/* +Update a record in the Person.Contact table in the AdventureWorks database. +*/ + +USE AdventureWorks + +BEGIN TRANSACTION + UPDATE Person.Contact + SET FirstName = 'Fran' + WHERE ContactID = 6 +-- For the purpose of the exercise, COMMIT TRANASACTION or ROLLBACK TRANSACTION are not used. + +SELECT @@spid AS 'spid' +-- Use the SPID to identify the connection when using sys.dm_tran_locks. + +ROLLBACK TRANSACTION \ No newline at end of file diff --git a/Lab10/Task4.sql b/Lab10/Task4.sql new file mode 100644 index 0000000..6ffe61a --- /dev/null +++ b/Lab10/Task4.sql @@ -0,0 +1,19 @@ +/* +Read and update a record in the Person.Contact table in the AdventureWorks database. +*/ + +USE AdventureWorks + +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE + +SET lock_timeout 5000 + +BEGIN TRANSACTION + SELECT * FROM Person.Contact WHERE ContactID = 10 + UPDATE Person.Contact SET FirstName = 'Frances' WHERE ContactID = 6 +-- For the purpose of the exercise, COMMIT TRANSACTION or ROLLBACK TRANSACTION are not used. + +SELECT @@spid AS 'SPID' +-- Use the SPID to identify the connection when using sp_lock. + +-- ROLLBACK TRANSACTION \ No newline at end of file diff --git a/Lab10/Task4_1.sql b/Lab10/Task4_1.sql new file mode 100644 index 0000000..5f429c9 --- /dev/null +++ b/Lab10/Task4_1.sql @@ -0,0 +1,2 @@ +SELECT resource_type, request_mode,request_type, request_status, request_session_id +FROM sys.dm_tran_locks \ No newline at end of file