当我 运行 查询时,我启动了 RDS SQL 服务器(SQL 服务器网络版),它抛出权限被拒绝的错误

I launched RDS SQL Server (SQL Server Web Edition) when I ran the query and It is throwing permission denied error

我运行下面的查询和错误是

The SELECT permission was denied on the object 'sysschedules', database 'msdb', schema 'dbo'.

SQL服务器网络版
引擎版本
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];

遗憾的是,目前 RDS 不支持此功能。看起来 RDS 没有给你访问这些系统表的权限:

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

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

您需要从查询中删除对这些表的连接,这会为您提供以下内容 - 可以针对 SQL 服务器实例的 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];