Correct use of transactions in SQL Server

0 votes

I have 2 commands and need both of them executed correctly or none of them executed. So I think I need a transaction, but I don't know how to use it correctly.

What's the problem with the following script?

BEGIN TRANSACTION [Tran1]

INSERT INTO [Test].[dbo].[T1]
    ([Title], [AVG])
VALUES ('Tidd130', 130), ('Tidd230', 230)

UPDATE [Test].[dbo].[T1]
  SET [Title] = N'az2' ,[AVG] = 1
  WHERE [dbo].[T1].[Title] = N'az'

COMMIT TRANSACTION [Tran1]
GO

The INSERT command is executed, but the UPDATE command has a problem.

How can I implement this to rollback both commands if any of them have an error in execution?


 

Nov 4 in Others by gaurav
• 22,040 points
27 views

1 answer to this question.

0 votes

A transaction in SQL Server is a sequential group of statements or queries to perform single or multiple tasks in a database. Each transaction may have single read, write, update, or delete operations or a combination of all these operations.

answered Nov 7 by gaurav
• 22,040 points

Related Questions In Others

0 votes
1 answer

How Can I use "Date" Datatype in sql server?

Your dates are interpreted as MM-DD-YYYY. This ...READ MORE

answered Jun 21 in Others by nisha
• 2,190 points
80 views
0 votes
1 answer

What is use of Babel in Node.js ?

Node.js is a runtime environment  which let us ...READ MORE

answered Jul 2, 2019 in Others by ArchanaNagur
• 2,360 points
1,556 views
0 votes
1 answer

How to delete duplicate rows in SQL Server?

To answer your query, note that CTEs ...READ MORE

answered Feb 10 in Others by Soham
• 9,670 points
265 views
0 votes
1 answer
0 votes
0 answers

Altering column size in SQL Server

How to change the column size of ...READ MORE

Aug 23 in Database by Kithuzzz
• 20,660 points
89 views
0 votes
0 answers

Self Join to get employee manager name

Hello, the columns in my employee table ...READ MORE

Aug 25 in Database by Kithuzzz
• 20,660 points
30 views
0 votes
0 answers

Why do you create a View in a database?

When and why does a database owner ...READ MORE

Aug 26 in Database by Kithuzzz
• 20,660 points
56 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3 in Database by Vaani
• 7,020 points
168 views
0 votes
1 answer

Change date format of cell in excel from dd.mm.yyyy to yyy/mm/dd ( excel version 2013 )

Hello :)   Excel’s Format Cells function can quickly ...READ MORE

answered Feb 9 in Others by gaurav
• 22,040 points
529 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP