Share via

Encrypted search

David Chase 681 Reputation points
2020-10-27T17:49:06.117+00:00

In moving to an SQL Server 2016 Always Encrypted database, we have some search stored procedures that find records based on a full or partial match on name columns that will be encrypted. Is there any workaround or options we can consider? A sample SELECT statement is below and FirstName and LastName columns will be encrypted.

    SELECT P.FirstName, 
            P.LastName, 
            P.CaregiverLogin, 
            O.Organization, 
            O.OrgUserName
      FROM dbo.tblPatients AS P INNER JOIN
           dbo.tblOrganization AS O ON P.OrgID = O.OrgID
     WHERE (CASE WHEN @SearchType = 0 AND P.CaregiverLogin = @SearchText THEN 'T'
                 WHEN @SearchType = 1 AND (P.LastName = @SearchText OR P.FirstName = @SearchText) THEN 'T'
                 ELSE 'F'
                 END = 'T')
    ORDER BY LastName, FirstName;
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

EchoLiu-MSFT 14,626 Reputation points
2020-10-28T09:09:29.177+00:00

Hi @David Chase ,

Could you share with us any search stored procedures you have tried?
This thread may be useful to you:SQL Encrypted Columns in WHERE Clause

If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.

Regards
Echo


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2020-10-27T23:04:03.65+00:00

    In this example, you are making point lookups on equality. In that case, you can use the code above if you use deterministic encryption, so that the a value always results in the same encrypted value. (Which is a weakness from a security standpoint.)

    If you are using LIKE, this is possible if you set up Always Encrypted with Enclaves, which I have told you about a few times already. (And also warned you that it is complex.)

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.