Encrypted columns in SQL Server database

Nicky 116 Reputation points
2021-02-17T12:54:46.057+00:00

Hi

I have to retrieve data from database (DB1) which has encrypted columns.

I have written a stored proc (as my query has lot of temp tables) to extract data from DB1 and to insert it to tables in database DB2.

In SSIS package or direct insert from SQL Server I get this error:

Operand type clash: nvarchar(255) encrypted with (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'ColoumnEncryptionKey', column_encryption_key_database_name = 'DB1') is incompatible with nvarchar

I have tried using ADO.NET connection but no success.
I would really appreciate any help here.

I have installed certificate on my machine.

Thanks!
Nikz

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,894 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,467 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,563 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sean Gallardy - MSFT 1,886 Reputation points Microsoft Employee
    2021-02-17T16:52:15.497+00:00

    Stored proc select rows and insert to destination database.

    That's never going to work because that's not how Always Encrypted works. In order for Always Encrypted to be used, the client driver is what does the decryption and encryption processes, in your scenario the result set is never returned to the client driver but is attempted to be directly inserted into a different table, this will fail as it has.

    I edited the public Docs article on this a few weeks ago to better help those understand this, please reference it here under 'Remarks':

    Encryption and decryption occurs via the client driver. This means that some actions that occur only server-side will not work when using Always Encrypted. These actions include (but are not limited to):

    Copying data from one columng to another via an UPDATE, BULK INSERT(T-SQL), SELECT INTO, INSERT..SELECT.
    Triggers, temporal tables, sparse columns, full-text, in-memory OLTP, and Change Data Capture (CDC).