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
Related
noob SQL question here. I have a database in SQL Server 2012 and access to another MySQL database with information I'd like to pull.
I would like to update a column representing quantities of a workstation, and that information is present on the other database. I would like to structure a query that updates quantities of all items where model numbers match in each database.
I'll include two select statements to demonstrate what I'd like to work with.
The data from the other database I want to pull from.
SELECT *
FROM OPENQUERY(S3MONITOR,
'SELECT count(BuildDriver) AS ''# of Workstations'', BuildDriver AS ''Workstation Model''
FROM workstation
GROUP BY BuildDriver')
Which produces this result
My database that I'd like to update.
SELECT NumOfDevices, Model
FROM dbo.Currency
INNER JOIN dbo.SubCategory
ON Currency.SubCategoryId = SubCategory.SubCategoryId
WHERE SubCategory.SubCategoryName = 'WorkStation';
Which produces this result
The models will be updated in my database to make sure model names correspond to one another but in the interim I'd like to test a query using the M93z. So what would be the most ideal way of updating NumofDevices in my database using # of Workstations in the other database, where the model names are equal?
Sorry if this is an obvious question, I'm still an SQL noob and it's still not intuitive to me when it comes to these kinds of things.
EDIT: The end goal is to routinely update all Workstation quantities nightly through an SQL Server Agent scheduled job but I'll cross that bridge when I come to it (I actually have some faith that I'll be able to apply the query solution to this job as soon as it's figured out) but I want to include this information in the event that it changes any of your suggestions.
You can use join in the update statement.
with newvals as (
SELECT *
FROM OPENQUERY(S3MONITOR,
'SELECT count(BuildDriver) AS ''# of Workstations'', BuildDriver AS ''Workstation Model''
FROM workstation
GROUP BY BuildDriver'
) oq
)
update c
set NumOfDevices = newvals.num_workstations
from dbo.Currency c join
dbo.SubCategory sc
on c.SubCategoryId = sc.SubCategoryId join
newvals
on newvals.model = c.model
where sc.SubCategoryName = 'WorkStation';
Note: This updates values already in the table. You might want to use merge if you want to add new rows as well. If this is the case, ask another question, because this question is specifically about updating.
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 wanna search data that is different from each other.
I don't know how to link table in two database to search different data.
For example....
tblCustomer in Database1 has all data
tblCustomer in Database2 has some data that contain in Database1
I want to search which data does not contain in Database 1.
In single query one cannot fetch data from two different databases.You may take data in dataset and perform your operation.
You can use a three-part name to reference objects in another database (or four part if its also on another server/instance). Something like:
SELECT * --TODO, name columns
FROM
tblCustomer c
left join
Database1..tblCustomer c_not
on
c.CustomerID = c_not.CustomerID --TODO - Actual match conditions
WHERE
c_not.CustomerID is null --Only select rows where no match occurred.
(Here, I've assumed that the query is running in Database2 and that tblCustomer in Database1 is in the default schema)
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
Please help me in this....
There are few databases DB1,DB2,DB3,DB4,DB5....
each database has same tables T1,T2,T3.....Tn. (each table has same columns C1, C2, C3......Cn... but data in those tables are different)
Requirement: query: select C1, C2, C3 from T1 inner join T2 on T1.C4 = T2.C4
the query will be same for all databases.
First step is to create drop down menu for Databases DB1, DB2, DB3, DB4, DB5
Then select one or more databases after that the query should run for selected database or databeses.
What source should I select and how to create dataset for them?
Your datasource could be ANY of the databases, the master db, or a newly created database called say "DBA". Then after you have the source you'd create a stored procedure that would fill up your drop down list with database names:
select name from master..sysdatabases
A dataset in reporting services is simply either table direct or stored procedure. In your case, you should make that a stored procedure that pulls the names of all the databases.
A datasource in RS means where you are going to pull the data, in reality this doesnt matter because you can access any data from any database including linked servers. In the case of another database simply use the database name.owner.table name.