In SQL Server 2000 how to convert table to text file - sql-server-2000

In SQL Server 2000, I need to convert a table data (whole, not partial) to a text file (csv or tab).
How can I do this with a table (tblCustomer) which has name, address, phone number, for example?
I don't use SQL Server Management Studio. I have many groups of stored procedures running on daily basis and just want to write a stored procedure to add in to one of the groups.

Are you using SQL Server Management Studio?
If so, just right click on the database name, and choose tasks->export data from the context menu, then follow the wizard.

You you are using Management studio, you can try this:
In your SQL Server Management Studio, right-click on the database where the table is and select Tasks->Export Data. This will bring up
the SQL Server Import and Export Wizard, which is very much similar to
the DTS Import/Export utility of SQL Server 2000.
Select your table from the "Choose a Data Source" screen then click on Next.
In the "Choose a Destination", select "Flat File Destination" as your Destination and enter the File name.
Just click on Next after that and you should be ok already.
You can also export it to xml using a query and then, on your app, convert it do csv. Check this out: FOR XML
Or, as #Aaron said, use The BCP Utility
EDIT
As you said you want the query way, I would go for the For XML and them parse the result to a CSV format. AFAIK, SQL server does not have an way of doing what you want automatically, so you will have to make it yourself. Also, you can adapt this sample.
Here and here you can find some scripts that may help you.

Related

Copying an SQL table from one Server to another on SQL Server 2000 / 2005

