Advertisement

06.18.2008 at 11:50AM PDT, ID: 23496399
[x]
Attachment Details

Insert Query And Transaction

Asked by matrix_aash in MS SQL Server

Hi All,

I have an lots of insert statement running and inserting data into different table picking up data from different server which are linked to my server on which inserts run(I have all this in a stored procedure which I have inherited from previous programmer). First of all the the stored procedure empty's all the tables in which inserts is going to take place and then it starts insert statements as I explained above.

You can see a sample code in the snippet

I want to write a transaction so that if any of the inserts or deletes fail for any reason(say source missing or anything)

It rolls back and the data which is deleted intially in the delete statement is rollback.

Hope this makes sense.

Thanks in advance.

Cheers,

Aash.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
exec sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
 
    DELETE  FROM [dbo].[table1]
    DELETE  FROM [dbo].[table2]
 
 exec sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
 
-- data import script 
-- lookups 
 
    insert  into table1            (
              column1,
              column2
            )
            select  column1,
                    column2
            from    sqlservername.databasename.dbo.table_name1
 
    insert  into table2            (
              table2_column1,
              table2_column2
            )
            select  column1,
                    column2
            from    sqlservername.databasename.dbo.table_name2
 
Loading Advertisement...
 
[+][-]06.18.2008 at 11:56AM PDT, ID: 21815889

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 12:01PM PDT, ID: 21815927

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 12:54PM PDT, ID: 21816468

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 12:57PM PDT, ID: 21816513

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 01:08PM PDT, ID: 21816630

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 01:13PM PDT, ID: 21816687

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 01:33PM PDT, ID: 21816913

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 01:36PM PDT, ID: 21816939

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Sign Up Now!
Solution Provided By: chapmandew
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906