All Products
Search
Document Center

ApsaraDB RDS:Stored procedures

Last Updated:May 14, 2026

This topic lists the stored procedures available for ApsaraDB RDS for SQL Server instances running SQL Server 2012 or later.

Usage

The commands in this topic are for SSMS and use GO as the batch command separator. If you run stored procedure commands in DMS, do not include the GO keyword. Otherwise, an error will occur.

Update database statistics

T-SQL command

sp_rds_update_db_stats

Description

Updates database statistics flexibly and efficiently. You can configure parameters such as sampling rate, degree of parallelism, timeout, and modification threshold.

Usage

-- This is a comprehensive example that includes multiple parameters.
-- Update database statistics for the test_db database. Set the sampling rate to 50%, the degree of parallelism to 4, the timeout to 7,200 seconds, and the modification threshold to 3.
EXEC sp_rds_update_db_stats        
    @db_name = 'test_db',          -- Database name (required)
    @sample_percent = 50,          -- Sampling rate (optional)
    @max_dop = 4,                  -- Degree of parallelism (optional, not supported in SQL Server 2012 and earlier)
    @timeout_seconds = 7200,       -- Timeout in seconds (optional)
    @modification_threshold = 3;   -- Modification threshold (optional)
Note

If you specify only the @db_name parameter or are using SQL Server 2008, the system executes the sp_updatestats stored procedure by default. For more information, see the Microsoft official documentation.

Parameter

Required

Description

@db_name

Yes

Specifies the database for which to update statistics. Example:

-- Specify only the database name. The update follows the logic of Microsoft's sp_updatestats.
EXEC sp_rds_update_db_stats @db_name = 'test_db';

@sample_percent

No

Specifies the percentage of the table to sample for statistics. The data type is float, and the value must be in the range of [0, 100].

If omitted, the system uses the default sampling rate. For more information, see the Microsoft official documentation. Example:

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

@max_dop

No

Specifies the degree of parallelism (DOP). The data type is int. The default value is 0, which means the system uses its default setting. The maximum value cannot exceed the RDS instance core count. This parameter is not supported in SQL Server 2012 and earlier. Example:

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

@timeout_seconds

No

Specifies the timeout for the statistics update, in seconds (s). The default value is 3600 seconds (1 hour). Example:

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

@modification_threshold

No

Specifies the modification threshold as a percentage for updating statistics. The data type is int, and the default value is 0.

  • Default policy (when the value is 0): Alibaba Cloud applies its recommended best practices:

    • table row count < 500: 30%

    • 500 ≤ table row count ≤ 1,000,000: 20%

    • table row count > 1,000,000: 5%

  • Manual input: Specify the threshold value. The value must be in the range of [0, 100]. The modification percentage is calculated using the formula: (number of modified rows * 100) / total number of rows. To trigger a statistics update, the calculated percentage must be greater than or equal to the threshold you set.

    -- For a table with 10,000 rows, to trigger a statistics 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 editions

Basic Edition and High-availability Edition

Description

Creates a copy of a database within the same instance.

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

  • You cannot use this operation on ApsaraDB MyBase for SQL Server instances.

Usage

USE db
GO
EXEC sp_rds_copy_database 'db','db_copy'
GO
  • The first parameter is the name of the source database.

  • The second parameter is the name of the destination database.

Bring a database online

T-SQL command

sp_rds_set_db_online

Supported editions

RDS Basic Edition and RDS High-availability Edition

Description

After you set a database to OFFLINE, you cannot use the ALTER DATABASE statement to bring it back online. Use this stored procedure instead.

Usage

USE master
GO
EXEC sp_rds_set_db_online 'db'
GO

The name of the database to bring online.

Global database permissions

T-SQL command

sp_rds_set_all_db_privileges

Supported instance editions

Basic Edition, High-availability Edition

Description

Grants permissions to a user on all or multiple user databases.

Note

The current user's permissions on the target databases must be greater than or equal to the permissions being granted.

Usage

