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)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: |
@sample_percent | No | The sampling percentage for statistics information. The data type is 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: |
@max_dop | No | The degree of parallelism (DOP). The data type is |
@timeout_seconds | No | The timeout period for the statistics information update operation, in seconds. The default value is |
@modification_threshold | No | The modification threshold percentage for updating statistics information. The data type is
|
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.
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'
GOThe 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'
GOThe 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.
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
Basic Edition instances do not support this stored procedure. You can use
DROP DATABASE dbinstead.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'
GOThe 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
GOThe 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. |
|
max worker threads | Specifies the maximum number of worker threads for parallel query execution and request processing. |
|
cost threshold for parallelism | Specifies the cost threshold for parallelism. |
|
max degree of parallelism | Specifies the maximum degree of parallelism for queries. |
|
min server memory (MB) | Specifies the minimum amount of memory used by the SQL Server instance. |
|
max server memory (MB) | Specifies the maximum amount of memory used by the SQL Server instance. |
|
blocked process threshold (s) | Specifies the threshold for blocked processes. |
|
nested triggers | Specifies whether to enable nested triggers. Valid values:
Note This parameter is not supported for instances created before May 2023. |
|
Ad Hoc Distributed Queries | Specifies whether to enable ad hoc distributed queries. Valid values:
Note This parameter is not supported for instances created before May 2023. |
|
clr enabled | Specifies whether to enable Common Language Runtime (CLR). Valid values:
Note This parameter is not supported for instances created before May 2023. |
|
default full-text language | Specifies the default full-text search language. Common values:
Note This parameter is not supported for instances created before May 2023. |
|
default language | Specifies the default language. Common values:
Note This parameter is not supported for instances created before May 2023. |
|
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:
|
optimize for ad hoc workloads | Specifies whether to enable the dynamic management view that is optimized for ad hoc workloads. Valid values:
Note This parameter is not supported for instances created before May 2023. |
|
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. |
|
recovery interval (min) | Specifies the recovery interval. Note This parameter is not supported for instances created before May 2023. |
|
remote login timeout (s) | Specifies the timeout period for a remote logon. Note This parameter is not supported for instances created before May 2023. |
|
remote query timeout (s) | Specifies the timeout period for a remote query. Note This parameter is not supported for instances created before May 2023. |
|
query wait (s) | Specifies the time that a query waits for resources. Note This parameter is not supported for instances created before May 2023. |
|
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. |
|
in-doubt xact resolution | Specifies how the system handles in-doubt distributed transactions. Valid values:
|
|
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_optionsSet 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/0The 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'
GOThe 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.
Because
tempdbis a temporary system database,tempdbis 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) anddrop(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.