Muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti mustaqil-1-2-3-4-5


Download 131.16 Kb.
bet7/7
Sana31.01.2024
Hajmi131.16 Kb.
#1818316
1   2   3   4   5   6   7
Bog'liq
Afzalbek (1)

Rollback amalini bajarilishi
-- Begin Transaction
BEGIN;

-- Savepoint


SAVEPOINT before_complex_query;

BEGIN TRY


-- Complex query, you can replace this with your actual query
UPDATE Products
SET price = price * 1.1
WHERE category = 'Electronics';

-- Simulate an error, for example, a constraint violation


INSERT INTO Products (name, category, price) VALUES ('New Product', 'Clothing', 25.99);
EXCEPTION
-- If an error occurs, rollback to the savepoint
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT before_complex_query;
-- Optionally handle or log the error
RAISE NOTICE 'An error occurred: %', SQLERRM;
END TRY;

-- If no error occurred, commit the transaction


COMMIT;

-- Mijozlarni o'zgartirish


UPDATE customers
SET email = 'new_email@example.com'
WHERE last_name = 'Smith' AND phone_number LIKE '+998%';

-- Buyurtmalarni o'zgartirish


UPDATE orders
SET total_amount = 2000
WHERE customer_id IN (SELECT customer_id FROM customers WHERE last_name = 'Smith');


SAVEPOINT tranzaksiyasini ishlatilishi
-- Begin Transaction
BEGIN;

-- Savepoint


SAVEPOINT before_complex_transaction;

BEGIN TRY


-- Complex transaction, you can replace this with your actual logic
-- Update prices of products in the 'Electronics' category
UPDATE Products
SET price = price * 1.1
WHERE category = 'Electronics';

-- Savepoint


SAVEPOINT after_price_update;

-- Insert a new product


INSERT INTO Products (name, category, price) VALUES ('New Product', 'Clothing', 25.99);

-- Commit the transaction


COMMIT;
EXCEPTION
-- If an error occurs, rollback to the savepoint
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT after_price_update;
-- Optionally handle or log the error
RAISE NOTICE 'An error occurred: %', SQLERRM;
-- Rollback the entire transaction (excluding the price update)
ROLLBACK TO SAVEPOINT before_complex_transaction;
END TRY;
Download 131.16 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling