Solved

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

Posted on 2009-07-06
18
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
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
 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

624 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