Amazon RDS SQL 服务器:如何检测是不是RDS?

Amazon RDS SQL Server: how to detect if it is RDS?

我正在尝试将我的数据库移植到 RDS。由于限制需要做一些更改。

是否可以检测到当前数据库在 RDS 中的内部脚本(存储过程等)?

更新。 我在我的函数中使用这种方式进行测试:

if CHARINDEX(N'EC2AMAZ',(cast(serverproperty('ServerName') as nvarchar(256))))>0 
   return 1 
else 
   return 0

它不是万无一失的,并且取决于现有的 rdsadmin 数据库(AWS 似乎总是创建),但我使用这个:

SELECT CASE WHEN db_id('rdsadmin') IS NULL THEN 0 ELSE 1 END AS RDS_DATABASE;

我在我的函数中使用这种方式:

if CHARINDEX(N'EC2AMAZ',(cast(serverproperty('ServerName') as nvarchar(256))))>0 
   return 1 
else 
   return 0

现在可以了。

我喜欢@xiani 的方法,并决定稍微增强它(如果 SQL 服务器的 "normal" 实例有一个 [rdsadmin] 数据库)。

DECLARE @IsAmazonRDS BIT = 0;

--1st test: does the [rdsadmin] database exist?
IF DB_ID('rdsadmin') IS NOT NULL
BEGIN
    BEGIN TRY
        --2nd test: If this is an Amazon RDS instance, we should not able to access the database "model" under the current security context.
        DECLARE @FileCount INT;
        SELECT @FileCount = COUNT(*) FROM model.sys.database_files;
    END TRY
    BEGIN CATCH
        SET @IsAmazonRDS = 1;
        --Comment/uncomment to verify the flag is set.
        --SELECT 'Inside Catch';
    END CATCH
END

这可以根据您确定的任何标准进行第三次、第四次等额外检查来补充。自己决定要走多远。