Syntax Error Syntax Error - 7 days ago 6
C# Question

How to insert and update in 3 different databases?

I have 3 different databases,

database1, database2, and database3
.

I have a table
Account
in each databases. Inside my
database1
and
database2
table
Account
, I have 3 columns,
ID, Username, and Password
and inside my database 3 table Account I have 4 columns
ID, Username, Password, and db_name


What I want to do is to insert all of
database1.Account
and
database2.Account
data in
database3.Account
and in my 4th column, I need to identify where database it came from.

By the way, I have already made it but I am not satisfied with my solution because I spend 3 queries for that. As much as possible, I want to make it with just 1 query If it is possible.

P.S: Sorry for my bad English

Answer

Using union you can achieve this.

INSERT INTO database3.dbo.Account (ID, Username, Password,db_name)
SELECT ID, Username, Password,'database1' FROM database1.dbo.Account
UNION
SELECT ID, Username, Password,'database2' FROM database2.dbo.Account