I have a SQL Server 2008 database that I want to copy and create a new database (with the a different name) on the server with. I am not concerned with maintaining data, the new database can be created with no data for starters. What I am looking to do is the following:
- Create the new database maintaining structure of old database
- Set the name of the new database
- Change all varchar and char datatypes to nvarchar and nchar
- Change all text datatypes to nvarchar(MAX)
As an aside, I have 2 more questions which are not part of my task but would like to consider the following:
- How can I upgrade the sql server database to sql server 2012
- Is there any preparatory work I need to carry out on the database to ensure I can easily upgrade it?
This really is a combination of multiple questions.
QUESTIONS 1 AND 2
The simplest backup command would be:
Now to restore this as a different database, you need to know the file names because it will try to put the same files in the same place. So if you run the following:
You should see output that contains the names and paths of the data and log files. When you construct your restore, you’ll need to use these, but replace the paths with the name of the new database, e.g.:
You’ll have to replace
dbnameandnewnamewith your actual database names, and alsoC:\some folderandC:\path_from_sp_helpfile_output\with your actual paths. I can’t get more specific in my answer unless I know what those are.Here is a full repro:
Partial results:
Now I run the backup:
Of course if the clone target (in this case
DB-B) already exists, you’ll want to drop it:Now this restore will run successfully, giving you a copy of DB-A renamed as DB-B:
QUESTIONS 3 AND 4
Refactoring is a major pain, especially if some of these columns participate in constraints. You can build a very basic script this way, but you will need something much more industrial strength to deal with all of these variables. This assumes all the columns are nullable and don’t participate in constraints.
You can use the PRINT output to verify the first 8K of the script, and when you think it looks good, uncomment the
EXEC.You’ll want to rebuild all of your indexes once you’re done.
That said, scripting the database as Tony suggested (or using a tool like Red Gate’s SQL Compare – or one of its many alternatives – against an empty database) is probably going to much easier, particularly if some of these columns participate in constraints – which may need to be dropped and re-created in order to change the types.
QUESTIONS 5 AND 6
You can’t upgrade just a single database on a 2008 instance. You either upgrade in place or you set up a new instance (as Tony described) and then migrate your database (preferably using backup / restore – many folks will tell you to detach / attach but that is far less safe). Preparatory work you should do include:
And after the upgrade you’ll want to: