Modify only Database name
ALTER DATABASE DB_OldName
MODIFY NAME = DB_NewName
GO
Alter Logical Names (only)
ALTER DATABASE [DB_Name] MODIFY FILE (NAME=N'DB_DataFileNameOld', NEWNAME= N’DB_DataFileNameNew')
GO
ALTER DATABASE [DB_Name] MODIFY FILE (NAME=N'DB_LogFileNameOld_log', NEWNAME= N’DB_LogFileNameNew_log')
GO
Restore Database backup with new Database file names.
RESTORE DATABASE [DatabaseName]
FROM DISK = 'P:\DatabasBackup.bak'
WITH
MOVE 'DB_DataLogicalName' TO
'P:\MSSQL.SHAREPOINT\Data\DatabaseName_NewName.mdb',
MOVE 'DB_LogLogicalName_Log' TO
'P:\MSSQL.SHAREPOINT\Log\DatabaseName_NewName_Log.ldf',
RECOVERY, REPLACE, STATS = 10;
Testing a Login to Database in PowerShell
When you run the following script you shouldn't get any error and script terminates gracefully.
$dataSource = "SQLDEV\SharePoint"$user = "SharePointUser"$pwd = "Password"$database = "DatabaseName"$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"$connection = New-Object System.Data.SqlClient.SqlConnection$connection.ConnectionString = $connectionString$connection.Open()
Note: If you get an error as "A network-related error or instance-specific error occurred..." then you are connecting to an incorrect SQL Server instance or the ports may be blocking your communication.
Run query after opening connection in above script.
$query = "SELECT * FROM aspnet_Users"
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
Fix Database login during Database migrations
When you migrate a database with an existing login which you want to map your database user to, you could run the following. First parameter of ‘SharePointDBUser′ is the user in the database, the second one is login existing in SQL Server securities to be mapped to.
EXEC
sp_change_users_login
'update_one'
,
'SharePointDBUser'
,
'SharePointDBUser'