加密SQL Server中的现有列数据

时间:2018-12-18 21:14:38

标签: sql sql-server

我想加密SQL数据库列中的现有数据,例如。 “名字”,“姓氏”

我尝试了以下查询来加密单行:

DECLARE @FirstName VARCHAR(100) = (SELECT FirstName FROM db_table WHERE Id = 518)
DECLARE @LastName VARCHAR(100) = (SELECT LastName FROM db_table WHERE Id = 518)

OPEN SYMMETRIC KEY DemoKey  DECRYPTION BY CERTIFICATE MyCert; 

UPDATE db_table 
SET FirstName = EncryptByKey(Key_GUID('DemoKey'), @FirstName),
    LastName = EncryptByKey(Key_GUID('DemoKey'), @LastName)
WHERE Id = 518

CLOSE SYMMETRIC KEY DemoKey;

如何加密列中的多行?

1 个答案:

答案 0 :(得分:2)

只需删除或修改where子句。就这样

OPEN SYMMETRIC KEY DemoKey  DECRYPTION BY CERTIFICATE MyCert; 

UPDATE db_table 
SET FirstName = EncryptByKey(Key_GUID('DemoKey'), FirstName),
    LastName = EncryptByKey(Key_GUID('DemoKey'), LastName)