All Products
Search
Document Center

ApsaraDB RDS:Stored procedures

Last Updated:Aug 22, 2025

This topic describes the stored procedures supported by ApsaraDB RDS for SQL Server 2012 and later versions.

Usage notes

The commands in this topic are intended for execution in SQL Server Management Studio (SSMS). These commands include GO as a batch separator. If you plan to run stored procedure commands in Data Management Service (DMS), do not include the GO keyword. Otherwise, an error occurs.

Update database statistics

T-SQL command

sp_rds_update_db_stats

Description

Updates database statistics information in a flexible and efficient manner. You can configure multiple dimensions, such as sample rate, degree of parallelism, timeout period, and threshold percentage.

Usage

-- The following example shows how to use multiple parameters.
-- Update the statistics information for the test_db database. Set the sample rate to 50%, the degree of parallelism to 4, the timeout period to 7200 seconds, and the modification threshold to 3.
EXEC sp_rds_update_db_stats        
    @db_name = 'test_db',          -- Database name (required)
    @sample_percent = 50,          -- Sample rate (optional)
    @max_dop = 4,                  -- Degree of parallelism (optional, not supported for SQL Server 2012 and earlier)
    @timeout_seconds = 7200,       -- Timeout period in seconds (optional)
    @modification_threshold = 3;   -- Modification threshold (optional)
Note

If you pass only the @db_name parameter, or if the SQL Server database version is 2008, the system runs sp_updatestats by default. For more information, see the official Microsoft documentation.

Parameter

Required

Description

@db_name

Yes

The name of the destination database for which you want to update statistics information. The following example shows how to use this parameter:

-- Set only the database name to follow the statistics information update logic of Microsoft sp_updatestats.
EXEC sp_rds_update_db_stats @db_name = 'test_db';

@sample_percent

No

The sampling percentage for statistics information. The data type is float. The value ranges from [0, 100].

If you do not specify this parameter, the system default sample rate is used. For more information, see the official Microsoft documentation. The following example shows how to use this parameter:

-- Set the sample rate to 30%.
EXEC sp_rds_update_db_stats 
    @db_name = 'test_db',
    @sample_percent = 30;

@max_dop

No

The degree of parallelism (DOP). The data type is int. The default value is 0, which indicates that the system default setting is used. The maximum value cannot exceed the number of vCores of the RDS instance type. This parameter is not supported for SQL Server 2012 and earlier versions. The following example shows how to use this parameter:

-- Set the maximum degree of parallelism to 4.
EXEC sp_rds_update_db_stats 
    @db_name = 'test_db',
    @max_dop = 4;

@timeout_seconds

No

The timeout period for the statistics information update operation, in seconds. The default value is 3600 seconds (1 hour). The following example shows how to use this parameter:

-- Set the timeout period to 7200 seconds (2 hours).
EXEC sp_rds_update_db_stats 
 @db_name = 'test_db',
 @timeout_seconds = 7200;

@modification_threshold

No

The modification threshold percentage for updating statistics information. The data type is int. The default value is 0.

  • Default policy (when the value is 0): Alibaba Cloud's recommended best practice policy is used:

    • Number of table rows < 500: 30%

    • 500 ≤ Number of rows ≤ 1,000,000: 20%

    • Number of rows > 1,000,000: 5%

  • Manual parameter passing: You must calculate the value in advance. The value ranges from [0, 100]. The formula is: Value = Number of modified rows × 100 / Total number of rows. To trigger a statistics information update, the calculation result must be less than or equal to the specified value.

    -- If a table has 10,000 rows and you want to trigger a statistics information update when 100 rows are modified, set the threshold to 1.
    EXEC sp_rds_update_db_stats 
        @db_name = 'test_db',
        @modification_threshold = 1;

Copy a database within an instance

T-SQL command

sp_rds_copy_database

Supported instance editions

Basic Edition, High-availability Edition

Description

Copies a database within an instance.

