Solved

Update from table of a database to another table of another database.

Posted on 2009-07-06
18
253 Views
Last Modified: 2012-05-07
Hi,

I have below situation.

I have 2 databases.

1. Inventory.
2. Accounting.

The user just input data using "Invoicing Menu" and press "Save" button.

I want to post the transaction into those database table.

TableSales in database Inventory.
TableJournal in databse Accounting.

I still want to use Transaction (Begin, Commit and Rollback) for both of them, in case something happened to one of them.

How could achieve this?

Thank you.

0
Comment
Question by:emi_sastra
  • 9
  • 6
  • 3
18 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24786465
you have to use the 3 part naming conventions ( databaseName.dbo.TableName )



beging tran
 insert into Inventory.dbo.Sales select ....
 insert into Accounting.dbo.TableJournal select ....
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24786496
Hi aneeshattingal,

Some questions:

1. Which connection should I use.
2. Select from what table, since we can not read Inventory Table because it is not commit yet.

Thank you.
0
 
LVL 14

Expert Comment

by:shahprabal
ID: 24786513
here is a good example.
http://msdn.microsoft.com/en-us/library/2k2hy99x(VS.71).aspx
Basically you should use the same transaction object for both sqlcommands. On error you rollback.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24786558
Hi shahprabal,

Thanks for the link. I have knowledge about this.

1. Which connection should I use.
Basically you should use the same transaction object for both sqlcommands. On error you rollback.

2. Select from what table, since we can not read Inventory Table because it is not commit yet.
How about this point? The problem what I am facing is here.

Thank you.


0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24786576
>2. Select from what table, since we can not read Inventory Table because it is not commit yet.
You can read from the table within that transaction, it wont make the changes unless you commit them
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24786605
Hi aneeshattingal,

- You can read from the table within that transaction, it wont make the changes unless you commit them
No, I can not read it, since it is locked by SQL cause waiting till Timeout.

Thank you.


0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24786664
create table test1 ( i int )
create table test2 ( j int)

begin tran
 insert into test1 select 1
 insert into test2 select * from test1
 select * from test1
 select * from test2
go
commit tran
0
 
LVL 14

Accepted Solution

by:
shahprabal earned 500 total points
ID: 24786728
Check the information about isolation level:
http://msdn.microsoft.com/en-us/library/system.data.isolationlevel(VS.71).aspx
If you set the transaction isolation level to readcommited then you should be able to read from the first table.
 
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24786750
Hi aneeshattingal,

Yes, it works, may be I used different connection. that's why I can not read it?
But it use one database, does it the same for different database?

Thank you.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:shahprabal
ID: 24786803
@ aneesh, the query that you posted will try to get executed as a whole if I am understanding it correctly. So how does the user know where the transaction failed?  
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24786813
Hi shahprabal,

I have no idea how to code it.

Would you please provide simple sample?

Thank you.
0
 
LVL 14

Expert Comment

by:shahprabal
ID: 24786864
You code it just like in the example except for one minor change :
            Dim sqlTran As SqlTransaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
instead of
            Dim sqlTran As SqlTransaction = connection.BeginTransaction()

        Using connection As SqlConnection = New SqlConnection(connectString)

            connection.Open()
 

            ' Start a local transaction.

            Dim sqlTran As SqlTransaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
 

            ' Enlist the command in the current transaction.

            Dim command As SqlCommand = connection.CreateCommand()

            command.Transaction = sqlTran
 

            Try

                command.CommandText = _

                  "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')"

                command.ExecuteNonQuery()

                command.CommandText = _

                  "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')"

                command.ExecuteNonQuery()

                sqlTran.Commit()

                Console.WriteLine("Both records were written to database.")

            Catch ex As Exception

                Console.WriteLine(ex.Message)

                Console.WriteLine("Neither record was written to database.")

                sqlTran.Rollback()

            End Try

        End Using

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24786899
Hi shahprabal,

I use 2 database and 2 tables.
I read table1 in database1 to insert table2 in database2.

Thank you.
0
 
LVL 14

Expert Comment

by:shahprabal
ID: 24787012
Are you just reading the data from table1? If so you can do a insert into like so :
INSERT INTO Insurance (Name)
    SELECT Employee.Username FROM Employee
    INNER JOIN Project ON Employee.EmployeeID = Project.EmployeeID
    WHERE Project.ProjectName = 'Hardwork';  
http://www.devguru.com/technologies/t-sql/7124.asp
I don't know what you need help with.. the sql to get the insert to work or help with the transaction. If you can't get the transaction to work then look at the isolation level. It doesn't matter if they are in different db or not (since they are on the same server). Post your query if you need more help.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24787068
Please see below code.

Try
                command.CommandText = _
                  "INSERT INTO Database1.Table1(Name) VALUES('Wrong size')"
                command.ExecuteNonQuery()
                command.CommandText = _
                  "INSERT INTO Database2.Table2(Name) SELECT  Name FROM Database1.Table1"
                command.ExecuteNonQuery()
                sqlTran.Commit()
                Console.WriteLine("Both records were written to database.")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.WriteLine("Neither record was written to database.")
                sqlTran.Rollback()
            End Try

Could it work?

Thank you.


0
 
LVL 14

Expert Comment

by:shahprabal
ID: 24787154
Ok so you are inserting, then reading the value for another insert... so all you need to do to get this to work is add:
sqlTrans.Isolationlevel = IsolationLevel.ReadCommitted
after you declare the trans or declare it as :
Dim sqlTran As SqlTransaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24787193
I just need Dim sqlTran As SqlTransaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)  to make it work?

Thank you.



 
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24787414
Hi shahprabal,

It is solved now.

Thank you very much for your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now