Efficiently moving large sets of data between SQL Server tables? - sql-server-2005

I have a rather large (many gigabytes) table of data in SQL Server that I wish to move to a table in another database on the same server.
The tables are the same layout.
What would be the most effecient way of going about doing this?
This is a one off operation so no automation is required.
Many thanks.

If it is a one-off operation, why care about top efficiency so much?
SELECT * INTO OtherDatabase..NewTable FROM ThisDatabase..OldTable
or
INSERT OtherDatabase..NewTable
SELECT * FROM ThisDatabase..OldTable
...and let it run over night. I would dare to say that using SELECT/INSERT INTO on the same server is not far from the best efficiency you can get anyway.

Or you could use the "SQL Import and Export Wizard" found under "Management" in Microsoft SQL Server Management Studio.

I'd go with Tomalak's answer.
You might want to temporarily put your target database into bulk-logged recovery mode before executing a 'select into' to stop the log file exploding...

If it's SQL Server 7 or 2000 look at Data Transformation Services (DTS). For SQL 2005 and 2008 look at SQL Server Integration Services (SSIS)

Definitely put the target DB into bulk-logged mode. This will minimally log the operation and speed it up.

Related

Sql: export database using TSQL

I have database connection to database DB1. The only thing I could do - execute any t-sql statements including using stored procedures. I want to export the specific table (or even the specific rows of specific table) to my local database. As you can read abve, DBs are on diffrent servers meaning no direct connection is possible. Therefore question: Is it possible to write query that returns the other query to execute on local server and get data? Also note, that table contains BLOBs. Thanks.
If you have SQL Server Management Studio, you can use the data import function on your local database to get the data. It works as long as you have Read/Select access on the tables you are trying to copy.
If you have Visual Studio you can use the database tools in there to move data between two servers as long as you can connect to both from your workstation.
Needs Ultimate or Premium though:
http://msdn.microsoft.com/en-us/library/dd193261.aspx
RedGate has some usefull tools too:
http://www.red-gate.com/products/sql-development/sql-compare/features
Maybe you should ask at https://dba.stackexchange.com/ instead.
If you can login to the remote db (where you can only issue t-sql), you may create linked server on your local server to the remote and use it later directly in queries, like:
select * from [LinkedServerName].[DatabaseName].[SchemaName].[TableName]

Is it useful to use Auto Create Statistics = "ON" on a SQL Server 2005 DB?

After restoring a large DB (70 Gb) from SQL Server 2000 onto a SQL Server 2005 Server we had encountered loss of performance while running queries, functions or procedures that on SQL Server 2000 worked ok.
After some troubleshooting we found that SQL Server Option "Auto Create Statistics" was ON, and after set it to OFF, the performance was improved very much.
Why was this option so bad to use in my case?
When is it OK to use Auto Create Statstistics = ON?
Thx
I think what you really need to do is address why you are experiencing frequent statistic creation/update operations.
Did you update ALL of your statistics as part of your Upgrade process from SQL Server 2000 to 2005? You should have.
If you did not perform this as part of your upgrade process, then SQL will be creating the required statistics that it needs to perform your queries, as and when you execute them, when you have the AUTO CREATE STATS setting enabled. This of course creates resource overhead.

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.

How do I make a script in SQL Management Studio 2005?

I have a table in an MS SQL Server db. I want to create a script that will put the table and all records into another db. So I right-click the table in Management Studio and select Create-To new query editor... but all I get is the table structure.
How exactly do I get the values too?
One of the things I really like about the tools for MySQL that SQL Server is missing out of the box to be certain.
You can use a script to do it however.
You might also want to consider using something like Red-Gate SQL Compare and Red-Gate SQL Data Compare. They aren't cheap tools, priced at $395 each (for the standard editions), but there are 14 day free trials available for download, and they make copying schema and data from one SQL Server to another very easy.
If both are on the same machine (or on different machines but the servers are linked)
you can create the table with the script you can generate automatically and do this to copy the data:
INSERT INTO [destinationdb].[dbo].[destinationtable] SELECT *
FROM [originaldb].[dbo].[originaltable]
(Prepend [servername] to the database name if you'll be using linked servers)
Another option is to enable xp_cmdshell (do with care, it's relaxing security constraints) and use the bcp command line utility from the management studio to create copies you can then import into the other database/server. You can do that directly from the shell as well and do not need to enable xp_cmdshell in that case, of course.
it doesn't really create a "SQL script" but it does the job :
select the database in the object explorer
right click
select import/export data
follow the wizard
at the end of the process you can save the "integration service package" to reuse it
later you can modify the details by opening the .dtsx
(it will take care of security, and won't cost one more penny, it's seems we have to compete with other answers :) )
hope it helps.

Cross-server SQL

I want to port data from one server's database to another server's database.
The databases are both on a different mssql 2005 server.
Replication is probably not an option since the destination database is generated from scratch on a [time interval] basis.
Preferebly I would do something like
insert *
from db1/table1
into db2/table2
where rule1 = true
It's obvious that connection credentials would go in somehwere in this script.
I think what you want to do is create a linked server as per this webarchive snapshot of msdn article from 2015 or this article from learn.microsoft.com. You would then select using a 4 part object name eg:
Select * From ServerName.DbName.SchemaName.TableName
You can use Open Data Source Like this :
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
SELECT *
FROM OPENDATASOURCE('SQLOLEDB',
'Data Source=<Ip Of Your Server>;
User ID=<SQL User Name>;Password=<SQL password>').<DataBase name>.<SchemaName>.<Table Or View Name>
Go
Are SQL Server Integration Services (SSIS) an option? If so, I'd use that.
Would you be transferring the whole content of the database from one server to another or just some data from a couple of tables?
For both options SSIS would do the job especially if you are planning to to the transfer on a regular basis.
If you simply want to copy some data from 1 or 2 tables and prefer to do it using TSQL in SQL Management Studio then you can use linked server as suggested by pelser
Set up the source database server as a linked server
Use the following syntax to access data
select columnName1, columnName2, etc from serverName.databaseName.schemaName.tableName
Well I don't agree with your comment on replication. You can start a replication by creating a database from scratch, and you can control either the updates will be done by updating the available client database or simply recreating the database.
Automated replication will ease your work by automatically managing keys and relations.
I think the easiest thing to do is to start a snapshot replication through MSSQL Server Studio, get the T-SQL corresponding scripts (ie the corresponding T-SQL instructions for both publication and subscriptions), and record these scripts as part of a job in the Jobs list of the SQL Agent or as a replication job in the replications folder.
You could go the linked server route.
you just can't use the select * into you have to do an insert into select.
I would avoid replication if you don't have experience with it as it can be difficult to fix if it breaks and can be prone to other problems if not properly managed.
Keep it simple especially if the databases are small.
Can you use the Data Transformation Services to do the job? This provides all sorts of bolt-together tools for doing this kind of thing.
You can download the SQL Server 2005 feature pack from Microsoft's website
here
CREATE VIEW newR1
AS
SELECT * from OPENQUERY ([INSTANCE_NAME], 'select * from DbName.SchemaName.TableName')