SQL SERVER – 2005 – Generate Script with Data from Database – Database Publishing Wizard

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

 

About these ads

70 thoughts on “SQL SERVER – 2005 – Generate Script with Data from Database – Database Publishing Wizard

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

  2. 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…..

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

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

  5. 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~

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

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

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

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

  10. 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!

  11. 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…

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

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

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

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

  16. 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 ?

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

  18. Pingback: SQL SERVER – Several Readers Questions and Readers Answers Journey to SQL Authority with Pinal Dave

  19. 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!

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

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

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

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

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

  25. 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]

  26. Nice tool. It saves lot of time for me. I wanted to prepare a tool . Then I searched by googling and found.

    Thanks

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

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

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

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

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

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

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

  34. Pingback: SQL SERVER – Copy Data from One Table to Another Table – SQL in Sixty Seconds #031 – Video « SQL Server Journey with SQL Authority

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

  36. Pingback: SQL SERVER – Weekly Series – Memory Lane – #003 « SQL Server Journey with SQL Authority

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s