All Products
Search
Document Center

ApsaraDB RDS:Stored procedures

Last Updated:Mar 28, 2026

ApsaraDB RDS for SQL Server provides custom stored procedures that replace native SQL Server operations requiring elevated privileges. This topic covers the stored procedures supported in SQL Server 2012 and later.

Usage notes

The examples in this topic are written for SQL Server Management Studio (SSMS), which uses GO as a batch separator. If you run stored procedure commands in Data Management Service (DMS), remove the GO keyword — otherwise an error occurs.

Available stored procedures

ProcedureTask
sp_rds_update_db_statsUpdate database statistics
sp_rds_copy_databaseCopy a database within an instance
sp_rds_set_db_onlineSet a database online
sp_rds_set_all_db_privilegesGrant global database permissions
sp_rds_drop_databaseDelete a database
sp_rds_change_trackingSet change tracking
sp_rds_cdc_enable_dbEnable change data capture for a database
sp_rds_cdc_disable_dbDisable change data capture for a database
sp_rds_configureConfigure instance parameters
sp_rds_add_linked_serverAdd a linked server
sp_rds_dbcc_traceSet a trace flag
sp_rds_modify_db_nameChange the name of a database
sp_rds_set_server_roleGrant server-level roles

Update database statistics

T-SQL command: sp_rds_update_db_stats

Updates statistics for a database with configurable sampling percentage, degree of parallelism (DOP), timeout period, and modification threshold.

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

Syntax

EXEC sp_rds_update_db_stats
    @db_name = 'test_db',          -- Database name (required)
    @sample_percent = 50,          -- Sampling percentage (optional)
    @max_dop = 4,                  -- Degree of parallelism (optional)
    @timeout_seconds = 7200,       -- Timeout in seconds (optional)
    @modification_threshold = 3;   -- Modification threshold (optional)

Parameters

ParameterRequiredTypeDefaultDescription
@db_nameYesThe name of the database to update statistics for.
@sample_percentNofloatSystem defaultSampling percentage for statistics. Range: [0, 100]. If omitted, the system default sampling rate applies. See the Microsoft documentation for details.
@max_dopNoint0 (system default)Maximum degree of parallelism. Cannot exceed the number of vCores of the RDS instance type. Not supported for SQL Server 2012 and earlier.
@timeout_secondsNoint3600Timeout for the statistics update operation, in seconds.
@modification_thresholdNoint0Modification threshold percentage. See below for details.

`@modification_threshold` policy

When set to 0 (default), the following Alibaba Cloud-recommended thresholds apply:

Row countThreshold
< 50030%
500 – 1,000,00020%
> 1,000,0005%

To set a custom threshold, calculate: Value = (modified rows × 100) / total rows. A statistics update is triggered when the calculation result is less than or equal to the specified value.

Examples

Set only the database name to follow the sp_updatestats logic:

EXEC sp_rds_update_db_stats @db_name = 'test_db';

Set the sampling percentage to 30%:

EXEC sp_rds_update_db_stats
    @db_name = 'test_db',
    @sample_percent = 30;

Set the maximum degree of parallelism to 4:

EXEC sp_rds_update_db_stats
    @db_name = 'test_db',
    @max_dop = 4;

Set the timeout to 7,200 seconds (2 hours):

EXEC sp_rds_update_db_stats
    @db_name = 'test_db',
    @timeout_seconds = 7200;

Set a custom modification threshold: if a table has 10,000 rows and you want to trigger a statistics update after 100 rows change, set the threshold to 1 (100 × 100 / 10,000 = 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, High-availability Edition

Copies a database to a new database within the same instance.

The instance must have at least 1.3 times the current database size in available storage.
Not supported for MyBase dedicated clusters for SQL Server.

Syntax

USE db
GO
EXEC sp_rds_copy_database 'db', 'db_copy'
GO

The first parameter specifies the source database. The second parameter specifies the destination database name.

Set a database online

T-SQL command: sp_rds_set_db_online

Supported editions: Basic Edition, High-availability Edition

After a database is set to OFFLINE, ALTER DATABASE cannot restore it to ONLINE. Use this stored procedure instead.

Native SQL ServerApsaraDB RDS for SQL Server
ALTER DATABASE db_name SET ONLINE;EXEC sp_rds_set_db_online 'db'

Syntax

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 editions: Basic Edition, High-availability Edition

Grants a user permissions on all or selected user databases.

The account performing the grant must have permissions equal to or greater than the permissions being granted.

Syntax

sp_rds_set_all_db_privileges 'user', 'db_owner', 'db1,db2...'
  • Parameter 1: The user to grant permissions to.

  • Parameter 2: The database role to assign.

  • Parameter 3: (Optional) A comma-separated list of databases. If omitted, permissions are granted on all user databases.

Delete a database

T-SQL command: sp_rds_drop_database

Supported editions: High-availability Edition

Basic Edition instances do not support this stored procedure. Use DROP DATABASE db instead.
Run this command with a privileged account in a command window that is not connected to the target database. Make sure the account has the required permissions. See Modify the permissions of an account for details.

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

Syntax

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 editions: High-availability Edition

Sets the change tracking flag for a database.

Syntax

USE db
GO
EXEC sp_rds_change_tracking 'db', 1
GO
  • Parameter 1: The database name.

  • Parameter 2: 1 to enable, 0 to disable.

Enable change data capture for a database

T-SQL command: sp_rds_cdc_enable_db

Supported editions: High-availability Edition, Cluster Edition

Enables change data capture (CDC) for a database.

Syntax

Enable CDC at the database level:

USE db
GO
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 editions: High-availability Edition, Cluster Edition

Disables CDC for a database.

Syntax

Disable CDC at the database level:

USE db
GO
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>'

To get the CDC capture instance name for a 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 families: Basic Edition, High-availability Edition

Sets SQL Server instance parameters. For instances with primary and secondary nodes, parameters are automatically synchronized to both nodes. For parameter descriptions, see the Microsoft documentation on server configuration options.

Syntax

EXEC sp_rds_configure '<parameter>', <value>

Supported parameters

ParameterDescriptionExample
fill factor (%)Fill factor percentage for index pages.EXEC sp_rds_configure 'fill factor (%)', 90;
max worker threadsMaximum worker threads for parallel query execution and request processing.EXEC sp_rds_configure 'max worker threads', 100;
cost threshold for parallelismCost threshold for parallelism.EXEC sp_rds_configure 'cost threshold for parallelism', 30;
max degree of parallelismMaximum degree of parallelism for queries.EXEC sp_rds_configure 'max degree of parallelism', 4;
min server memory (MB)Minimum memory used by the SQL Server instance.EXEC sp_rds_configure 'min server memory (MB)', 1024;
max server memory (MB)Maximum memory used by the SQL Server instance.EXEC sp_rds_configure 'max server memory (MB)', 4096;
blocked process threshold (s)Threshold for blocked processes.EXEC sp_rds_configure 'blocked process threshold (s)', 20;
nested triggersEnable or disable nested triggers. 0: disabled. 1: enabled.EXEC sp_rds_configure 'nested triggers', 1;
Ad Hoc Distributed QueriesEnable or disable ad hoc distributed queries. 0: disabled. 1: enabled.EXEC sp_rds_configure 'Ad Hoc Distributed Queries', 1;
clr enabledEnable or disable Common Language Runtime (CLR). 0: disabled. 1: enabled.EXEC sp_rds_configure 'clr enabled', 1;
default full-text languageDefault language for full-text search. Common values: 0 (OS locale), 1033 (English), 2052 (Simplified Chinese). See the full list below.EXEC sp_rds_configure 'default full-text language', 2052;
default languageDefault language for the instance. Common values: 0 (English, US), 30 (Simplified Chinese). See the full list below.EXEC sp_rds_configure 'default language', 30;
max text repl size (B)Maximum size of text data in replication. For example, set to 100 MB:EXEC sp_rds_configure 'max text repl size (B)', 104857600;
optimize for ad hoc workloadsEnable or disable the dynamic management view optimized for ad hoc workloads. 0: disabled. 1: enabled.EXEC sp_rds_configure 'optimize for ad hoc workloads', 1;
query governor cost limitMaximum run time for a query, in seconds. 0 means no limit.EXEC sp_rds_configure 'query governor cost limit', 10;
recovery interval (min)Recovery interval.EXEC sp_rds_configure 'recovery interval (min)', 60;
remote login timeout (s)Timeout for remote login.EXEC sp_rds_configure 'remote login timeout (s)', 30;
remote query timeout (s)Timeout for remote queries.EXEC sp_rds_configure 'remote query timeout (s)', 60;
query wait (s)Time a query waits for resources.EXEC sp_rds_configure 'query wait (s)', 5;
min memory per query (KB)Minimum memory allocated per query.EXEC sp_rds_configure 'min memory per query (KB)', 1024;
in-doubt xact resolutionHow the system handles in-doubt distributed transactions. 0: manual resolution (default for non-cluster instances). 1: auto-commit. 2: auto-rollback (default for cluster instances).EXEC sp_configure 'in-doubt xact resolution', 2;

Full list of `default full-text language` values

<details> <summary>Click to expand</summary>

ValueLanguage
0Neutral
1025Arabic
1026Bulgarian
1027Catalan
1028Traditional Chinese
1029Czech
1030Danish
1031German
1032Greek
1033English
1036French
1037Hebrew
1039Icelandic
1040Italian
1041Japanese
1042Korean
1043Dutch
1044Bokmål
1045Polish
1046Brazilian
1048Romanian
1049Russian
1050Croatian
1051Slovak
1053Swedish
1054Thai
1055Turkish
1056Urdu
1057Indonesian
1058Ukrainian
1060Slovenian
1062Latvian
1063Lithuanian
1066Vietnamese
1081Hindi
1086Malay - Malaysia
1093Bengali (India)
1094Punjabi
1095Gujarati
1097Tamil
1098Telugu
1099Kannada
1100Malayalam
1102Marathi
2052Simplified Chinese
2057British English
2070Portuguese
2074Serbian (Latin)
3076Chinese (Hong Kong SAR, PRC)
3082Spanish
3098Serbian (Cyrillic)
4100Chinese (Singapore)
5124Chinese (Macao SAR)

</details>

Full list of `default language` values

<details> <summary>Click to expand</summary>

ValueLanguage
0English (US)
1German
2French
3Japanese
4Danish
5Spanish
6Italian
7Dutch
8Norwegian
9Portuguese
10Finnish
11Swedish
12Czech
13Hungarian
14Polish
15Romanian
16Croatian
17Slovak
18Slovenian
19Greek
20Bulgarian
21Russian
22Turkish
23British English
24Estonian
25Latvian
26Lithuanian
27Brazilian Portuguese
28Traditional Chinese
29Korean
30Simplified Chinese
31Arabic
32Thai
33Norwegian (Bokmål)

</details>

Add a linked server

T-SQL command: sp_rds_add_linked_server

Supported instances:

Adds a linked server to an instance. Distributed transactions are supported. A linked server created on an instance is automatically synchronized between primary and secondary nodes, so no reconfiguration is needed after a high-availability (HA) switchover. However, subsequent changes to existing linked servers on the primary node are not synchronized to the secondary node. For more information, see Automatic or manual primary/secondary failover.

Syntax

DECLARE
@linked_server_name sysname = N'yangzhao_slb',          -- Linked server name
@data_source sysname = N'****.sqlserver.rds.aliyuncs.com,3888', -- Destination SQL Server address in IP,Port format
@user_name sysname = N'ay15',                           -- Login name for the destination SQL Server
@password nvarchar(128) = N'******',                    -- Password for the destination SQL Server login
@source_user_name sysname = N'test',                    -- Login name used to create the linked server on the current instance
@source_password nvarchar(128) = N'******',             -- Password for the current instance login
-- Linked server settings in XML format (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 editions: Basic Edition, High-availability Edition

Sets a trace flag for the instance. Only specific trace flags are supported. For instances with primary and secondary nodes, trace flags are automatically synchronized.

Syntax

EXEC sp_rds_dbcc_trace '1222', 1
  • Parameter 1: The trace flag.

  • Parameter 2: 1 to enable, 0 to disable.

Change the name of a database

T-SQL command: sp_rds_modify_db_name

Supported editions: Basic Edition, High-availability Edition, Cluster Edition

Renames a database. Before running, make sure the account has the required permissions on the target database and that the database is online.

For High-availability Edition and Cluster Edition instances, renaming triggers an automatic rebuild of the primary/secondary relationship through a backup and restoration process. If the database is large, check available storage space before proceeding. If storage is insufficient, scale out the instance first.

Syntax

USE master
GO
EXEC sp_rds_modify_db_name 'db', 'new_db'
GO
  • Parameter 1: The original database name.

  • Parameter 2: The new database name.

Grant server-level roles

T-SQL command: sp_rds_set_server_role

Supported editions: Basic Edition

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

Syntax

EXEC sp_rds_set_server_role @login_name = 'test_login', @server_role = 'setupadmin'
  • @login_name: The login name.

  • @server_role: The role to assign. Valid values: setupadmin, processadmin.

FAQ

Why do I get `Cannot use KILL to kill your own process` when I run `EXEC sp_rds_drop_database 'dbtest'` with a standard account?

Run this command with a privileged account in a command window that is not connected to the target database. Make sure the account has the required permissions. See Modify the permissions of an account for details.