从另一个数据库使用SQL证书 [英] Use SQL Certificate from another database
问题描述
我有数据库 A
在这个数据库中有一个证书和一个表。我想在数据库 A
中的表中插入加密的值。存储过程在数据库 B
中,并且理想情况下将进行如下调用:
I have database A
in this database there is a certificate and a table. I want to insert an encrypted value into a table which is in database A
. The stored procedure is in database B
and would ideally make a call like this:
EncryptByCert(Cert_ID('CertifiacteName'), 'SecretData')
因为数据库 B
在数据库中找不到证书 A
This will always fail, because database B
cannot find the certificate in database A
无需移动存储过程或证书,如何在数据库 B
中使用证书在数据库 A $ c $中创建加密值c>
Without moving the stored procedure or the certificate, how can I create an encrypted value in database B
using a certificate in database A
?
推荐答案
您可以将EncryptByCert调用包装在数据库A中的函数中,如下所示:
You could wrap the EncryptByCert call in a function that lives in database A like this:
CREATE FUNCTION dbo.MyEncrypt(@Data VARCHAR(4000))
RETURNS TABLE
AS
RETURN
SELECT ENCRYPTBYCERT(CERT_ID('MyCert'),@Data) EncryptedData;
要在连接到数据库B时将数据插入到数据库A中的表中,请使用以下命令:
To insert data into your table in database A while connected to database B use something like this:
INSERT INTO DatabaseA.dbo.MyTable(id,EncryptedData)
VALUES(42, (SELECT EncryptedData FROM DatabaseA.dbo.MyEncrypt('testvalue')) );
这篇关于从另一个数据库使用SQL证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!