Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Amazon RDS SQL Server:如何检测是否为RDS?_Sql Server_Amazon Rds - Fatal编程技术网

Sql server Amazon RDS SQL Server:如何检测是否为RDS?

Sql server Amazon RDS SQL Server:如何检测是否为RDS?,sql-server,amazon-rds,Sql Server,Amazon Rds,我正在尝试将我的数据库移植到RDS。由于限制,需要进行一些更改 是否可以在脚本内部(存储过程等)检测当前数据库是否在RDS中 Upd。 我在我的函数中使用这种方式进行测试: if CHARINDEX(N'EC2AMAZ',(cast(serverproperty('ServerName') as nvarchar(256))))>0 return 1 else return 0 它不是万无一失的,取决于现有的rdsadmin数据库(AWS似乎总是创建该数据库),但我使用

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

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

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

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 Server的“普通”实例有一个
[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