Note
  • The available storage space of the instance must be at least 1.3 times the size of the current database.

  • This procedure is not supported for MyBase dedicated clusters for SQL Server.

Usage

USE db
GO
EXEC sp_rds_copy_database 'db','db_copy'
GO
  • The first parameter specifies the database to copy.

  • The second parameter specifies the destination database.

Set a database online

T-SQL command

sp_rds_set_db_online

Supported instance editions

Basic Edition, High-availability Edition

Description

After you set a database to OFFLINE, you cannot use ALTER DATABASE to set it back to ONLINE. You can use this stored procedure to set the database to ONLINE.

Usage

USE master
GO
EXEC sp_rds_set_db_online 'db'
GO

The parameter specifies the database to set to ONLINE.

Grant global database permissions

T-SQL command

sp_rds_set_all_db_privileges

Supported instance editions

Basic Edition, High-availability Edition

Description

Grants a user permissions on all or multiple user databases.

Note

When you grant permissions, the permissions of the current user on the database must be greater than or equal to the permissions that you want to grant.

Usage

sp_rds_set_all_db_privileges 'user','db_owner','db1,db2...'
  • The first parameter specifies the user to whom you want to grant permissions.

  • The second parameter specifies the database role to grant to the user.

  • The third parameter specifies the databases. You can specify one or more databases separated by commas. This parameter is optional. If you do not specify this parameter, permissions are granted on all user databases.

Delete a database

T-SQL command

sp_rds_drop_database

Supported instance editions

High-availability Edition

Note
  • Basic Edition instances do not support this stored procedure. You can use DROP DATABASE db instead.

  • In a command window that is not connected to the destination database, you can run this command using a privileged account. Make sure that the account has the required permissions on the destination database. For more information, see Modify the permissions of an account.

Description

Deletes a database from an instance. When the database is deleted, its associated objects are also removed. For High-availability Edition instances, the database image is automatically removed, and connections to the database are terminated.

Usage

USE db
GO
EXEC sp_rds_drop_database 'db'
GO

The parameter specifies the database to delete.

Set change tracking

T-SQL command

sp_rds_change_tracking

Supported instance editions

High-availability Edition

Description

Sets the change tracking flag for a database.

Usage

USE db
GO
EXEC sp_rds_change_tracking 'db',1
GO
  • The first parameter specifies the database name.

  • The second parameter specifies whether to enable the feature.

    • 1: Enable.

    • 0: Disable.

Enable change data capture for a database

T-SQL command

sp_rds_cdc_enable_db

Supported instance editions

High-availability Edition, Cluster Edition

Description

Enables change data capture (CDC) for a database.

Usage

USE db
GO
-- Enable CDC at the database level.
EXEC sp_rds_cdc_enable_db
GO
-- Enable CDC for a specific table.
EXEC sys.sp_cdc_enable_table
    @source_schema = '<schema_name>',
    @source_name = '<table_name>',
    @role_name = '<CDC_role_name>'

Disable change data capture for a database

T-SQL command

sp_rds_cdc_disable_db

Supported instance editions

High-availability Edition, Cluster Edition

Description

Disables CDC for a database.

Usage

USE db
GO
-- Disable CDC at the database level.
EXEC sp_rds_cdc_disable_db
GO
-- Disable CDC for a specific table.
EXEC sys.sp_cdc_disable_table
    @source_schema = '<schema_name>',
    @source_name = '<table_name>',
    @capture_instance = '<CDC_capture_instance_name>'
    
-- Method to get the CDC capture instance name for a specific table.
SELECT capture_instance
FROM cdc.change_tables
WHERE source_schema = '<schema_name>'
    AND source_name = '<table_name>'

Configure instance parameters

T-SQL command

sp_rds_configure

Supported instance editions

Basic Edition, High-availability Edition

Description

Configures instance parameters. If the instance has a primary and a secondary node, the parameters are automatically synchronized. The following parameters are supported. For more information about how to use these parameters, see the official Microsoft documentation.

