Have no idea how to do this! I have two databases on the same instance of SQL server 2008
In Database1 I have a table that has a column that holds a productID.
In Database2 I have a table that holds product info, i want to grab the SKU from this table based on the productID from Database1.
Its pickling my head! Hope you can help!
Many thanks
Use the fully qualified table name to access the table cross-database.
DatabaseName.Schema.TableName
join them as if they were on the same DB, just add the DB name in front of the query.
Of course, you may have to deal with permissions to access one DB from another
Select D1.productID, D2.productINfo
from Database1.SchemaName.Table D1 join Database2.SchemaName.Table D2 on D1.key=D2.key
maybe this:
TSQL: Create a view that accesses multiple databases
and
http://msdn.microsoft.com/en-us/library/ms187956.aspx
Related
I have a table in a database and I have same table to other databases.I add some columns of the table of first database.Now I want to check that all off database's table are same or not.If its not then add those columns on those tables.Please give any kind of writing sp or another idea to do that
You can use INFORMATION_SCHEMA.COLUMNS to Check all the column names of all tables present in a database.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
Of course this may not be a complete answer for your question but thought this will help you in some way.
I want to copy some tables from my DB1 to my DB2. Tables in DB1 are same as tables on DB2 but data in the tables are different. I want to copy tables from DB1 to DB2 but to keep old tables and data on DB2. How I can do this with Microsoft SQL Server Management Studio? I tried to right click and do the export but before I have to click on Finish button looks like that will just copt all data from DB1 to DB2 and I do not want that. If anyone can help with this please let me know. Thank you.
You can export the tables from DB1 with another name to DB2 if you don't want to modify them. In the export wizard just change the name of the destination table.
So you want to merge the schema AND data from DB1 into DB2?
You should list out the exact requirements, the question is still vague even with that info.
What data do you want to keep, what is ok to blow out?
What schema do you want to keep, are you archiving the old tables? Changing table names?
If you are literally trying to merge db1 into db2 your issue is going to be in managing the relationship ids which will be getting reassigned since DB2 could already be using IDs that are present in DB1.
If you want to keep old data in the destination table (or juste update it), so you might use a Merge:
MERGE INTO db2.dbo.table1 B
USING (
SELECT *
FROM db1.dbo.table1) A
ON (A.Column1 = B.Column1 )
WHEN MATCHED THEN
-- update
WHEN NOT MATCHED THEN
-- insert new rows
USE db2;
CREATE TABLE table2 LIKE db1.table1;
INSERT INTO table2
SELECT * FROM db1.table1;
This is also a way to copy a table with its records to another database.
I have a SQL Server table that has a unique field which matches a unique field in an Access DB table.
I need to get the Access DB table into the SQL Server table based on that field.
How do I go about doing this?
I want to say a JOIN, but a JOIN is just a temporary query, isn't it? I want this to be permanent. I want the Access DB table data merged into SQL Server table based on this unique field.
What's the best way to do this?
Thanks.
The reason I need this separate is that a software is reading this SQL Server data, so I need it all in the same table.
B,
I believe what you would be looking for is Union
With the two tables being
HumanResources.Employees and HumanResources.Employees2
SELECT ID, Name FROM HumanResources.Employees
UNION
SELECT ID, Name FROM HumanResources.Employees2
I have two databases, on two separate SQL Servers (trying to consolidate both).
This is my setup, and I'm trying to import from Server1:Orders table to Server2:Orders table.
Server1
Database1
Orders(ID, CustomerName, DateOrdered)
Server2
Database2
Customers(ID, Name)
Orders(ID, CustomerID, DateOrdered)
As you can see, Database1 has de-normalized data, and Database2 has the same data, properly normalized.
The issue I'm having is doing the SQL Server import. In Database2, the Customers table is populated, and there WILL be a match between Server1.Database1.Orders.CustomerName and Server2.Database2.Customers.Name.
What I'd LIKE to have happen, is during the import, have the Customer.ID field "looked-up" based on the value of the CustomerName field in the import data, then do the corresponding insert to my new Orders table.
I am able to connect to both servers through SSMS, and I'm trying to do the import via the "SQL Server Native Client 10" as the datasource.
Update
It appears I am not going to be able to do an SSIS "package" so what I've done is this:
Moved Database1.Orders to Database2.OrdersOLD.
I'm now looking for a query to create new Order records in Database2.Orders and insert the correctly looked up CustomerID, since now all three tables are within the same database, is this possible?
Use SSIS, specifically the Lookup Transformation. See the linked blogs and tutorials from the MSDN article link.
I think it could be done like this:
SELECT O1.ID, C2.ID, O1.DateOrdered
INTO Server2.Database2.Orders
FROM Server1.Database1.Orders O1
INNER JOIN Server2.Database2.Customers C2 ON C2.Name = O1.CustomerName
Lets say we have two databases, one named db1, the other one named db2.
Both databases have a table called Employees. Is there anyway to join results from both tables even though they have the same name?
Note that the two tables do NOT have conflicting field names. I know this is stupid but we had to make an extension database to the existing one and to keep it simple we'd like to have the same table name in both databases.
Help is greatly appreciated, thanks
Also I know there is a similar question answered here but I didn't find it helpful in this context.
You should be able to refer to the tables by their fully-qualified names. If they're in different schemas, you can say:
SELECT * FROM Schema1.MyTable as T1 JOIN Schema2.MyTable as T2 ON T1.Something = T2.SomethingElse
If they're actually different databases, you'd need a database link in which case it becomes MyTable1#Database1 etc.
Yes you can definitely do this, you'll just need to alias them like so:
SELECT ... FROM [db1].[dbo].[Employees] as e1 INNER JOIN [db2].[dbo].[Employees] as e2 ON ...
Then you can get what you need by referencing e1.MyColumn or e2.MyOtherColumn.
You should be able to reference them by database.owner.table, e.g. db1.dbo.Employees and db2.dbo.Employees (assuming the tables are owned by dbo)
select * from db1,db2 where db1.employee=db2.employee Is this what you mean?