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

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!

Related

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.

In SQL Server 2000 how to convert table to text file

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.

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.

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

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).

How to create automatically a table populate script from current data in SQL Server?

Is there a tool that allows me to create a sql populate script for a table using its current data?
Old question, but I got to the question before figuring out a way to solve the problem. So I'm just posting the method here in case it might help anyone else.
In SSMS:
Right click on db -> "Tasks" -> "Generate Scripts..."
Click "Next" and, at "Choose Objects" step, select the specific table
Click "Next" and, at "Set Scription Options" step, click "Advanced"
Set value for "Type of data to script", depending on what you need
Continue to finish
Check out the SSMS Addins (for both SSMS 2005 and 2008):
alt text http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=SSMSAddins&DownloadId=48526
One of the tools included in the "DataScripter" which will script out INSERT statements for the table(s) you want to.
Maybe this is just enough for you: http://snipplr.com/view/12003/generate-insert-statements/
You could look at purchasing the Data Generator by Red-Gate which can create random sets of data based on your database schema.
Note: I'm not sure if the tool will generate data based on your current data though
You can do it with Database Publishing Wizard. Here is a tutorial: Generate Script with Data from Database – Database Publishing Wizard
Another way to do it, even more convenient for developers, is within Visual Studio 2015 or 2017 (possibly earlier versions too):
Main menu 'View' -> 'SQL Server Object Explorer'
In the tree view connect to the Database of choice and select a table
Right click on the table and select 'View Data' menu item
In the [Data] window click on a 'Script' or 'Script to File' button