How do I export a table's data into INSERT statements? - sql

How can I export a table from a SQL Server 2000 database to a .sql file as a bunch of INSERT INTO statements?
One of the fields in the table is a Text datatype and holds HTML so doing this by hand would be rather time-consuming.
I have access to SQL Server Management Studio 2008 to access the SQL Server 2000 database.

Updating since this Q&A was at the top of the search results when I was looking for the answer.
In MSSQL 2008 R2:
Right Click on database: Tasks -> Generate Scripts...
The Generate and Publish Scripts dialog will pop up. The Intro page is worthless. Click "Next"
Choose "Select Specific database objects" and then select the Table(s) you want to get Inserts for. Click Next and the dialog will advance to the "Set Scripting Options".
Click on Advanced and you should see:
Scroll down the list of Options until you find "Types of data to script". Click on that row and choose "Data Only" from the pull-down. Click "OK". Choose your Save options and click "Next" a few times.
Note - The output also includes the following after every 100 inserts.
GO
print 'Processed 200 total records'

Check out the SSMS Tool Pack - it's a great, FREE add-on for SQL Server Management Studio which does a lot of things - among other it can generate INSERT statements from a given table.

I have been using this stored procedure for a long time: sp_generate_inserts: the 2000 version and the 2005 (and up) version.
You use it like this:
sp_generate_inserts 'thetablename'
or if you want to filter:
sp_generate_inserts 'thetablename', #from='from ... where ... order by ...'
The sp will return inserts statements as query results. Don't forget to modify setting: increase the maximum number of characters displayed in each column (tools - options - query results).

If you can use other DB management apps the quickest way would be using a tool like SqlDbx which has a built-in "Export as inserts (SQL)" function (just execute a query like SELECT * FROM Table and then use the contextual menu from the result grid).
If you need to stick to SQL Management Studio then you could use a stored procedure like this one:
http://vyaskn.tripod.com/code/generate_inserts.txt
It generates a set of results with the SQL INSERT statement for each row of the target table. Then you can exports the results to a file, or just copy them to the clipboard and paste in the query window (it works fine even with several megabytes of data).

Related

Dropping and recreating database, along with tables

I have a database whose structure I'm happy with, but which has a fair amount of dummy data in it. I would like to drop and recreate the database while at the same time wiping out the tables, but retain the table structures and relationships.
When I right-click on the database and choose to script 'drop and create' statements they're for the database itself, but no mention of the tables within-- unless I'm missing something.
Is it possible to generate a script that drops/creates a database and the tables within? I can individually select each table and script out their drop/create statements and order things so it will work, but are there other ways of doing this in one swoop?
I have a localized version of Sql Server 2008 R2, so some of my instructions could be imprecise.
I hope that there are no big differences.
Right click on your database and select Tasks and then Generate Scripts
Leave the first option selected (Build script for all db and objects) and select next
On the second page click Advanced
Select the option to Build Script for DROP & CREATE
Select the option to Use only the Schema (not Schema and Data)
Check the other options you would like to use.
Finally choose your save options and click Next until SSMS creates the script
Instead of that you should do this. That way you can select different tables or stored procedure to script for. See MSDN How to: Generate a Script (SQL Server Management Studio) on how to do it step-by-step.
Right click on DB_Name -> select tasks -> Generate Scripts

SQL- Editing the 10 most recent rows in a table [duplicate]

