Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-06
18
Medium Priority
?
261 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

824 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