Parameter

Description

Example

fill factor (%)

Specifies the fill factor percentage for index pages.

EXEC sp_rds_configure 'fill factor (%)', 90;

max worker threads

Specifies the maximum number of worker threads for parallel query execution and request processing.

EXEC sp_rds_configure 'max worker threads', 100;

cost threshold for parallelism

Specifies the cost threshold for parallelism.

EXEC sp_rds_configure 'cost threshold for parallelism', 30;

max degree of parallelism

Specifies the maximum degree of parallelism for queries.

EXEC sp_rds_configure 'max degree of parallelism', 4;

min server memory (MB)

Specifies the minimum amount of memory used by the SQL Server instance.

EXEC sp_rds_configure 'min server memory (MB)', 1024;

max server memory (MB)

Specifies the maximum amount of memory used by the SQL Server instance.

EXEC sp_rds_configure 'max server memory (MB)', 4096;

blocked process threshold (s)

Specifies the threshold for blocked processes.

EXEC sp_rds_configure 'blocked process threshold (s)', 20;

nested triggers

Specifies whether to enable nested triggers. Valid values:

  • 0: Disable.

  • 1: Enable.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'nested triggers', 1;

Ad Hoc Distributed Queries

Specifies whether to enable ad hoc distributed queries. Valid values:

  • 0: Disable.

  • 1: Enable.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'Ad Hoc Distributed Queries', 1;

clr enabled

Specifies whether to enable Common Language Runtime (CLR). Valid values:

  • 0: Disable.

  • 1: Enable.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'clr enabled', 1;

default full-text language

Specifies the default full-text search language. Common values:

  • 0: Default language. The default language is determined by the operating system locale.

  • 1033: English.

  • 2052: Simplified Chinese.

Click to view all values

Value

Language (English)

Language (Chinese)

0

Neutral

Neutral

1025

Arabic

Arabic

1026

Bulgarian

Bulgarian

1027

Catalan

Catalan

1028

Traditional Chinese

Traditional Chinese

1029

Czech

Czech

1030

Danish

Danish

1031

German

German

1032

Greek

Greek

1033

English

English

1036

French

French

1037

Hebrew

Hebrew

1039

Icelandic

Icelandic

1040

Italian

Italian

1041

Japanese

Japanese

1042

Korean

Korean

1043

Dutch

Dutch

1044

Bokmål

Norwegian (Bokmål)

1045

Polish

Polish

1046

Brazilian

Brazilian Portuguese

1048

Romanian

Romanian

1049

Russian

Russian

1050

Croatian

Croatian

1051

Slovak

Slovak

1053

Swedish

Swedish

1054

Thai

Thai

1055

Turkish

Turkish

1056

Urdu

Urdu

1057

Indonesian

Indonesian

1058

Ukrainian

Ukrainian

1060

Slovenian

Slovenian

1062

Latvian

Latvian

1063

Lithuanian

Lithuanian

1066

Vietnamese

Vietnamese

1081

Hindi

Hindi

1086

Malay - Malaysia

Malay (Malaysia)

1093

Bengali (India)

Bengali (India)

1094

Punjabi

Punjabi

1095

Gujarati

Gujarati

1097

Tamil

Tamil

1098

Telugu

Telugu

1099

Kannada

Kannada

1100

Malayalam

Malayalam

1102

Marathi

Marathi

2052

Simplified Chinese

Simplified Chinese

2057

British English

British English

2070

Portuguese

Portuguese

2074

Serbian (Latin)

Serbian (Latin)

3076

Chinese (Hong Kong SAR, PRC)

Chinese (Hong Kong SAR)

3082

Spanish

Spanish

3098

Serbian (Cyrillic)

Serbian (Cyrillic)

4100

Chinese (Singapore)

Chinese (Singapore)

5124

Chinese (Macao SAR)

Chinese (Macao SAR)

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'default full-text language', 2052;

default language