sp_rds_set_all_db_privileges 'user','db_owner','db1,db2...'
  • The first parameter specifies the user who receives the permissions.

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

  • The third parameter specifies one or more target databases, separated by commas. This parameter is optional. Omitting it grants permissions on all user databases.

Delete a database

T-SQL command

sp_rds_drop_database

Supported editions

High-availability Edition

Note
  • This stored procedure is not supported on Basic Edition instances. Use the DROP DATABASE db command instead.

  • Execute this command using a privileged account while connected to a database other than the target database. Ensure that the account has the necessary permissions on the target database. For more information, see Modify the permissions of an account.

Description

Deletes a database from an instance. This operation removes all associated objects. For High-availability Edition instances, this operation also removes the database mirror and terminates all connections to the database.

Usage

USE master
GO
EXEC sp_rds_drop_database 'db'
GO

The parameter specifies the name of the database to delete.

Configure change tracking

T-SQL command

sp_rds_change_tracking

Supported instance series

High-availability Edition

Description

Enables or disables change tracking for a database.

Usage

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

  • The second parameter specifies whether to enable or disable change tracking. Valid values:

    • 1: Enables change tracking.

    • 0: Disables change tracking.

Enable change data capture

T-SQL command

sp_rds_cdc_enable_db

Supported instance series

High-availability Edition and Cluster Edition

Description

Enables change data capture (CDC) for a database.

Usage

USE db
GO
-- Enable CDC for a database.
EXEC sp_rds_cdc_enable_db
GO
-- Enable CDC for a table.
EXEC sys.sp_cdc_enable_table
    @source_schema = '<schema name>',
    @source_name = '<table name>',
    @role_name = '<CDC role name>'

Disable change data capture

T-SQL command

sp_rds_cdc_disable_db

Supported instance series

High-availability Edition, Cluster Edition

Description

Disables change data capture (CDC) for a database.

Usage

USE db
GO
-- Disable change data capture (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 = '<capture_instance_name>'
    
-- Get the capture instance name for a specific table.
SELECT capture_instance
FROM cdc.change_tables
WHERE source_schema = '<schema_name>'
    AND source_name = '<table_name>'

RDS instance parameters

T-SQL command

sp_rds_configure

Supported instance families

Basic Edition and High-availability Edition

Description

Configure the following instance parameters. For primary/standby instances, settings are synchronized automatically. For more details, see Microsoft documentation.

Parameter

Description

Example

fill factor (%)

Specifies the fill factor percentage for an index page.

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

max worker threads

Specifies the maximum number of worker threads to execute queries and process requests in parallel.

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 a query.

EXEC sp_rds_configure 'max degree of parallelism', 4;

min server memory (MB)

Specifies the minimum amount of memory, in megabytes (MB), used by the RDS instance.

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

max server memory (MB)

Specifies the maximum amount of memory, in megabytes (MB), used by the RDS instance.

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

blocked process threshold (s)

Specifies the threshold, in seconds, for reporting blocked processes.

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

nested triggers

Enables or disables nested triggers. Valid values:

  • 0: Disable.

  • 1: Enable.

EXEC sp_rds_configure 'nested triggers', 1;

Ad Hoc Distributed Queries

Enables or disables Ad Hoc Distributed Queries. Valid values:

  • 0: Disable.

  • 1: Enable.

EXEC sp_rds_configure 'Ad Hoc Distributed Queries', 1;

clr enabled

Enables or disables running user-created Common Language Runtime (CLR) assemblies. Valid values:

  • 0: Disable.

  • 1: Enable.

EXEC sp_rds_configure 'clr enabled', 1;

default full-text language

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

  • 0: Default language. The operating system's locale determines the default.

  • 1033: English.

  • 2052: Simplified Chinese.

Click to view all values

Value

Language

Description

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)

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

default language

Specifies the default language for the instance. Common values include:

  • 0: English (U.S.).

  • 30: Simplified Chinese.

Click to view all values

Value

Language

Description

0

English

English (U.S.)

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

British English

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)

EXEC sp_rds_configure 'default language', 30;

max text repl size (B)

