Export records from SQL Server 2005 express edition - sql

I have a little problem. My friend has a database with over 10 tables and each table has over 90-100 records.
I can't find a workaround to export the records (to put in a SQL file something like this: INSERT INTO .... VALUES ... for each existing records) from his tables to import in my database.
How to do that ?
I tried: right click on a table -> Script Table as -> INSERT TO -> File ...
but it only generate the INSERT statement.
There are a solution ? or this feature is only for commercial version ?
UPDATE
You can use BCP command with command prompt like this
For export: bcp ADatabase.dbo.OneTable out d:\test\OneTable.bcp -c -Usa -Ppassword
For import: bcp ADatabase.dbo.OneTable in d:\test\OneTable.bcp -c -Usa -Ppassword
these commands will create a BCP file which contains records for specified table. You can import using existing BCP file into another database
If you use remote database then:
bcp ADatabaseRemote.dbo.OneTableRemote out d:\test\OneTableRemote.bcp -Slocalhost/SQLExpress -Usa -Ppassword
Instead of localhost/SQLExpress, you can use localhost or other server name...

Probably the simplest way to do this would be to run a SELECT statement that outputs to a file. Then you can import that data into your database.
For simple moves, I have also done a copy/paste manually. Sometimes it is better to use Excel as a staging platform before pasting it into the new database. You may need to create a temporary table in your new database that matches up exactly with the data you are pasting over. For example, I usually don't put a PK on the temp table at first and make the PK field just an INT. That way the copy will go smoother.
In the corporate world, you would use SSIS to move this data around.

a couple of ways you could do this. One,select everything from each table and save the results as a csv or delimited file (you can do this from sql management studio). You can also script the tables as create and copy the scripts over to the new database, assuming it is a sql server also. Then for import use the load infile statement. You may have to google the syntax for sql server but I know this works in mysql and oracle. haven't tried it in sql server yet.
LOAD DATA INFILE 'myfile'
INTO TABLE stuff
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
SET id = NULL;
Or if you are going to another sql server use the sql export import wizard.
http://msdn.microsoft.com/en-us/library/ms141209.aspx

Related

Export a Large SQL Database via Query

I am trying to export a database within Microsoft SQL Server Management Studio 2000. My database is extremely large with over 1,300 unique tables.
I am aware of the export wizard and copy/paste options but am looking for something that would allow me to export all 1,300 tables (or at least a few hundred at once) into a single csv or xls file. Copy and pasting or selecting "save as" for each table would take far too long and the export wizard only allows a few dozen tables at a time.
Well , I would not recommended you to do this, but if you desperately need a solution in the way you have described , here it is :
First , run this query on the database :
SELECT 'sqlcmd -S . -d '+DB_NAME()+' -E -s, -W -Q "
SET NOCOUNT ON;
SELECT * FROM '+table_schema+'.'+TABLE_name+'" > "C:\Temp\'+Table_Name+'.csv"'
FROM [INFORMATION_SCHEMA].[TABLES]
You might want to change the folder name as per your convenience
Second, Copy all the rows returned into a Export.bat or Export.cmd file
Third, run the Export.bat file to get 1300 odd tables you need in separate CSV files
Fourth, open a cmd window , navigate to the folder you have your files in and use the following command :
copy *.csv Export.csv
You will have a single Export.csv file containing all your tables, along with headers for each table
Perhaps this will help you to resolve your problem.
SQL Server Management Studio 2012 - Export all tables of database as csv

Moving data from Excel to SQL Server table

I have a very Simple excel sheet:
I am wanting to put this data into a table in SQL Server. I also wanted to add a field that contains a date.
what is the best way to do this?
Create a table in SQL server with the same number of fields that you have in the spreadsheet.
In SQL Server Management Studio Object Explorer you can right click the table you created and select "Edit top 200 rows". Highlight the data you want to copy in Excel and right click -> copy. Right click the space to the left of the first row in the SSMS edit tabe widow and paste.
After the import check the SQL table to make sure it has the same amount of rows as the spreadsheet.
You can add the date column to the SQL table after you do the data import. Or add it in the spreadsheet before you do the import.
You can first create the table in SQL Server with the added field and then use the import wizard in the Mangement Studio for importing the excel file. Or you create the table during the import task, and you add the new field later.
Option 1:
Read the data in an IDataReader, and then call a stored procedure to insert the data.
http://granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/
I use the above when I have ~~alot~~ of rows to import and I want to validate them outside of the database.
Option 2:
http://support.microsoft.com/kb/321686
or search for:
Select FROM OPENDATASOURCE Excel
Option N:
There are other options out there.
It depends what you like, how much time you want to put into it, is it a "one off" or you gotta do it for 333 files every day.
My solution was to convert .xlsx to .csv and then use this site to convert .csv to .sql. I then ran the sql file in sql server and generated my tables.
It can be also be done by creating a batch file.
For this you already need to have a table created on server with the same data structure as you have in excel.
Now using BCP you can import that data from the excel file and insert it into sql table.
BCP utility function
sqlcmd -S IP -d databasename -U username -P passwd -Q "SQL query mostly it should be truncate query to truncate the created table"
bcp databasename.dbo.tablename in "Excel file path from where you need to input the data" -c -t, -S Server_IP -U Username -P passwd -b provide batch size
You can refer to the link below for more options on the bcp utility:
https://msdn.microsoft.com/en-IN/library/ms162802.aspx
Open your SQL server interface software, add the date field to the table.
Go to excel, add the date column, copy the excel data.
Go to your SQL server interface software and use the functionality to import the data from the clipboard. (SQL server interface software that has this is for example Database4.net, but if you have another package with the functionality then use that.)
Alternatively use VBA with DOA or ADO to interact with the SQL server database and use SQL statements to add the field and copy the data into your table