Specifies the default language. Common values:

  • 0: English (US).

  • 30: Simplified Chinese.

Click to view all values

Value

Language (English)

Language (Chinese)

0

English

English (US)

1

German

German

2

French

French

3

Japanese

Japanese

4

Danish

Danish

5

Spanish

Spanish

6

Italian

Italian

7

Dutch

Dutch

8

Norwegian

Norwegian

9

Portuguese

Portuguese

10

Finnish

Finnish

11

Swedish

Swedish

12

Czech

Czech

13

Hungarian

Hungarian

14

Polish

Polish

15

Romanian

Romanian

16

Croatian

Croatian

17

Slovak

Slovak

18

Slovenian

Slovenian

19

Greek

Greek

20

Bulgarian

Bulgarian

21

Russian

Russian

22

Turkish

Turkish

23

British English

English (UK)

24

Estonian

Estonian

25

Latvian

Latvian

26

Lithuanian

Lithuanian

27

Brazilian Portuguese

Brazilian Portuguese

28

Traditional Chinese

Traditional Chinese

29

Korean

Korean

30

Simplified Chinese

Simplified Chinese

31

Arabic

Arabic

32

Thai

Thai

33

Norwegian (Bokmål)

Norwegian (Bokmål)

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'default language', 30;

max text repl size (B)

Specifies the maximum size of text in a replication process.

Note

This parameter is not supported for instances created before May 2023.

Set the maximum text replication size to 100 MB:

EXEC sp_rds_configure 'max text repl size (B)', 104857600;

optimize for ad hoc workloads

Specifies whether to enable the dynamic management view that is optimized for ad hoc workloads. Valid values:

  • 0: Disable.

  • 1: Enable.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'optimize for ad hoc workloads', 1;

query governor cost limit

Specifies the maximum run time for a query, in seconds. A value of 0 indicates no time limit.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'query governor cost limit', 10;

recovery interval (min)

Specifies the recovery interval.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'recovery interval (min)', 60;

remote login timeout (s)

Specifies the timeout period for a remote logon.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'remote login timeout (s)', 30;

remote query timeout (s)

Specifies the timeout period for a remote query.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'remote query timeout (s)', 60;

query wait (s)

Specifies the time that a query waits for resources.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'query wait (s)', 5;

min memory per query (KB)

Specifies the minimum amount of memory allocated for each query.

Note

This parameter is not supported for instances created before May 2023.

EXEC sp_rds_configure 'min memory per query (KB)', 1024;

in-doubt xact resolution

Specifies how the system handles in-doubt distributed transactions. Valid values:

  • 0 (default for non-cluster instances): Disables automatic resolution. The system does not automatically handle in-doubt distributed transactions. You must handle them manually.

  • 1: Assumes automatic commit. If the system does not have enough information to resolve an in-doubt transaction, it commits the transaction by default.

  • 2 (default for cluster instances): Assumes automatic rollback. If the system encounters an in-doubt transaction, it rolls back the transaction by default.

EXEC sp_configure 'in-doubt xact resolution', 2;

Usage

EXEC sp_rds_configure '<parameter>',<value>
  • The first parameter specifies the instance configuration parameter to set.

  • The second parameter specifies the value of the instance parameter.

Add a linked server

T-SQL command

sp_rds_add_linked_server

Supported instances

  • Instance editions: Cluster Edition and High-availability Edition. Basic Edition is not supported.

  • Instance types: General-purpose and Dedicated. Shared instance types are not supported.

  • Billing methods: Subscription and Pay-as-you-go. Serverless instances are not supported.

Description

Adds a linked server to an instance. Distributed transactions are supported. The linked server is automatically created on the primary and secondary nodes of the instance. You do not need to reconfigure it after a high availability (HA) switchover. For more information, see Automatic or manual primary/secondary failover.

Usage

