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

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.

LVL 1
emi_sastraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
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
emi_sastraAuthor Commented:
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
shahprabalCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

emi_sastraAuthor Commented:
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
Aneesh RetnakaranDatabase AdministratorCommented:
>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
emi_sastraAuthor Commented:
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
Aneesh RetnakaranDatabase AdministratorCommented:
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
shahprabalCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emi_sastraAuthor Commented:
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
shahprabalCommented:
@ 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
emi_sastraAuthor Commented:
Hi shahprabal,

I have no idea how to code it.

Would you please provide simple sample?

Thank you.
0
shahprabalCommented:
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
emi_sastraAuthor Commented:
Hi shahprabal,

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

Thank you.
0
shahprabalCommented:
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
emi_sastraAuthor Commented:
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
shahprabalCommented:
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
emi_sastraAuthor Commented:
I just need Dim sqlTran As SqlTransaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)  to make it work?

Thank you.



 
0
emi_sastraAuthor Commented:
Hi shahprabal,

It is solved now.

Thank you very much for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.