我在运行查询时启动了 RDS SQL Server(SQL Server Web 版)并抛出了权限被拒绝错误

Posted

技术标签:

【中文标题】我在运行查询时启动了 RDS SQL Server(SQL Server Web 版)并抛出了权限被拒绝错误【英文标题】:I launched RDS SQL Server (SQL Server Web Edition) when I ran the query and It is throwing permission denied error 【发布时间】:2021-09-03 08:21:51 【问题描述】:

我在查询下方运行,错误是

对象“sysschedules”、数据库“msdb”、架构“dbo”的 SELECT 权限被拒绝。

SQL Server 网络版 引擎版本 15.00.4073.23.v1

如何解决?

SELECT [sJOB].[job_id] AS [JobID],
       [sJOB].[name] AS [JobName],
       [sDBP].[name] AS [JobOwner],
       [sCAT].[name] AS [JobCategory],
       [sJOB].[description] AS [JobDescription],
       CASE [sJOB].[enabled] WHEN 1 THEN 'TRUE' WHEN 0 THEN 'FALSE' END AS [IsEnabled],
       [sJOB].[date_created] AS [JobCreatedOn],
       [sJOB].[date_modified] AS [JobLastModifiedOn],
       [sSVR].[name] AS [OriginatingServerName],
       [sJSTP].[step_id] AS [JobStartStepNo],
       [sJSTP].[step_name] AS [JobStartStepName],
       CASE WHEN [sSCH].[schedule_uid] IS NULL THEN 'FALSE' ELSE 'TRUE' END AS [IsScheduled],
       [sSCH].[schedule_uid] AS [JobScheduleID],
       [sSCH].[name] AS [JobScheduleName],
       CASE [sJOB].[delete_level]
            WHEN 0 THEN 'Never'
            WHEN 1 THEN 'On Success'
            WHEN 2 THEN 'On Failure'
            WHEN 3 THEN 'On Completion'
       END AS [JobDeletionCriterion]
FROM [msdb].[dbo].[sysjobs] [sJOB]
     LEFT JOIN [msdb].[sys].[servers] [sSVR] ON [sJOB].[originating_server_id] = [sSVR].[server_id]
     LEFT JOIN [msdb].[dbo].[syscategories] [sCAT] ON [sJOB].[category_id] = [sCAT].[category_id]
     LEFT JOIN [msdb].[dbo].[sysjobsteps] [sJSTP] ON [sJOB].[job_id] = [sJSTP].[job_id]
                                                 AND [sJOB].[start_step_id] = [sJSTP].[step_id]
     LEFT JOIN [msdb].[sys].[syslogins] [sDBP] ON [sJOB].[owner_sid] = [sDBP].[sid]
     LEFT JOIN [msdb].[dbo].[sysjobschedules] [sJOBSCH] ON [sJOB].[job_id] = [sJOBSCH].[job_id]
     LEFT JOIN [msdb].[dbo].[sysschedules] [sSCH] ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id]
ORDER BY [JobName];

【问题讨论】:

我强烈建议您格式化查询 需要养成在写作中善用使用空格和换行符的习惯,包括代码.它使难以阅读的文本 Far 变得更易读。至于错误,它实际上是在告诉您这里的问题。你不明白所说的错误怎么办? 即使我有管理员权限,我也无法授予权限 您如何确定您拥有“管理员权限”?您的意思是您是系统管理员角色的成员吗?此查询是否返回任何行? use msdb; select * from sys.fn_my_permissions(null, 'database') where permission_name = 'take ownership' 它没有给出任何结果,只有0行受到影响 【参考方案1】:

很遗憾,到目前为止,RDS 不支持此功能。看起来 RDS 不允许您查询这些系统表:

[msdb].[dbo].[sysjobsteps] [msdb].[dbo].[sysjobschedules] [msdb].[dbo].[sysschedules]

对应的 AWS 论坛帖子:https://forums.aws.amazon.com/thread.jspa?threadID=220807

您需要从查询中删除这些表的连接,这会为您提供以下信息 - 可以针对 SQL Server 实例的 RDS 执行:

SELECT [sJOB].[job_id] AS [JobID],
       [sJOB].[name] AS [JobName],
       [sDBP].[name] AS [JobOwner],
       [sCAT].[name] AS [JobCategory],
       [sJOB].[description] AS [JobDescription],
       CASE [sJOB].[enabled] WHEN 1 THEN 'TRUE' WHEN 0 THEN 'FALSE' END AS [IsEnabled],
       [sJOB].[date_created] AS [JobCreatedOn],
       [sJOB].[date_modified] AS [JobLastModifiedOn],
       [sSVR].[name] AS [OriginatingServerName],
       CASE [sJOB].[delete_level]
            WHEN 0 THEN 'Never'
            WHEN 1 THEN 'On Success'
            WHEN 2 THEN 'On Failure'
            WHEN 3 THEN 'On Completion'
       END AS [JobDeletionCriterion]
FROM [msdb].[dbo].[sysjobs] [sJOB]
     LEFT JOIN [msdb].[sys].[servers] [sSVR] ON [sJOB].[originating_server_id] = [sSVR].[server_id]
     LEFT JOIN [msdb].[dbo].[syscategories] [sCAT] ON [sJOB].[category_id] = [sCAT].[category_id]
     LEFT JOIN [msdb].[sys].[syslogins] [sDBP] ON [sJOB].[owner_sid] = [sDBP].[sid]
ORDER BY [JobName];

【讨论】:

以上是关于我在运行查询时启动了 RDS SQL Server(SQL Server Web 版)并抛出了权限被拒绝错误的主要内容,如果未能解决你的问题,请参考以下文章

我在sql server 里面 运行一条SQL查询语句, 有时候要卡几钞钟才有结果,有时候一下就出结果了。

查询在 SQL Server 上运行,但不使用 PHP

如何从 AWS Lambda .Net Core 应用程序 API 连接到 AWS RDS SQL Server?

在sql server中参数化全文查询

在 SQL Server CE 中解析查询时出错

通过 C# 查询时 SQL Server 2005 区分大小写