DECLARE
@linked_server_name sysname = N'yangzhao_slb', -- The name of the linked server.
@data_source sysname = N'****.sqlserver.rds.aliyuncs.com,3888', -- The IP address and port number of the destination SQL Server in the IP,Port format.
@user_name sysname = N'ay15' , -- The logon username for the destination SQL Server.
@password nvarchar(128) = N'******', -- The password for the logon username of the destination SQL Server.
@source_user_name sysname = N'test', -- The logon username used to create the linked server on the current SQL Server instance.
@source_password nvarchar(128) = N'******', -- The password for the logon username used to create the linked server on the current SQL Server instance.
-- Settings for the linked server, passed in XML format. This example shows the settings for data access, RPC, and RPC out permissions.
@link_server_options xml
= N'
      <rds_linked_server>
        <config option="data access">true</config>
        <config option="rpc">true</config>
        <config option="rpc out">true</config>
      </rds_linked_server>
'

EXEC sp_rds_add_linked_server
@linked_server_name,
@data_source,
@user_name,
@password,
@source_user_name,
@source_password,
@link_server_options

Set a trace flag

T-SQL command

sp_rds_dbcc_trace

Supported instance editions

Basic Edition, High-availability Edition

Description

Sets a trace flag for the instance. Only specific trace flags are supported. If the instance has a primary and a secondary node, the trace flags are automatically synchronized.

Usage

EXEC sp_rds_dbcc_trace '1222',1/0
  • The first parameter specifies the trace flag.

  • The second parameter specifies whether to turn the flag on or off.

    • 1: On.

    • 0: Off.

Change the name of a database

T-SQL command

sp_rds_modify_db_name

Supported instance editions

Basic Edition, High-availability Edition, Cluster Edition

Description

Changes the name of a database. Make sure that the account used to connect to the database has the required permissions on the destination database and that the destination database is online.

For High-availability Edition and Cluster Edition instances, the primary/secondary relationship is automatically rebuilt after the name is changed. During this process, a backup and restoration are performed. If the database is large, check the available storage space of the instance. If the space is insufficient, you can scale out the instance.

Usage

USE master
GO
EXEC sp_rds_modify_db_name 'db','new_db'
GO
  • The first parameter specifies the original database name.

  • The second parameter specifies the new database name.

Grant server-level roles

T-SQL command

sp_rds_set_server_role

Supported instance editions

Basic Edition

Description

Grants server-level roles to a logon user. The supported roles are setupadmin and processadmin. To create other permissions and learn more about account permissions, see Create an account with SA permissions and Account permission list.

Usage

EXEC sp_rds_set_server_role @login_name='test_login',@server_role='setupadmin'
  • The first parameter specifies the username.

  • The second parameter specifies the role name. The supported roles are setupadmin and processadmin.

Manage db_owner permissions for the tempdb database

T-SQL command

sp_rds_manage_tempdb_user

Supported instance editions

Basic Edition, High-availability Edition, Cluster Edition

Description

Grants or revokes the db_owner permission on the tempdb database for a specified account.

Note
  • Because tempdb is a temporary system database, tempdb is automatically rebuilt and initialized each time the instance restarts. As a result, any granted permissions are lost and revert to their default state. You must therefore grant the permissions again after each restart. For instances that use a primary/secondary architecture, such as RDS High-availability Edition and RDS Cluster Edition, you must grant permissions to the primary and secondary nodes separately.

  • For related operations, see Reclaim temporary space in tempdb.

Usage

EXEC sp_rds_manage_tempdb_user 
    @login_name = '<login_name>', 
    @action = '<action>';
  • The first parameter specifies the username.

  • The second parameter specifies the action type. Supported values are create (grant permission) and drop (revoke permission).

FAQ

Q: Why do I receive the Cannot use KILL to kill your own process. error after I run the EXEC sp_rds_drop_database 'dbtest'; command using an account with standard permissions?

A: You can run this command using a privileged account in a command window that is not connected to the destination database. Make sure that the account has the required permissions on the destination database. For more information, see Modify the permissions of an account.