I’m trying to copy a SQL Server table, schema and data, from Server A to Server B. The SQL Server table is just a reference table which hasn't been populated for some reason on Server B. Can anyone advise how the entire table could be copied across please? On SQL Server 2000/2005.
So far we've tried a long-winded approach by copying the .mdf and .ldf files from Server A to Server B with a plan to then copy the table across into the Server B database but we are having some difficulty re-attaching the database to Server B.
Please can anyone help?
Kind Regards
James
Using SQL Server Management Studio (SSMS):
In Object Explorer right click on source database name, Tasks.. -> Generate Scripts.. - opens Generate and Publish Scripts dialog. Click Next to choose objects, choose "Select specific DB objects", expand Tables, choose your table. Next, setup script destination, for example New query window and (important step!!) - click Advanced, and set "Types of data to script"="Schema and data" and "Script USE DATABASE"=False, OK, Next, Next, .. wait .. Finish. Now you have got complete SQL script to reproduce this table with data. Connect to destination DB and run it.
Tested with SSMS 2014, but as I recall this feature should be available starting from SSMS 2005.
you can use the import/export data wizard in management studio, the wizard will create for you a new table in the server B with the same structure of the table in the server A. before using it you need to have at least one database in sever B.
This confirms why this is one of favourite forums.
Both these methods work beautifully :
Generate Scripts (when altering Types of data to script"="Schema and
data")
Export and Import
Interestingly Generate Scripts works with SQL Express perfectly but the Export method does not save unless you have at least SQL Server Standard Edition.
Thanks so much everyone
Cheers
James
Try this:
SELECT * INTO destination FROM source
But, it will not copy the indexes and key information or you can also try import/export data task from SSMS.

Exporting a table from SQL Server 2008 R2 to a file WITHOUT external tools

I would like to export a table from SQL Server 2008 R2 to a file. The problem is that I don't have bcp (nor can I install it or anything else) and am not able to run xpcmdshell. Anyone have any ideas on how this could be done without those permissions/tools? (I would like to have this happen on some automated basis preferably)
I'm usually using Copy/Paste from SSMS Results Pane to Excel
OR
you can right click on database in the Object Explorer and select Database->Tasks->Export Data. An SQL Server Import and Export Wizard dialog opens and you will be able to export data from any table or query to the file or another destination.
OR
you can use LinqPad - awesome, simlpe and free tool (I really love it) that doesn't require installation
In the results pane, click the top-left cell to highlight all the records, and then right-click the top-left cell and click "Save Results As". One of the export options is CSV.
You can also use a command like this too:
INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\Test.xls;','SELECT productid, price FROM dbo.product')
Lastly, you can look into using SSIS (replaced DTS) for data exports. Here is a link to a tutorial: http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
If you have SQL Server 2012 you could add File Tables to your database. Thus you could use SQL Agent to schedule a simple stored proc to update the file table when desired.
http://technet.microsoft.com/en-us/library/ff929144.aspx#Description
it has something called "Query Analyzer"
Query Analyzer (isqlw.exe) is the SQL 2000, pre-SSMS, query tool. A very fine tool. Among other things, is capable of exporting query results to a file. See https://stackoverflow.com/a/3769766/105929:
go to the Tools -> Options menu. On the Results tab, choose to send your output to a CSV file and select the "Print column headers" option.

Generating a SQL script of my database's data (SQL SERVER)

I cannot find how to generate a script with all the INSERT i have done so far in my database
I managed to generate a script for my database itself but not for the data.
How could i do this ?
Thanks in advance
Try this, using Sql Server Management Studio:
Right click the database
Select Tasks -> Generate Scripts
(Click next if you get the intro screen)
Select "Select specific database objects"
Pick the objects to generate scripts for (tables, stored procedures, etc...)
Click Next, then specify the output filename
This will generate the schemas only. If you want to do data generating scripts as well, click the Advanced button and scroll down to the "Types of data to script" and change it from "Schema only" to "Data only" or "Schema and data"
Click Finish to generate the script
If you are using SQL Server 2008, you can generate the script for data in an sql server database by setting the Script Data option in Generate script dialog box
For bringing up Generate Scripts dialog do the following.
1. Right click on a database name in server explorer
2. Select Tasks -> Generate Scripts
3. Click next until you reaches script options
4. Under table/view options there is setting "Script Data". Change it to true to generate script for data.
If you version is the one prior to SQL Server 2008, then are many tools available like
Sql Compare from RedGate!

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.

SQL Server 2005 - Export table programmatically (run a .sql file to rebuild it)

I have a database with a table Customers that have some data
I have another database in the office that everything is the same, but my table Customers is empty
How can I create a sql file in SQL Server 2005 (T-SQL) that takes everything on the table Customers from the first database, creates a, let's say, buildcustomers.sql, I zip that file, copy it across the network, execute it in my SQL Server and voila! my table Customers is full
How can I do the same for a whole database?
This functionality is already built in to Sql Server Management Studio 2008.
Just download the trial and only install the client tools (which shouldn't expire). Use Management Studio 2008 to connect to your 2005 database (its backwards compatible).
Right click your database
Choose Tasks > Generate Scripts
Press Next, select your database again
On the 'Choose Script Options' screen, there is an option called Script Data which will generate SQL insert statements for all your data.
(Note: for SQL Server Management Studio 2008 R2, the option is called "Types of data to script" and is the last one in the General section. The choices are "data only", "schema and data", and "schema only")
Use bcp (from the command line) to a networked file and then restore it.
e.g.
bcp "SELECT * FROM CustomerTable" queryout "c:\temp\CustomerTable.bcp"
-N -S SOURCESERVERNAME -T
bcp TargetDatabaseTable in "c:\temp\CustomerTable.bcp" -N -S TARGETSERVERNAME -T
-N use native types
-T use the trusted connection
-S ServerName
Very quick and easy to embed within code. (I've built a database backup(restore) system around this very command.
You can check the following article to see how you can do this by using both SQL Server native tools and the third party tools: SQL Server bulk copy and bulk import and export techniques
Disclaimer: I work for ApexSQL as a Support Engineer
Hope this helps
You could always export the data from the Customers table to an Excel file and import that data into your Customers table.
To import/export data:
Right click on database
Go to Tasks
Go to Import Data or Export Data
Change the data source to Microsoft Excel
Follow the wizard
If both databases resides in the same instance of SQL Server, ie use same connection, this SQL might be helpful:
INSERT INTO [DestinationDB].[schema].[table] ([column])
SELECT [column] FROM [OriginDB].[schema].[table]
GO
For Data Expoer as SQL script in SQL server 2005,
http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-database-publishing-wizard/
I just like to add some screen shoots for Sql Server Management Studio 2008. It is correct to use the steps describe previously. When you have the 'Generate and Publish Script' -> 'Set Script Options' then press Advance to see script options:
![Where to find Advanced script options]: image missing because I do not have the right reputation :(
For Sql Server Management Studio 2008 the option to included data is 'Types of data to script'
![Types of data to script]: image missing because I do not have the right reputation :(