Cannot Decrypt the encrypted columns from the database backup on local machine - sql

I've a SQL server 2014 running on one of our server. We're in the process of implementing security steps for our databases. I've encrypted a column in one of the table in the database on the server. The issue is when I restore the backup on my local SQL server and run a query to decrypt the column data it gives me null values. On the other end when I decrypt the column data on the main server it works fine. I found a thread on this forum which states to do the following when restoring the encrypted database on different server.
USE [master];
GO
OPEN MASTER KEY DECRYPTION BY PASSWORD = 'StrongPassword';
ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY;
GO
select File_Name
, CONVERT(nvarchar,DECRYPTBYKEY(File_Name))
from [test].[dbo].[Orders_Customer]
I tried doing above still no luck.
Can anybody point me in the right direction? Any help is greatly appreciated.
Thanks

You've opened the Master key (in your example) in the Master DB
Change the first line to use
Use Test;
The Open Master Key statement works in the context of the Current Database. You opened it whilst in Master, but then selected that data from the Test DB.

Related

Azure SQL, Copy most of a database into an existing one (not new one) same server

I know I can clone DB into a new one with
CREATE DATABASE Database1_copy AS COPY OF Database1;
(https://learn.microsoft.com/en-us/azure/sql-database/sql-database-copy-transact-sql)
and this goes flawesly, except in Azure, where db properties are managed by Azure portal, so I am try to find a way to copy most of the schema/resources/data into an EXISTING DB
would be great for:
CLONE DATABASE Database_test AS COPY OF Database_production
[even first approach has been to "clone" the entire db, indeed few tables on destination db should be kept, so better approach would be to CLONE EVERYTHING EXCEPT ('table1','table2'). Actually plan to achieving this by scripting the few tables needed on destination db and overwriting them after import, but bet solution would be the other]
You can do this in several ways:
Through the Azure Portal
Open your database in the Azure Portal(https://portal.azure.com)
In the overview blade of your database select the "copy" option
Fill in the parameters, in which server would you like the copy
Using a sql server client and connecting to the server
Open your SQL Server blade in Azure
Select the "Firewall" option
Click on "Add client IP"
Connect to your database with your connection string and your favorite client, could be SSMS
Execute your sql query to clone the database in the same server
-- Copy a SQL database to the same server
-- Execute on the master database.
-- Start copying.
CREATE DATABASE Database1_copy AS COPY OF Database1;
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-copy-transact-sql
The above SQL statement works perfectly fine as expected in Azure SQL Database.
Important Notes:
Log on to the master database (System Databases) using the
server-level principal login or the login that created the
database you want to copy.
Logins that are not the server-level principal must be members of
the dbmanager role in order to copy databases.
Use updated version of the SQL Server Management Studio

Copy database data from one server to another server

I need to copy all my data from a database on Server "x" to server "z". Both have the same data base name and table names. I need to copy my prod data to my test server without loosing my test servers data.
Try using SSIS to copy your databases. You can also use replication (push or pull or even merge), or you can manually backup a db as a *.bak file then create and restore it onto another server.
https://www.youtube.com/watch?v=pA242aMvz6E

Copy SQL Azure Database to different Server

SQL Azure database supports copying whole database asynchronously with a single command as below:
I have been using to copy database within same server using
CREATE DATABASE [targetdb] AS COPY OF [sourcedb]
But when I try to copy database to a different SQL Azure server:
CREATE DATABASE [targetdb] AS COPY OF [source_sql_azure_server].[sourcedb]
But I get below error:
Cannot open server "source_sql_azure_server" requested by the login. The login failed.
How do I copy?
Although this question is very old, it's the one I came across when trying to find out if I could easily copy an Azure SQL database from one server to another.
It turns out that it's now as simple as navigating to the source database from http://portal.azure.com then clicking copy and choosing the new destination server.
The whole process is explained in more detail in this article:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-copy/
To execute DB copy between 2 different servers you must be connected to the master database of the destination SQL Azure server and have correct permissions.
The exact same login/password must exist on the source server and destination server and the login must have db_owner permissions on the source server and dbmanager on the destination server.
Read more about it here: http://msdn.microsoft.com/en-us/library/ff951624.aspx
I guess there is also another way to achieve another copy in a different region. You can simply do an Asynchronous Replication to a different server and region and make that the primary.
This should solve your 5 year old problem! :)
$dblist = #("db1","db2","db3")
$sourceserver = "azureDBserversource"
$targetserver = "azureDBserverTarget"
$sourceRG = "mySourceRG"
$targetRG = "myTargetRG"
foreach ($db in $dblist) {
New-AzSqlDatabaseCopy -ResourceGroupName $sourceRG `
-ServerName $sourceserver `
-DatabaseName $db `
-CopyResourceGroupName $targetRG `
-CopyServerName $targetserver `
-CopyDatabaseName $db
}

How to make a copy of a database in SQL Server

I can't seem to find any SQL that will clone one database in SQL Server within the same server.
Let's say I have a database called MyDB. I simply want to make a copy of MyDB to MyDb2. I thought that this would work:
BACKUP DATABASE MyDB TO MyDB2;
But I get this error when I try to execute it:
Backup device 'DbTestBack' does not exist. To view existing backup devices, use the sys.backup_devices catalog view. To create a new backup device use either sp_addumpdevice or SQL Server Management Studio.
Does anyone know what the best way to do this is? I want an exact duplicate of the original including security permissions.
A simple way is taking a back up copy of current DB and restoring it.
You Can do this in single step with a simple script
backup database MyDB
to disk='D:\MyDB.bak';
restore database MyDB2
from disk='D:\MyDB.bak'
WITH move 'MyDB_Data' to 'D:\MyDB2_Data.mdf',
move 'MyDB_log' to 'D:\MyDB2_Data.ldf';
GO
Note: I made an assumption on your current data file and log file name (MyDB_Data, MyDB_log), you need to check them and make correct
DBAtools is your friend here.
Use Copy-DbaDatabase
ie.
Copy-DbaDatabase -Source SRV1 -Destination SRV1 -Database myDB -BackupRestore -SharedPath \\<<your temporary server location such as c:\temp>>

Restore a database using SQL Server 2005

I have backed up a database into a file using SQL Server from my old server.
Now i would like to restore that file into a new database on my new server.
I created a DB with the same name , I am getting an error saying :
"The Backup set holds a backup of the database other than the existing '*****' database"
Any thoughts?
Thanks
Add a WITH REPLACE option to your restore:
Specifies that SQL Server should
create the specified database and its
related files even if another database
already exists with the same name
Drop the new database - it's sitting in the way of the one you want to restore.
THen when you try to restore your old database, select the file to restore from, and the name will magically appear in the "to database" destination field in SSMS.
When you restore a database from backup, you are creating a new database on the SQL instance. If a database by that name is already present on that SQL instance, you will get an error--unless you select the option to overwrite any existing database, in which case the old database will be wiped out and replaced.
I was having the same issue, but even when putting WITH REPLACE, the error occurred. I had an empty database with the same name as the back up, but the problem was my .trn file I was using to backup from had two backup sets and I was choosing to restore from the full database AND the transaction log. I chose only the Full Database and it worked.