views:

302

answers:

3

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

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

This is untested:

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

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

Brimstedt