Cloud SQL stored procedures Stay organized with collections Save and categorize content based on your preferences.
This section describes stored procedures for Cloud SQL instances.
A stored procedure contains SQL code that you can reuse.
You cannot drop or alter system stored procedures.To execute a stored procedure, you use theEXEC command and replace thefollowing variables:
- procedure_name is the name of the stored procedure.
- database_name is the name of the database where you want to run theprocedure.
- schema_name is the name of the schema where you want to run theprocedure.
EXECdatabase_name.schema_name.procedure_name@param1,@param2;
To create your own stored procedure, seeCreate a Stored Procedure.
Cloud SQL for SQL Server stored procedures
Stored procedure for using bulk insert
For information about bulk insert, seeUse bulk insert for importing data.
msdb.dbo.gcloudsql_bulk_insert
Syntax
EXECmsdb.dbo.gcloudsql_bulk_insert@database@schema@object@file...
Description
This stored procedure has similar parameters and behavior to theBULK INSERT command.
The stored procedure imports data to a Cloud SQL instance from a file stored in aCloud Storage bucket. It usesCloud Storage interoperable APIandHMAC keys to authenticate access to the Cloud Storage bucket.
This stored procedure has the following parameters:
| Parameter | Type | Description |
|---|---|---|
@database | SYSNAME | Specifies the name of the target database to which the data is to be imported. |
@schema | SYSNAME | Specifies the name of the schema to which the table belongs. |
@object | NVARCHAR | Specifies the name of the table where the data is to be inserted. |
@file | NVARCHAR | Specifies the path to the import file in the Cloud Storage bucket.The path must have the following format:s3://storage.googleapis.com/BUCKET_NAME/FILE_PATH
|
@batchsize | INT | Specifies the number of rows in a batch. |
@checkconstraints | BIT | Specifies that all constraints on the target table must be checked. |
@codepage | NVARCHAR | Specifies the code page of the data in the file.RAW is the default and only option. |
@datafiletype | NVARCHAR | Specifies the bulk insert file type. |
@datasource | NVARCHAR | Specifies the name of the external data source from which you want to import the data. |
@errorfile | NVARCHAR | Specifies the path to the file used to collect rows that have formatting errors.The path must have the following format:s3://storage.googleapis.com/BUCKET_NAME/FILE_PATH
.ERROR.txt. This file contains references to each row in the error file and provides error diagnostics. |
@errorfiledatasource | NVARCHAR | Specifies the name of the external data source in which you want to create the error file. |
@firstrow | INT | Specifies the numeric identifier of the first row to load. |
@firetriggers | BIT | Indicates that any insert triggers defined on the target table would execute during the bulk insert operations. |
@formatfiledatasource | NVARCHAR | Specifies the name of the external data source from which you should load the format file. |
@keepidentity | BIT | Specifies the use of identity data from the import file for the identity column. The values are0, which means false, and1, which means true. |
@keepnulls | BIT | Specifies whether empty columns should retain a null value during the bulk import operation, instead of having any default values for the columns inserted. The values are0, which means false, and1, which means true. |
@kilobytesperbatch | INT | Specifies the amount of data per batch, in KB. |
@lastrow | INT | Specifies the numeric identifier of the last row to load. |
@maxerrors | INT | Specifies the number of errors allowed, before Cloud SQL cancels the operation. |
@ordercolumnsjson | NVARCHAR | Specifies the sort order and columns, in JSON format. For example:[{"name":"COLUMN_NAME","order":"ORDER"},{"name":"COLUMN_NAME","order":"ORDER"}]
|
@rowsperbatch | INT | Specifies the number of rows per batch.For more information about selecting a batch size, seePerformance considerations |
@tablock | BIT | Specifies that a table lock is taken for the duration of the bulk insert operation. |
@format | NVARCHAR | Specifies the format of the file. UseCSV as the value of this parameter. |
@fieldquote | NVARCHAR | Specifies the character to be used as the quote character in the CSV file.If you don't specify a value, then Cloud SQL uses" as the default value. |
@formatfile | NVARCHAR | Specifies the path of the file in Cloud Storage describing the format of the data to be imported.The path should have the following format:s3://storage.googleapis.com/BUCKET_NAME/FILE_PATH
|
@fieldterminator | NVARCHAR | Specifies the field terminator for char and widechar data files. |
@rowterminator | NVARCHAR | Specifies the row terminator for char and widechar data files. |
Stored procedures for SQL Server Audit functionality
For information about using the functionality of SQL Server Audit, seeSQL Server database auditing.
msdb.dbo.gcloudsql_fn_get_audit_file
Syntax
msdb.dbo.gcloudsql_fn_get_audit_file
Description
Retrieves the data from an audit file that was created by SQL Server Auditfunctionality.
This stored procedure accepts the same parameters as thesys.fn_get_audit_file function. See thedocumentation for that functionfor more information related tomsdb.dbo.gcloudsql_fn_get_audit_file.
Stored procedures for change data capture (CDC)
For more information about CDC, seeEnable change data capture.
msdb.dbo.gcloudsql_cdc_enable_db
Syntax
execmsdb.dbo.gcloudsql_cdc_enable_dbdatabaseName
Description
Turns change data capture on for a database.
databaseName- Name of the database to run this stored procedure on.
msdb.dbo.gcloudsql_cdc_disable_db
Syntax
execmsdb.dbo.gcloudsql_cdc_disable_dbdatabaseName
Description
Turns CDC off for a database.
databaseName- Name of the database to run this stored procedure on.
Stored procedures for external replication with Cloud SQL as a publisher
For more information about publishing to an external subscriber, or to anotherCloud SQL instance, seeConfigure external replicas.
msdb.dbo.gcloudsql_transrepl_setup_distribution
Syntax
execmsdb.dbo.gcloudsql_transrepl_setup_distribution@login,@passwordDescription
A wrapper stored procedure that sets up a distribution database. The wrappercalls the following:sp_adddistributor,sp_adddistributiondb,andsp_adddistpublisher.
- login - An existing login used to connect and create the distributiondatabase.
- password - The password used when connecting to the distributor.
msdb.dbo.gcloudsql_transrepl_replicationdboption
Syntax
execmsdb.dbo.gcloudsql_transrepl_replicationdboption@db,@valueDescription
Enables or disables the publishing option of a database for the publisher thatusessp_replicationdboption.
- db - The database for which the replication option is being set.
- value - Allows you to specify
Trueto enable the publishing option, orFalseto disable the publishing option.
msdb.dbo.gcloudsql_transrepl_addlogreader_agent
Syntax
execmsdb.dbo.gcloudsql_transrepl_addlogreader_agent@db,@login,@passwordDescription
Sets up the log reader agent for a database that usessp_addlogreader_agent.
- db - Database to be published.
- login - Login used when connecting to the publisher.
- password - The password used when connecting.
msdb.dbo.gcloudsql_transrepl_addpublication
Syntax
execmsdb.dbo.gcloudsql_transrepl_addpublication@db,@publicationDescription
Creates the transactional publication, and acts as a wrapper stored procedureforsp_addpublication.
- db - Database being published.
- publication - Name of the new publication created.
msdb.dbo.gcloudsql_transrepl_droppublication
Syntax
execmsdb.dbo.gcloudsql_transrepl_droppublication@db,@publicationDescription
Drops the transactional publication, and acts as a wrapper stored procedure forsp_droppublication.
- db - Database for which publication is dropped.
- publication - Name of the publication dropped.
msdb.dbo.gcloudsql_transrepl_addpublication_snapshot
Syntax
execmsdb.dbo.gcloudsql_transrepl_addpublication_snapshot@db,@publication,@login,@passwordDescription
Creates a snapshot agent for the database that is being published, acting as awrapper stored procedure forsp_addpublication_snapshot.
- db - Database for which publication is dropped.
- publication - Name of the publication dropped.
- login - Login used when connecting to the publisher.
- password - The password used when connecting.
msdb.dbo.gcloudsql_transrepl_addpushsubscription_agent
Syntax
execmsdb.dbo.gcloudsql_transrepl_addpushsubscription_agent@db,@publication,@subscriber_db,@subscriber_login,@subscriber_password,@subscriberDescription
Creates a new scheduled agent job to synchronize the push subscription, actingas a wrapper stored procedure forsp_addpushsubscription_agent.
- db - Database that is published.
- publication - Name of the publication to which to add a pushsubscription agent.
- subscriber_db - The database on the subscriber.
- subscriber_login - The login used when connecting to the subscriber.
- subscriber_password - The password used when connecting to thesubscriber.
- subscriber - The IP name of the subscriber instance. This value canspecified as:
<Hostname>,<PortNumber>
msdb.dbo.gcloudsql_transrepl_addmonitoraccess
Syntax
execmsdb.dbo.gcloudsql_transrepl_addmonitoraccess@loginDescription
Provides access to the Replication Monitor and theSELECT statement onreplication-related tables on the distribution database.
- login - The login used to access the replication monitor.
msdb.dbo.gcloudsql_transrepl_changedistributor_property
Syntax
execmsdb.dbo.gcloudsql_transrepl_changedistributor_property@property,@valueDescription
This stored procedure changes theheartbeat_interval, andwrapssp_changedistributor_property. For more information, see thedocumentationforsp_changedistributor_property. Also see that documentation for moreinformation about theheartbeat_interval value.
- property - The property for a distribution database.
- value - The value to provide for the specified property.
msdb.dbo.gcloudsql_transrepl_dropsubscriber
Syntax
execmsdb.dbo.gcloudsql_transrepl_dropsubscriber@subscriberDescription
Removes the subscriber, acting as a wrapper stored procedure forsp_dropsubscriber.
- subscriber - The IP name of the subscriber to be dropped. This value canspecified as:
<Hostname>,<PortNumber>
msdb.dbo.gcloudsql_transrepl_remove_distribution
Syntax
execmsdb.dbo.gcloudsql_transrepl_remove_distributionDescription
Removes the distribution setup, acting as a wrapper stored procedure for thefollowing:sp_dropdistpublisher,sp_dropdistributiondb,andsp_dropdistributor.
What's next
- Learn how toenable change data capture (CDC).
- Learn how toconfigure external replicas.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-17 UTC.