{"id":2037,"date":"2012-05-31T20:34:30","date_gmt":"2012-05-31T20:34:30","guid":{"rendered":"http:\/\/craftydba.com\/?p=2037"},"modified":"2017-10-11T19:56:38","modified_gmt":"2017-10-11T19:56:38","slug":"upgrading-older-databases","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=2037","title":{"rendered":"Upgrading Older Databases"},"content":{"rendered":"<p>A new version of SQL Server is released every few years.  This year was no different with the <a href=\"http:\/\/blogs.msdn.com\/b\/mssmallbiz\/archive\/2012\/01\/24\/sql-server-2012-virtual-launch-event-information-resources-and-much-more.aspx\">launch<\/a> of SQL Server 2012 in March.  It is important to migrate older, unsupported databases to a current version.  I am going to examine 3 different techniques to upgrade the [PUBS] sample database from SQL Server 2000 to 2008 R2.  <\/p>\n<p>The first two techniques require the database files to be copied from the older server to the new one.  Use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb522469.aspx\">ALTER DATABASE<\/a> command to kick off the users and set the database off-line.  Then copy the files to the target server.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"kick off users &#038; set database off-line\">\r\n-- Kick off users, roll back current work\r\nALTER DATABASE [WILDLIFE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;\r\nGO\r\n\r\n-- Set the database to off-line\r\nALTER DATABASE [WILDLIFE] SET OFFLINE\r\nGO\r\n<\/pre>\n<\/p>\n<p>For simplicity sake, the MSSQL directory on the C drive will hold both the data and log files.  The first technique uses the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa258257(v=sql.80).aspx\">CREATE DATABASE<\/a> command with the FOR ATTACH clause.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"create database - for attach\">\r\n-- Create database - upgrades it\r\nCREATE DATABASE [PUBS] ON\r\n(FILENAME = 'C:\\MSSQL\\Data\\pubs.mdf'),\r\n(FILENAME = 'C:\\MSSQL\\LOG\\pubs.ldf')\r\nFOR ATTACH;\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/create-database-for-attach.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/create-database-for-attach.jpg\" alt=\"\" title=\"create-database-for-attach\" width=\"538\" height=\"340\" class=\"aligncenter size-full wp-image-2044\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/create-database-for-attach.jpg 538w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/create-database-for-attach-300x189.jpg 300w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/a><\/p>\n<p>The second technique uses the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms179877.aspx\">sp_attach_db<\/a> command to add the database to the default instance.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"system stored procedure - sp_attach_db()\">\r\n-- Attach database - upgrades it\r\nEXEC sp_attach_db @dbname = N'PUBS',\r\n    @filename1 = N'C:\\MSSQL\\Data\\pubs.mdf',\r\n    @filename2 = N'C:\\MSSQL\\LOG\\pubs.ldf';\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/sp-attach-db.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/sp-attach-db.jpg\" alt=\"\" title=\"sp-attach-db\" width=\"548\" height=\"335\" class=\"aligncenter size-full wp-image-2048\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/sp-attach-db.jpg 548w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/sp-attach-db-300x183.jpg 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/a><\/p>\n<p>The third and last technique depends upon a backup from the SQL Server 2000 server.  I am using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173778.aspx\">RESTORE FILELISTONLY<\/a> to get details on the backup.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"restore filelist only\">\r\n-- Get file list\r\nRESTORE FILELISTONLY \r\n   FROM DISK = 'C:\\MSSQL\\BACKUP\\pubs.bak';\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-file-list-only.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-file-list-only.jpg\" alt=\"\" title=\"restore-file-list-only\" width=\"1398\" height=\"197\" class=\"aligncenter size-full wp-image-2047\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-file-list-only.jpg 1398w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-file-list-only-300x42.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-file-list-only-1024x144.jpg 1024w\" sizes=\"auto, (max-width: 1398px) 100vw, 1398px\" \/><\/a><\/p>\n<p>The restore technique requires a blank database to exist.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"create empty database\">\r\n-- Define empty db \r\nCREATE DATABASE [PUBS]\r\nON PRIMARY\r\n( NAME = 'PubsData',\r\n    FILENAME = 'C:\\MSSQL\\DATA\\pubs.mdf',\r\n    SIZE = 5MB,\r\n    FILEGROWTH = 5MB)\r\nLOG ON\r\n ( NAME = 'PubsLog',\r\n    FILENAME = 'C:\\MSSQL\\LOG\\pubs.ldf',\r\n    SIZE = 2MB,\r\n    FILEGROWTH = 2MB)\r\nGO\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186858.aspx\">RESTORE DATABASE<\/a> command will overwrite the empty database and upgrade the internal structures.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"restore database over empty one\">\r\n-- Restore from backup\r\nRESTORE DATABASE [PUBS] FROM  \r\n    DISK = N'C:\\MSSQL\\BACKUP\\pubs.bak' WITH FILE = 1,  \r\n    MOVE N'pubs' TO N'C:\\MSSQL\\DATA\\PUBS.mdf',  \r\n    MOVE N'pubs_log' TO N'C:\\MSSQL\\LOG\\PUBS.ldf',  \r\n    NOUNLOAD, REPLACE,  STATS = 10\r\nGO\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-backup-over-database.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-backup-over-database.jpg\" alt=\"\" title=\"restore-backup-over-database\" width=\"580\" height=\"486\" class=\"aligncenter size-full wp-image-2046\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-backup-over-database.jpg 580w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/restore-backup-over-database-300x251.jpg 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>With all techniques, make sure the capatibility level is set to SQL Server 2008 R2 (100).  It will be at SQL Server 2000 (80) after upgrading the database.  <\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/pubs-database-compatibility.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/pubs-database-compatibility.jpg\" alt=\"\" title=\"pubs-database-compatibility\" width=\"709\" height=\"642\" class=\"aligncenter size-full wp-image-2045\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/pubs-database-compatibility.jpg 709w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/05\/pubs-database-compatibility-300x271.jpg 300w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/a><\/p>\n<p>The system databases should never be moved from an older server to a newer one.  If you need objects such as jobs from msdb, script them out and install them on the target server by executing the script.  In summary, keep up to date with server versions so that you are not left in the dust when support is discontinued with your software.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A new version of SQL Server is released every few years. This year was no different with the launch of SQL Server 2012 in March. It is important to migrate older, unsupported databases to a current version. I am going to examine 3 different techniques to upgrade the [PUBS] sample database from SQL Server 2000 to 2008 R2. The first two techniques require the database files to be copied from the older server to the new one. Use the ALTER DATABASE command to kick off the users and set the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[50,12,15,386,430,383,28,29],"class_list":["post-2037","post","type-post","status-publish","format-standard","hentry","category-db-admin","tag-create-database","tag-free-code","tag-john-f-miner-iii","tag-restore-database","tag-restore-filelistonly","tag-sp_attach_db","tag-sql-server","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2037"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2037\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}