SQL Server 2008 Query Between Databases - sql

We are migrating database structures, so I have one database with the old structure and one database with the new structure (both on the same server). I want to write queries to copy data from one to the other. I am expecting to go table-by-table as the schema is different. How do I do this?

You need to provide more details to get a more specific answer, but in general you just use the three-part name:
INSERT INTO NewDB.dbo.TableName
SELECT <columns>
FROM OldDB.dbo.Tablename
Are you looking for a way to do this automatically for all the tables?

You can write cross database queries like so
INSERT INTO NewDatabase.Schema.Table
SELECT Column1, Column2
FROM OldDatabase.Schema.Table

you can probably use Import data under tasks.Right Click the Target DB -> Tasks ->Import Data .You can also specify the source-> target mapping here.. and also write queries

Related

Adding dbo or Support in stores proc

A colleague of mine has given me a task to ensure all table name and pages in a stored procedure contains the relevant schemas?
For example if I have a table in a database that is dbo.table1, then in a query if I have:
Select * from table1
He wants me to change it to:
Select * from dbo.table1
This is same for pages that start with Support.
What is the significance of adding in a scheme like dbo. At the start when manually writing SQL? Is it suppose to be better for performance as it seems to know where the tables even if I don't include .dbo at the start?
I'm using SQL server 2012 and its management studio.
Thank you
You may not notice the performance if you write the schema in all of your queries. If you don't specify it, the engine will look through all of your schemas in your databases ( I'm not pretty sure if the engine will check the sys schemas too ) until it finds the table you're getting the data; It's recommended as a best practice to write it because you are telling to the engine what schema need to search the table.
I hope this answer was helpful.
dbo is the schema which is used as part naming convention for tables in a mssql database
look at this post SQL Server edits the table name with dbo as prefix

Select into from one sql server into another?

I want to select data from one table (T1, in DB1) in one server (Data.Old.S1) into data in another table (T2, in DB2) in another server (Data.Latest.S2). How can I do this ?
Please note the way the servers are named. The query should take care of that too. That is,
SQL server should not be confused about fully qualified table names. For example - this could confuse SQL server - Data.Old.S1.DB1.dbo.T1.
I also want "mapping" . Eg Col1 of T1 should go to Col18 of T2 etc.
Use Sql Server Management Studio's Import feature.
right click on database in the object explorer and select import
select your source database
select your target database
choose the option to 'specify custom query' and just select your data from T1, in DB1
choose your destination table in the destination database i.e. T2
execute the import
create a linked server. then use an openquery sql statement.
select * into [newtable] from [linked_server].[databasename].dbo.[tablename]
If it is just one time linked server in appropriate.
But if it neede to move data frequently ,replication is better and easier.
I think you're overcomplicating it by insisting on SQL. In SSMS, right-click the server you want to export from, select "Tasks", "Export", and let the wizard walk you through the steps of selecting your target server and table, which includes mapping all of the columns exactly as you're trying to do with your SQL situation. All the functionality you seem to be looking for is already there.
There's no need for linked servers, SSIS, or anything else to accomplish this. It's already built into SQL Server Management Studio.
Based on the accepted answer.
Make sure the source server (server1) is linked to the destination server (server2):
SELECT *
INTO Server2.DB2.dbo.T2
FROM OPENQUERY (server1
, ' SELECT col_one, col_two, ...
FROM DB1.dbo.T1
WHERE ...
...
);
You could look into this:
Clicky!
Or you could use SSIS, which would probably be much simpler.

Import data from one database to another? Databases are the same

I have two instances of same database and I want to have only one instance of database, so what I need is to import data from one databes to another.
Ofc I can go table by table, but there is lots of tables. I was wondering is there sql statement that can merge two databases or is there option in Microsoft SQL server database studio to this thing?
There is nothing built in like that.
If you don't want to go the manual way (comparing and then importing/exporting), you can look at a propriety tool like RedGate Data Compare that can accomplish this.

How do you transfer all tables between databases using SQL Management Studio?

When I right click on the database I want to export data from, I only get to select a single table or view, rather than being able to export all of the data. Is there a way to export all of the data?
If this is not possible, could you advise on how I could do the following:
I have two databases, with the same table names, but one has more data than the other
They both have different database names (Table names are identical)
They are both on different servers
I need to get all of the additional data from the larger database, into the smaller database.
Both are MS SQL databases
Being that both are MS SQL Servers, on different hosts... why bother with CSV when you can setup a Linked Server instance so you can access one instance from the other via a SQL statement?
Make sure you have a valid user on the instance you want to retrieve data from - it must have access to the table(s)
Create the Linked Server instance
Reference the name in queries using four name syntax:
INSERT INTO db1.dbo.SmallerTable
SELECT *
FROM linked_server.db.dbo.LargerTable lt
WHERE NOT EXISTS(SELECT NULL
FROM db1.dbo.SmallerTable st
WHERE st.col = lt.col)
Replace WHERE st.col = lt.col with whatever criteria you consider to be duplicate values between the two tables.
There is also a very good tool by Redgate software that syncs data between two databases.
I've also used SQL scripter before to generate a SQL file with insert statements that you can run on the other database to insert the data.
If you right-click on the database, under the Tasks menu, you can use the Generate Scripts option to produce SQL scripts for all the tables and data. See this blog post for details. If you want to sync the second database with the first, then you're better off using something like Redgate as suggested in mpenrow's answer.

Move Data from Oracle to SQL Server

I would like to copy parts of an Oracle DB to a SQL Server DB. I need to move the data because the Oracle box is being decommissioned. I only need the data for reference purposes so don't need indexes or stored procedures or contstaints, etc. All I need is the data.
I have a link to the Oracle DB in SQL Server. I have tested the following query, which seemed to work just fine:
select
*
into
NewTableName
from
linkedserver.OracleTable
I was wondering if there are any potential issues with using this approach?
Using SSIS (sql integration services) may be a good alternative especially if your table names are the same on both servers. Use the import wizard via and it should create the destination tables for you and let you edit any mappings.
The only issue I see with that is you will need to execute that of course for each and every table you need. Glad you are decommissioning the oracle server :-). Otherwise if you are not concerned with indexes or any of the existing sprocs I don't see any issue in what you are doing.
The "select " approach could be very slow if tables are large. Consider writing pro*C in that case or use Fastreader http://www.wisdomforce.com/products-FastReader.html
A faster and easier approach might be to use the Data Transformation Services, depending on the number of objects you're trying to copy over.