Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 5123

Uninstall SQL 2014 x86, install SQL 2014 x64, help with "CREATE DATABASE FOR ATTACH" and retain current compatibility level

$
0
0

I had a previous problem which was answered (https://social.msdn.microsoft.com/Forums/sqlserver/en-US/9434168c-c31f-4efd-b07b-f4e3752962a4/current-memory-allocation-is-below-available-memory-on-server-where-sql-server-2014-is-installed?forum=sqldatabaseengine) but I need help with forming a script which will retain the existing compatibility_level for each of the databases in the resulting query.

Is there a way to generate a script which will give me the results of the following query...

SELECT 'CREATE DATABASE ' + quotename(d.name) +
		' ON (NAME = ' + quotename(mf.name, '''') +
		', FILENAME = ' + quotename(mf.physical_name, '''') +
       ') FOR ATTACH'
FROM   sys.databases d
JOIN   sys.master_files mf ON d.database_id = mf.database_id
WHERE  d.database_id > 4
  AND  mf.file_id  = 1

Example from above:

CREATE DATABASE [Prod45] ON (NAME = 'Prod45_Data', FILENAME = 'C:\MSSSDATABASE\DATA\Prod45_Data.MDF') FOR ATTACH

AND somehow add the current 'compatibility_level' value for each database returned by the above SELECT 'CREATE DATABASE' 

SELECT name, compatibility_level, database_id, create_date
FROM sys.databases ;
GO 

Example result:

namecompatibility_leveldatabase_idcreate_date
Prod4580 62016-09-21 15:21:13.757


I could manually change the compatibility_level for each database but I'd like to know if there is a way to automate this.

Thank you everyone!






Viewing all articles
Browse latest Browse all 5123

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>