0

I need to write a sql query that adds one column from one database (DB1) to another column and the sum is save in that column in the second database(DB2). where userIds are the same

DB1
TableA
UserId People


DB2
TableB
Amount UserId

it would be something like this

DB2.TableB.Amount = DB2.TableB.Amount + DB1.TableA.People

1
  • Please specify platform (SQL Server, Oracle, whatever) Commented Apr 2, 2009 at 10:02

3 Answers 3

4

Do you mean:

UPDATE b
SET    Amount = b.Amount + a.People
FROM DB2.dbo.TableB b
INNER JOIN DB1.dbo.TableA a
  ON  a.UserId = b.UserId

dbo = owner of table, it can also be unspecified: DB1..TableA

Sign up to request clarification or add additional context in comments.

Comments

0
INSERT INTO DB2.dbo.TableB
SELECT COUNT(*), UserID
FROM DB1.dbo.TableA
GROUP BY UserID

Comments

0

This is untested:

INSERT INTO DB2.dbo.TableB
SELECT SUM(DB2.TableB.Amount + DB1.TableA.People), UserID
FROM DB1.dbo.TableA
GROUP BY UserID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.