In SQL Server 2008 Management Studio, when I right click on a database table and choose Select Top 100 Rows, I can then e.g. easily add a ORDER BY statement to the SQL. That works fine.
But when I do choose Edit Top 200 Rows, I don't have the ability to alter the SQL (which makes it hard to find and edit a record just added in the 10,000 that are there.
I am quite sure I was able to do this in SQL Server 2000.
Is there any way in SMSS 2008 to alter the way the records are displayed when editing records?
If you right click on any result of "Edit Top 200 Rows" query in SSMS you will see the option "Pane -> SQL". It then shows the SQL Query that was run, which you can edit as you wish.
In SMSS 2012 and 2008, you can use Ctrl+3 to quickly get there.
You can also change the pop-up options themselves, to be more convenient for your normal use. Summary:
Run the SQL Management Studio Express 2008
Click the Tools -> Options
Select SQL Server Object Explorer . Now you should be able to see the options
Value for Edit Top Rows Command
Value for Select Top Rows Command
Give the Values 0 here to select/ Edit all the Records
Full Instructions with screenshots are here: http://m-elshazly.blogspot.com/2011/01/sql-server-2008-change-edit-top-200.html
Very quick and easy visual instructions to change this (and the select top 1000) for 2008 R2 through SSMS GUI
http://bradmarsh.net/index.php/2008/04/21/sql-2008-change-edit-top-200-rows/
Summary:
Go to Tools menu -> Options -> SQL Server Object Explorer
Expand SQL Server Object Explorer
Choose 'Commands'
For 'Value for Edit Top Rows' command, specify '0' to edit all rows
Follow the above image to edit rows from 200 to 100,000 Rows
Ctrl+3 in SQL Server 2012. Might work in 2008 too
in SQL 2017 You can do it more easily in the toolbar to the right just hit
the SQL button then its gonna apear the query with the top 200 you edit until the quantity that You want and Execute the query and Done! just Edit
Similar to David Hall's response, if you are a non-coder, it may be easiest to right-click within results, then choose Pane > Criteria. This allows you to adjust sort, add filters, etc... without adjusting SQL code.
The default to open/add rows to a table is Edit Top 200 Rows. If you have more than 200 rows, like me now, then you need to change the default setting. Here's what I did to change the edit default to 300:
Go to Tools in top nav
Select options, then SQL Service Object Explorer (on left)
On right side of panel, click into the field that contains 200 and change to 300 (or whatever number you wish)
Click OK and voila, you're all set!

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!

Create SQL script that create database and tables

I have a SQL database and tables that I would like to replicate in another SQL Server. I would like to create a SQL script that creates the database and tables in a single script.
I can create "Create" script using the SQL Management Studio for each case (Database and Tables), but I would like to know if combining the both "Create" scripts into single script would be enough.
Thanks.
Although Clayton's answer will get you there (eventually), in SQL2005/2008/R2/2012 you have a far easier option:
Right-click on the Database, select Tasks and then Generate Scripts, which will launch the Script Wizard. This allows you to generate a single script that can recreate the full database including table/indexes & constraints/stored procedures/functions/users/etc. There are a multitude of options that you can configure to customise the output, but most of it is self explanatory.
If you are happy with the default options, you can do the whole job in a matter of seconds.
If you want to recreate the data in the database (as a series of INSERTS) I'd also recommend SSMS Tools Pack (Free for SQL 2008 version, Paid for SQL 2012 version).
In SQL Server Management Studio you can right click on the database you want to replicate, and select "Script Database as" to have the tool create the appropriate SQL file to replicate that database on another server. You can repeat this process for each table you want to create, and then merge the files into a single SQL file. Don't forget to add a using statement after you create your Database but prior to any table creation.
In more recent versions of SQL Server you can get this in one file in SSMS.
Right click a database.
Tasks
Generate Scripts
This will launch a wizard where you can script the entire database or just portions. There does not appear to be a T-SQL way of doing this.
An excellent explanation can be found here: Generate script in SQL Server Management Studio
Courtesy Ali Issa Here's what you have to do:
Right click the database (not the table) and select tasks --> generate scripts
Next --> select the requested table/tables (from select specific database objects)
Next --> click advanced --> types of data to script = schema and data
If you want to create a script that just generates the tables (no data) you can skip the advanced part of the instructions!
Not sure why SSMS doesn’t take into account execution order but it just doesn’t. This is not an issue for small databases but what if your database has 200 objects? In that case order of execution does matter because it’s not really easy to go through all of these.
For unordered scripts generated by SSMS you can go following
a) Execute script (some objects will be inserted some wont, there will be some errors)
b) Remove all objects from the script that have been added to database
c) Go back to a) until everything is eventually executed
Alternative option is to use third party tool such as ApexSQL Script or any other tools already mentioned in this thread (SSMS toolpack, Red Gate and others).
All of these will take care of the dependencies for you and save you even more time.
Yes, you can add as many SQL statements into a single script as you wish. Just one thing to note: the order matters. You can't INSERT into a table until you CREATE it; you can't set a foreign key until the primary key is inserted.