[英]Connect to the same database, but as a another user, without hardcoding a connection string
Can I use OPENDATASOURCE
(or another mechanism) from a Stored Procedure to connect to the same database as a different user? 是否可以使用存储过程中的
OPENDATASOURCE
(或其他机制)以其他用户身份连接到同一数据库? If so, how? 如果是这样,怎么办?
The database is meant to be deployed to several customers, and replicated by them as many times as they want to, etc. For this reason, I CANNOT HARDCODE the database server's name or the database's name. 该数据库打算部署到多个客户,并由他们进行多次复制, 以此类推 。因此,我无法对数据库服务器的名称或数据库名称进行硬编码 。
(I tried using OPENDATASOURCE
, but it only accepts hardcoded connection strings.) (我尝试使用
OPENDATASOURCE
,但它仅接受硬编码的连接字符串。)
Might EXECUTE AS
work in your situation? 可以在您所处的环境中
EXECUTE AS
吗? http://msdn.microsoft.com/en-us/library/ms181362.aspx http://msdn.microsoft.com/en-us/library/ms181362.aspx
You can set up a Linked Server to connect to the remote server using the login's current security context (or other options as it applies to your situation). 您可以设置链接服务器以使用登录名的当前安全上下文(或其他适用于您情况的选项)连接到远程服务器。
From your stored procedure, you could access it with something like SELECT * FROM mylinkedservername.mylinkedserverdatabase.dbo.mytable
从存储过程中,您可以使用
SELECT * FROM mylinkedservername.mylinkedserverdatabase.dbo.mytable
类的方式访问它
But you say you want to connect to the same database but using a different login? 但是您说要连接到相同的数据库但使用不同的登录名吗? You're looking for impersonation.
您正在寻找模仿。 Perhaps you can do this making a Linked Server that references itself, I haven't tried it.
也许您可以这样做,使链接服务器引用自己,但我还没有尝试过。 Search Microsoft Help documentation for how to set it up normally and test if it does what you're looking to do.
在Microsoft帮助文档中搜索如何正常设置它,并测试它是否符合您的期望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.