Export table data from one SQL Server to another

I have two SQL Servers (both 2005 version).
I want to migrate several tables from one to another.
I have tried:
On source server I have right clicked on the database, selected Tasks/Generate scripts.
The problem is that under Table/View options there is no Script data option.
Then I used Script Table As/Create script to generate SQL files in order to create the tables on my destination server. But I still need all the data.
Then I tried using:
SELECT *
INTO [destination server].[destination database].[dbo].[destination table]
FROM [source server].[source database].[dbo].[source table]
But I get the error:
Object contains more than the maximum number of prefixes. Maximum is
2.
Can someone please point me to the right solution to my problem?
Try this:
create your table on the target server using your scripts from the Script Table As / Create Script step
on the target server, you can then issue a T-SQL statement:
INSERT INTO dbo.YourTableNameHere
SELECT *
FROM [SourceServer].[SourceDatabase].dbo.YourTableNameHere
This should work just fine.
Just to show yet another option (for SQL Server 2008 and above):
right-click on Database -> select 'Tasks' -> select 'Generate Scripts'
Select specific database objects you want to copy. Let's say one or more tables. Click Next
Click Advanced and scroll down to 'Types of Data to script' and choose 'Schema and Data'. Click OK
Choose where to save generated script and proceed by clicking Next
If you don't have permission to link servers, here are the steps to import a table from one server to another using Sql Server Import/Export Wizard:
Right click on the source database you want to copy from.
Select Tasks - Export Data.
Select Sql Server Native Client in the data source.
Select your authentication type (Sql Server or Windows authentication).
Select the source database.
Next, choose the Destination: Sql Server Native Client
Type in your Server Name (the server you want to copy the table to).
Select your authentication type (Sql Server or Windows authentication).
Select the destination database.
Select Copy data.
Select your table from the list.
Hit Next, Select Run immediately, or optionally, you can also save the package to a file or Sql Server if you want to run it later.
Finish
There is script table option in Tasks/Generate scripts! I also missed it at beginning! But you can generate insert scripts there (very nice feature, but in very un-intuitive place).
When you get to step "Set Scripting Options" go to "Advanced" tab.
Steps described here (pictures can understand, but i do write in latvian there).
Try using the SQL Server Import and Export Wizard (under Tasks -> Export Data).
It offers to create the tables in the destination database. Whereas, as you've seen, the scripting wizard can only create the table structure.
If the tables are already created using the scripts, then there is another way to copy the data is by using BCP command to copy all the data from your source server to your destination server
To export the table data into a text file on source server:
bcp <database name>.<schema name>.<table name> OUT C:\FILE.TXT -c -t -T -S <server_name[ \instance_name]> -U <username> -P <Password>
To import the table data from a text file on target server:
bcp <database name>.<schema name>.<table name> IN C:\FILE.TXT -c -t -T -S <server_name[ \instance_name]> -U <username> -P <Password>
For copying data from source to destination:
use <DestinationDatabase>
select * into <DestinationTable> from <SourceDataBase>.dbo.<SourceTable>
Just for the kicks.
Since I wasnt able to create linked server and since just connecting to production server was not enough to use INSERT INTO i did the following:
created a backup of production server database
restored the database on my test server
executed the insert into statements
Its a backdoor solution, but since i had problems it worked for me.
Since i have created empty tables using SCRIPT TABLE AS / CREATE in order to transfer all the keys and indexes I couldnt use SELECT INTO. SELECT INTO only works if the tables do not exist on the destination location but it does not copy keys and indexes, so you have to do that manualy. The downside of using INSERT INTO statement is that you have to manualy provide with all the column names, plus it might give you some problems if some foreign key constraints fail.
Thanks to all anwsers, there are some great solutions but i have decided to accept marc_s anwser.
You can't choose a source/destination server.
If the databases are on the same server you can do this:
If the columns of the table are equal (including order!) then you can do this:
INSERT INTO [destination database].[dbo].[destination table]
SELECT *
FROM [source database].[dbo].[source table]
If you want to do this once you can backup/restore the source database.
If you need to do this more often I recommend you start a SSIS project where you define source database (there you can choose any connection on any server) and create a project where you move your data there.
See more information here: http://msdn.microsoft.com/en-us/library/ms169917%28v=sql.105%29.aspx
It can be done through "Import/Export Data..." in SQL Server Management Studio
This is somewhat a go around solution but it worked for me I hope it works for this problem for others as well:
You can run the select SQL query on the table that you want to export and save the result as .xls in you drive.
Now create the table you want to add data with all the columns and indexes. This can be easily done with the right click on the actual table and selecting Create To script option.
Now you can right click on the DB where you want to add you table and select the Tasks>Import .
Import Export wizard opens and select next.Select the Microsoft Excel as input Data source and then browse and select the .xls file you have saved earlier.
Now select the destination server and also the destination table we have created already.
Note:If there is any identity based field, in the destination table you might want to remove the identity property as this data will also be inserted . So if you had this one as Identity property only then it would error out the import process.
Now hit next and hit finish and it will show you how many records are being imported and return success if no errors occur.
Yet another option if you have it available: c# .net. In particular, the Microsoft.SqlServer.Management.Smo namespace.
I use code similar to the following in a Script Component of one of my SSIS packages.
var tableToTransfer = "someTable";
var transferringTableSchema = "dbo";
var srvSource = new Server("sourceServer");
var dbSource = srvSource.Databases["sourceDB"];
var srvDestination = new Server("destinationServer");
var dbDestination = srvDestination.Databases["destinationDB"];
var xfr =
new Transfer(dbSource) {
DestinationServer = srvDestination.Name,
DestinationDatabase = dbDestination.Name,
CopyAllObjects = false,
DestinationLoginSecure = true,
DropDestinationObjectsFirst = true,
CopyData = true
};
xfr.Options.ContinueScriptingOnError = false;
xfr.Options.WithDependencies = false;
xfr.ObjectList.Add(dbSource.Tables[tableToTransfer,transferringTableSchema]);
xfr.TransferData();
I think I had to explicitly search for and add the Microsoft.SqlServer.Smo library to the references. But outside of that, this has been working out for me.
Update: The namespace and libraries were more complicated than I remembered.
For libraries, add references to:
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.SmoExtended.dll
Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Management.Sdk.Sfc.dll
For the Namespaces, add:
Microsoft.SqlServer.Management.Common
Microsoft.SqlServer.Management.Smo