Specifies the maximum size of text data, in bytes, in a replication process.

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 plan cache optimization for ad hoc workloads. Valid values:

  • 0: Disable.

  • 1: Enable.

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

query governor cost limit

Specifies the upper limit on the estimated cost for a query. A value of 0 disables the query governor.

EXEC sp_rds_configure 'query governor cost limit', 10;

recovery interval (min)

Specifies the maximum time, in minutes, that it should take to recover a database.

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

remote login timeout (s)

Specifies the timeout period, in seconds, for a remote login attempt.

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

remote query timeout (s)

Specifies the timeout period, in seconds, for a remote query.

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

query wait (s)

Specifies the time, in seconds, that a query waits for memory resources before timing out.

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

min memory per query (KB)

Specifies the minimum amount of memory, in kilobytes (KB), allocated for a query to execute.

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

in-doubt xact resolution

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

  • 0 (Default for non-Cluster Edition RDS instances): No automatic resolution. The system does not automatically resolve in-doubt transactions and requires manual intervention.

  • 1: Assume commit. If the system lacks sufficient information to resolve an in-doubt transaction, it commits the transaction by default.

  • 2 (Default for RDS Cluster Edition instances): Assume rollback. If the system encounters an in-doubt transaction, it rolls back the transaction by default.

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

Usage

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

  • The second parameter is its value.

Add a linked server

T-SQL command

sp_rds_add_linked_server

Supported instances

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

  • Instance type: general-purpose and dedicated. shared instances are not supported.

  • Billing method: subscription and pay-as-you-go. serverless instances are not supported.

Description

This command adds a linked server to an instance and supports distributed transactions. A linked server created on the primary instance automatically synchronizes to the secondary instance. You do not need to reconfigure the linked server after a primary/secondary switchover. However, modifications to existing linked servers on the primary instance are not synchronized to the secondary instance. For more information, see Automatic or manual primary/secondary switchover.

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 instance. Format: IP,Port
@user_name sysname = N'ay15' , --The username for the destination SQL Server instance.
@password nvarchar(128) = N'******', --The password for the destination user.
@source_user_name sysname = N'test', --The username on the source instance.
@source_password nvarchar(128) = N'******', --The password for the source user.
--Server options for the linked server in XML format. This example sets permissions for data access, rpc, and rpc out.
@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

Configure a trace flag

T-SQL command

sp_rds_dbcc_trace

Supported editions

Basic Edition, High-availability Edition

Description

This stored procedure sets a trace flag for an RDS instance. It currently supports only a subset of trace flags. For primary/secondary instances, the platform automatically synchronizes this setting.

Usage

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

  • The second parameter specifies whether to enable or disable the trace flag. Valid values:

    • 1: Enables the trace flag.

    • 0: Off.

Rename a database

T-SQL command

sp_rds_modify_db_name

Supported instance editions

Basic Edition, High-availability Edition, and Cluster Edition

Description

Renames a database. Ensure the connecting account has the required permissions on the target database and that the database is in the online state.

For High-availability Edition or Cluster Edition instances, renaming a database automatically rebuilds the primary/secondary relationship. This process includes a backup and restore. If the database is large, ensure the instance has sufficient available storage. You can scale up the instance if needed.

Usage

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

  • The second parameter is the new database name.

Grant server roles

T-SQL command

sp_rds_set_server_role

Supported editions

Basic Edition

Description

Grants a server role to a login. The available roles are setupadmin and processadmin. To create accounts with other permissions or to learn more about account permissions, see Create an account with SA permissions and Account permissions.

Usage

EXEC sp_rds_set_server_role @login_name='test_login',@server_role='setupadmin'
  • Specifies the login name.

  • Specifies the role name. Valid values are setupadmin and processadmin.

FAQ

Q: Why do I get the Cannot use KILL to kill your own process. error when running the EXEC sp_rds_drop_database 'dbtest'; command with a standard account?

A: Run the command using a privileged account from a command window connected to a different database. Ensure that the account has the required permissions on the target database. For more information, see Modify account permissions.