简体   繁体   English

EXECUTE AS和OPENQUERY链接服务器

[英]EXECUTE AS and OPENQUERY linked server

I have a problem. 我有个问题。 When I use the statement execute as and then want to use that principle to open data from a linked server, the openquery query is not run with the User2 principle, but with the login (User1)that runs the query. 当我使用语句execute as,然后要使用该原理从链接服务器打开数据时,openquery查询不是以User2原理运行的,而是以运行查询的登录名(User1)运行的。 I deliberately gave User1 the wrong credentials for the linked server. 我故意为User1提供了链接服务器的错误凭据。 The following query gives an error 以下查询给出错误

Msg 18456, Level 14, State 1, Line 1
Login failed for user 'LinkedServerUser1'.

Query: 查询:

USE Database1
EXECUTE AS LOGIN = 'User2'

IF EXISTS(
    SELECT *
    FROM
    OPENQUERY(LinkedServer1,'
    SELECT
    name
    ,state
    ,state_desc
    FROM sys.databases
    WHERE name = ''DatabaseLinked1'' AND state = 0
    '
    )
)
BEGIN
    PRINT 'test'
END

REVERT

In need this to work because normally this query will be run by the SQL Server Agent, and I don't want to grant the SQL Server Agent User access to the linked server. 需要这项工作,因为通常此查询将由SQL Server代理运行,并且我不想授予SQL Server代理用户对链接服务器的访问权限。

Ok I already found the solution. 好的,我已经找到了解决方案。 Adding 新增中

GO

after EXECUTE AS, solves the problem... 在EXECUTE AS之后,解决了问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM