Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Add two numbers from different databases

Author  Topic 

JasonShave
Starting Member

2 Posts

Posted - 2005-11-21 : 21:43:11
Greetings,

I'm trying to calculate two "Quantity" values from different databases and create a third table with these values. Does anyone know how to write this in T-SQL?

All databases are identical in structure.

Database1=edmdb, Table=Item, Value=Quantity
Database2=whsdb, Table=Item, Value=Quantity
Database3=webdb, Table=tblItem, Value=WebQuantity

The WebQuantity should be the sum of both quantity values from db1 and db2.

Thanks in advance! I'm lost!!

nr
SQLTeam MVY

12543 Posts

Posted - 2005-11-21 : 21:58:53
insert tblItem (item_id, WebQuantity)
select edmdb.item_id, edmdb_Item.Quantity + whsdb_Item.Quantity
from edmdb.dbo.Item edmdb_Item
join whsdb.dbo.Item whsdb_Item
on edmdb_Item.item_id = whsdb_Item.item_id

I'm guessing about the item_id but you'll get the idea.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

JasonShave
Starting Member

2 Posts

Posted - 2005-11-21 : 22:01:15
great! thanks. so you don't need to specify the "use" statement when working with different databases?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-22 : 00:54:32
quote:
Originally posted by JasonShave

great! thanks. so you don't need to specify the "use" statement when working with different databases?


Yes
Also Usage of USE is not allowed inside a stored Procedure

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -