I really enjoyed writing about SQL SERVER – 2005 – Create Script to Copy Database Schema and All The Objects – Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects. Since then the I have received question that how to copy data as well along with schema. The answer to this is Database Publishing Wizard. This wizard is very flexible and works with modes like schema only, data only or both. It generates a single SQL script file which can be used to recreate the contents of a database by manually executing the script on a target server.
The pre-requisite for Database Publishing Wizard is .NET 2.0 Framework, SQL Server 2005 Management Objects, SMO. The Database Publishing Wizard will script all objects that the supplied User has permissions to see in the source database. Any objects created “WITH ENCRYPTION” cannot be scripted. If such objects exist in the source database, the tool will not produce a script.
First of all install Database Publishing Wizard from here : Download Database Publishing Wizard.
It will be installed at following location : C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\
Now login using Command prompt and run following command on any desire database, it will create the script at your specified location. Script will have schema as well as data which can be used to create the same information on new server.
Command to run which will create schema and database:
C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz script -d AdventureWorks “C:\AdventureWorks.sql”
Command to run which will create schema:
C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz script -d AdventureWorks “C:\AdventureWorks.sql” -schemaonly
Command to run which will create data:
C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz script -d AdventureWorks “C:\AdventureWorks.sql” -dataonly
Command windows will generate output of action it is taking. See below two screen shots of it.


If you have followed this tutorial exactly you will end up with adventurework.sql which will be quite big and if your computer is not powerful enough it will hang your computer for a while. I suggest that you try this on smaller database of size around 100MB.
Reference : Pinal Dave (http://blog.SQLAuthority.com) , Database Publishing Wizard












Can u suggest me how to script the remaining objects of the database excluding the encrypted one’s….as this wizard is not having the capacity to script encrypted objects
why users are converted to roles during the scripting. How to overcome this limitation? Apart from that its a good tool
Try SQLScripter (http://www.sqlscripter.com) to script your data. This tool supports all DML commands (Insert, Update, Delete) as well as some other cool stuff (Export to Text/Csv, Excel, Pdf …). Currently, it’s free.
Cheers
Please anyone advice …
The scriptor is running fine but it is not scripting the database even if a single object is encrypted. It should actually exclude that encrypted object instead of excluding whole objects in that database.
Can any one advice me how to exclude the object which in encrypted..instead of excluding the entire database…..
FYI if you spell the database name wrong this program gives you cryptic errors (like it cannot find the destination folder of the generated script file)
This worked perfect. Thanks for posting this.
how can we use this for database on remote machine..i.e using sql server authentications
Hello Pindaldave
I tried to generate a script with the publishing wizard, but it didnt work because i should give the username and password in order to access the database server. I want only the data to be scripted. Is it possible in the command line?
Thank you
Hi,
Does anyone know if there’s anyway to use command to generate data on specific tables rather than on all tables? On the Data Publishing Wizard documentation, one of the example shows a -include switch, however, when I tried to use it in command, it complains that -include switch is not recognized. Thanks~
I have used the publishing wizard and it works in most cases. for the situations where I have any of the following special characters the inserts fail:
”
crlf
‘
Is there some strategy that I can use to avoid the problems caused by the characters.
The only thing that I can think of is to have a translation before insertion and then translate back after retrieval for all fields for all 3 characters.
Thanks,
Stan
Hi,
In SQL Server 2005, is there any features provided to backup tables and data in *.sql format? exclude any other external tools to perform the backup. I tried the Generate Script features. while i tried to follow the wizard, my database doesn’t appear on the database selection?may i know why?
Akram
Hi,
We are generally using script generation to get table structures only. With data means backup and restore.
Then Why we need Database Publishing wizard?.
Thank You
Rgds,
Balasubramaniyam
Balasubramaniyam,
Here is a good case for when you want to script the schema and the data, but you do NOT want to backup & restore: You want to reorder the columns in a table!
After you run the wizard, find the scripted CREATE TABLE xxx in the SQL, cut and paste the columns for that table in the desired order, then execute the SQL. The entire database is re-created, the data inserted, and your columns are in the desired order.
JC
When I script my database to an .sql file, I cant run it on my new server. it seems to be working but the table never gets created and the data never goes in.
What am I doing wrong? I successfully pulled all data from my remote host into the sql file and confirmed that it’s in there, but when I run it as a query against my new DB, I get NADA.
Please help! this is frustrating!
If we want to generate insert script for inserted data from sql server then what we can do?
Because when we use generate script from sql server at that time it shows only structure of table not the inserted data.
It working threw import/export wizard but if we have foreign key then it not work.
Can any one help me?
Thanks in advance…
i have a scrilpt file.i want to generate the database.i.e.
tables ,stored procedures etc.
how can
i do it?
Hi All, How to Generate cyclic order using sq l server
you can create this procedure and get ur output
create procedure cyclic_order(@n int)
as
declare @i int
declare @count int
set @count = 0
BEGIN
while (@count < @n)
begin
set @i = 0
while (@i < 10)
begin
print @i
set @i = @i + 1
end
set @count = @count + 1
end
END
Am looking to find the SQL code which is working behind the Database Publishing wizard/Generate Scripts wizard.
Kindly let me know if someone is having info about this.
great tool..!
Thank you Pinal.
This tool solves my Problem.
Great Tool …….
Thanks Pinal :)
Pinal,
Do you know if they released a publishing wizard for sql server 2008? My research indicates that it is designed for 2000 and 2005, but I could be wrong. I haven’t tried it yet on 2008, but I figured I would search first.
Pinal,
As it turns out it seems to be a part of sql server 2008 already. I was expecting a separate install!
Great Tool !
I was doing backup/restore everytime !!
Saved much time.
how to script table data in SQL Server 2005
Thanks, super tool!
Great Tool !
Hi,
Previously i had SQL2005 and i used always database publishing wizard and it worked fine but now i have SQL2008 and i have searched so much for database publishuing wizard for the same but not able to get it anywhere.
please help me how to generate the script so that i can copy the script onto my online database server as i have so many tables and need to upload only 1 table…
please suggest..
Thanks in advance
Sunny
i am a new web signer , i need more understanding about data base, my SQL, data base server and mySqL server. can some tell me more about that ?
thank you
it helped me
Thank you, this helped a lot when I used it to backup a database from a machine that I had no permissions to do a backup “the regular ways”.
Hi,
I appriciate if anyone can provide me the procedure or the way of Automated process for generating sql script which we generaly use by right clicking on DB then all task then go for generating sql script,
I want the Automated process for SQL2005.
Hi all,
Could anyone suggest me a stored procedure to generate a script for crud operation
Thanks for step by step solution
[...] A. In SQL Server 2008 : Right click Database > Tasks > Generate Scripts > In the wizard on Choose Script Option page, set Script Data option to True and complete the wizard.For SQL 2005 or earlier versions, use Database Publishing Wizard. For more details about Database Publishing wizard, please visit the blog http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-data… [...]
Why does everything have to be so difficult in SQL Server? I want to move a large DB from one server to another. In MySQL, I script it out, download the script, upload the script to the new place, and then type in ten or twenty characters to install it EXACTLY as it was. In SQL Server, on the other hand, all the wizards destroy the constraints and relations and there is no easy way to script the new database in (in most cases hosts prevent you from installing a .BAK file). I’ve said it before and I’ll say it again: migrating MySQL DBs is like shipping a boat full of containers from china to los angeles. Migrating a MS SQL database is like dealing with refugees fleeing a war zone. Can someone please write a script that automates the hell i am now going through? Parts of that script exists, but there is still no end-to-end solution that works with large databases! Come on, people, it’s 2010. I guess that’s why all the DB work that matters is happening on MySQL!
Hello Judas,
Please check the article:
http://mssqlonline.blogspot.com/2009/07/sql-server-2008-copy-database-with-data.html
If you not want to include data in scrip the set the “Script Data” option to Flase.
Regards,
Pinal Dave
I did not get the exact answer in the sql authority articles. My staight question is, can we generate the script with data of table in SQL server 2005. I read most of the articles in SQL athority but every article is describing about the SQL 2008.
I tried to generate the script in 2005 but I am unable to see the script data in 2005. I am not sure whether this is available in 2005 or not? Could you please clarify my doubt.
i want restore the database by script with data….
bcoz i want change my db 2000 to 2005.
Hi Pinal,
Thank for the tool..i have an urgent questions
1. the tool creates the contents of the db without the db, i need the db first.
2. i dont need the data in the script.
please help..btw nice to meet you
hey,
on the top of the script type
USE [your database name]
That will import the files over…
Nice one.
How can I get Schema and Data(Only for Selected Tables) using Publishing Wizard.
My task is to script of whole Database where I can, but also need data from selected tables(Not needed from all).
Is it a way I can do it.
Thanx. This really helped :)
hi
i have problem generating script
well i have a database in sql server 2000 and want to use in sql server 2005 or sql server 2008
in mssql 2000 one of the table, im using nchar
now when i use sql publishing wizard error occurs
do i have to change nchar
and if i had to what will be the datatype
Hi Pinal,
Can you an example script for 2005 on a remote server?
Thank you,
Mark
I installed and using graphical tool and command line while executing I am getting this error. I hope u can help me.Thanks in advance.
Error: Could not load file or assembly ‘Microsoft.SqlServer.BatchParser, Version
=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91′ or one of its depe
ndencies. The system cannot find the file specified.
Hi Pinal,
Thanks for the detail. Can you throw some light on how to script index columns and re-create for all tables in a database.
Thanks again in advance
Regards
K. Sankara Raman
[removed phone number]
How to generate script of of a database with data in tables
If you use version 2008, there is an option. Otherwise you need to use import/export wizard to export data
Nice tool. It saves lot of time for me. I wanted to prepare a tool . Then I searched by googling and found.
Thanks
I want to deploy the Database changes around in 100 production databases. It is not possible to compare and deploy. Because all databases are not upto date as per the program. So I want deploy the stored procedures and views in the following way.
if the view/sp exists – Alter the that otherwise create that. I searched for a tool which generate alter & create based on if exist condition. I could not find one. please help me if anybody come to know about such tool.
I’m having a problem with the SqlPubWiz scripting the create views before the create tables which those views reference. Obviously this doesn’t work since those tables are not found until later. I’ve seen other people have problems with this but have yet to find a solution other than manually rearranging the output file.
Does anybody have a solution to this problem? Does anybody know WHY on earth it would create the views before the tables? I can see NO reason to create the views first and every reason to create them last.
Bill
Having the same issue reported by Jim on 9/23/11. The only solution that comes to mind is to script out the tables, views etc seperately and then run the script for views after the tables script etc.
create procedure cyclic_order(@n int)
as
declare @i int
declare @count int
set @count = 0
BEGIN
while (@count < @n)
begin
set @i = 0
while (@i < 10)
begin
print @i
set @i = @i + 1
end
set @count = @count + 1
end
END
When I tried creating a schema for one of my databases, I got the below in the command prompt. Can anyone tell me why, Is there any limitation in the database size or number of objects while generating a schema script this way?
SqlPubWiz Command Line: Error: Operation is not valid du e to the current state of the object
Thanks,
Pradhan
Thanks for this. Two important problems i found using this with SQL Server 2008. But finally i am glad that it worked.
1. The server should be referred as (Local)\, without local if you refer with your machine name, it will not work.
2. The database name after -d is case sensitive, otherwise it will not work.
The following example is for creating a blank database.
“C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2\sqlpubwiz.exe” script -S (Local)\ -U sa -P 1 -targetserver 2008 -d c:\mbwindemodbfile.sql -schemaonly
Thanks.
hi. how can this generated script be use to convert it to mysql?
can anyone help me. thnaks
You need to manually convert it. Becuase some of the datatype names are different in mysql
this tool is given by microsoft, but pinal u give the idea how to use it. great. thank for the help..
hi pinal ,
how to remove timout expire error from sql server 2000
pls give me solution
When do you get this error?
whats for Remore server
Hi Pinal,
Can you please share your inputs on how to generate SQL Database schema and Data scripts, in an automated process, without using any tool or wizard.
I mean from the command prompt….
Please do mail me those details.. it would be of great help…
Your efforts would be greatly appreciated.
Thanks in advance.
Really thank for this help
Hi, Not able to find script data option in sql server 2008, please help
Hi, i have seen one issue during schema generation through GenerateScript wizard that some tables schema is not getting generated.that table is present in database and also GenerateScript wizard is showing that table schema will be created.
[...] SQL SERVER – 2005 – Generate Script with Data from Database – Database Publishing Wizard [...]
Hi All,I want to publish database to godaddy using database publishing wizard but when i connect with server name and windows authentication it does not show all databases.it shows only master,msdb and tempdb.I am totally new to it can somebody help.
[...] Generate Script with Data from Database – Database Publishing Wizard Generating schema script with data is one of the most frequently performed tasks among SQL Server Data Professionals. There are many ways to do the same. In the above article I demonstrated that how we can use the Database Publishing Wizard to accomplish the same. It was new to me at that time but I have not seen much of the adoption of the same still in the industry. [...]
thank you