Which one is good way to Import Excel to Database?

Hi i am using SQL Server 2008.
How can I import an Excel file into the database, which is the easiest way and simple to do?
OpenRowSet
BulkCopy
Linked Servers
SSIS
I have the above options to Import Excel to Database.
In my opinion SSIS wizard is best way to import excel data where you get row and column wise whole view of table data which will be inserted and also specify column names and contraints and parse data using query.
UPDATE :
If the data in your excel file does not require any processing to match your database table then I recommend you save your excel file as a csv and use a combination of BULK INSERT and the BCP.exe program.
To use BULK INSERT you will need a format file which defines how your datafile matches up to your database table. You can write this by hand to match the existing database table or you can use the following command to generate the format file you need:
bcp [ServerName].[SchemaName].[TableName] format nul -c -f [FormatFileOutputName].fmt -S[ServerHostName] -U[DbUserName] -P[DbUserPassword]
Now you will have 2 files:
DatafileName.csv
FormatFileName.fmt.
Use BULK INSERT within Sql Server to insert your data.
Note: If the columns in your datafile are in a different order than your database table then you can simply edit the generated format file to have them map correctly.

How to view tables in sql?

Hai i am a beginner of Database,
i have a .sql file which contains some tables of data, i want to know how to import them and how to view the list of tables.
presently im using the following:-
software or editor : navicat lite
server : localhost.
databse file format: .sql
Maybe you can try to execute the script in sql server, then type
select * from [database_name].information_schema.tables
to view tables and relevant information.
Remember that a sql file is not really a database, it is a script. You can run the script from any tool, but I'd use command line. This is navicat connected to mysql?
mysql -u username -p databasename < script.sql
password: **
And then the results can be seen using navicat or any other tool
If the .sql file has statements such as "CREATE TABLE..." and then later on "INSERT INTO..." then the script is possibly creating the tables and inserting the data.
To allow that to happen, the tables need to not exist in the database. You can then run the script and it will create the tables and fill in the data.
If the tables do exist, you can always either delete them, or change the CREATE to an ALTER and the script should then run.
Hope that helps.