Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

handling a transaction

Status
Not open for further replies.

fikir

Programmer
Jun 25, 2007
86
I am a bit confused here with handling a transaction

I am trying to insert data into a series of production tables.

insert into tbl1 (col1, col2)
select colt, col2 from stagingTable1

--error handling

insert into tbl2 (col1, col2
select col1, col2 from stagingTable2

--error handling

.
.
.

I want to put this in a single transaction and if the process failes any where it should rollback every thing, It should rollback to the point before the load data process starts

how can I do this.

second thing is every tables have identity columns and I want to reset these identity columns of each tables to 1
after rolling back

How do I do this also

Thanks
 
a rollback without a save tran will roll back everything
use SET XACT_ABORT ON to make sure if you get a non trapable error

to reset identties use TRUNCATE TABLE (if you don't have FK constraints) or DBCC CHECKIDENT (TableName, RESEED, 1)


Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
SQLBlog.com, Google Interview Questions
 
Thanks Denis, that was a great help

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top