[ Web Proxy ]
URL:
Viewing: https://cloud.google.com/sql/docs/mysql/use-parameterized-secure-views [Back]  [Original]

Use parameterized secure views  |  Cloud SQL for MySQL  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback

Use parameterized secure views Stay organized with collections Save and categorize content based on your preferences.

Preview — Parameterized secure views

This feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the Service Specific Terms. Pre-GA features are available "as is" and might have limited support. For more information, see the launch stage descriptions.

This document describes how to use parameterized secure views in Cloud SQL for MySQL, which let you limit data access based on application-specific named parameters, like application user credentials. Parameterized secure views improve security and access control by extending the functionality of MySQL views. These views also mitigate the risks of running untrusted queries from applications by enforcing a number of restrictions automatically on any query that is executed.

For more information, see Parameterized secure views in Cloud SQL.

Before you begin

This document assumes that you've already created a Cloud SQL for MySQL instance.

  1. Make sure that your Cloud SQL instance is running MySQL 8.0.43 or later. If your MySQL 8.0 instance is running an earlier minor version, then you can perform a minor version upgrade.

  2. Enable the cloudsql_parameterized_secure_view database flag for your instance.

    This flag change doesn't require a database restart. For more information, see Configure database flags.

Create a parameterized secure view

To create a parameterized secure view, run the CREATE VIEW DDL command. For example:

CREATE VIEW v_orders
AS SELECT * FROM orders
WHERE customer_id = @local_customer_id;

Use the following syntax:

-- Create a view with a parameter using the WHERE clause
CREATE VIEW VIEW_NAME AS
SELECT * FROM TABLE_NAME
WHERE CONDITION;
-- Create a view with a parameter using the HAVING clause
CREATE VIEW VIEW_NAME AS
SELECT * FROM TABLE_NAME
WHERE CONDITION_1      -- row level security
GROUP BY COLUMN_NAME
HAVING CONDITION_2;    -- grouping
-- Create a view with a parameter using the ON clause
CREATE VIEW VIEW_NAME AS
SELECT * FROM TABLE_NAME_1
JOIN TABLE_NAME_2
ON CONDITION_1       --  join criteria
WHERE CONDITION_2;   --  row level security

Replace the following:

Query a parameterized secure view

To query a parameterized secure view in Cloud SQL for MySQL, you have two options:

Use hint values

To query the parameterized secure view by using the SET_VIEW_VARS hint, do the following:

-- Set the parameter and query
SELECT /*+ SET_VIEW_VARS(SESSION_VARIABLE = VALUE) */ *
FROM VIEW_NAME;

Replace the following:

For example:

SELECT /*+ SET_VIEW_VARS(local_customer_id = 12345) */ *
FROM v_orders;

Use the parameterized query procedure

To query the parameterized secure view by using the mysql.execute_parameterized_query procedure, use the following syntax:

CALL mysql.execute_parameterized_query(
  'SELECT * FROM DATABASE_NAME.VIEW_NAME',
  'SESSION_VARIABLE=VALUE');

Replace the following:

For example:

   CALL mysql.execute_parameterized_query(
    'SELECT * FROM db.v_orders',
    'local_customer_id = 5, earliest_year = 2021');

Restrictions on queries

The following lists the set of restricted operations for queries that you run using the options described in Query a parameterized secure view:

View parameterized secure views in logs

When Cloud SQL for MySQL database users try to create a parameterized secure view, an information-level message is logged in the MySQL error log similar to the following:

 User variables permitted in %s.%s through Parameterized
 Secure View feature

Troubleshoot parameterized secure views

Issue Troubleshooting
cloud_parameterized_secure_view flag isn't set for the instance If the cloudsql_parameterized_secure_view flag is set to OFF, then any attempt to query an existing parameterized secure view will result in the following error:

Cannot operate on a Parameterized Secure View without `cloudsql_parameterized_secure_view` being set. Please turn the flag on first before querying Parameterized Secure Views

If you attempt to create a parameterized secure view without the flag set, then you receive the following error:

View's SELECT contains a variable or parameter

You can verify whether the parameterized secure views is enabled for a Cloud SQL instance by checking the global variable:

SHOW VARIABLES LIKE 'cloudsql_parameterized_secure_view';
Query fails with the error View's SELECT contains a variable or parameter. Variables aren't allowed outside of WHERE, HAVING, or ON clauses. Using a variable outside of these clauses results in an error.
Query fails with User variable `%s` used in Parameterized Secure View %s must be set in the query before using SET_VIEW_VARS hint error Set the session variable before you run the query.
Query fails with a MySQL version error If you try to query an existing parameterized secure view from a MySQL version earlier than 8.0.43, then you can encounter the following error:

Variable %s found in view %s. Please upgrade to 8.0.43 or above to use Parameterized Secure Views

What's next

Send feedback

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-08-17 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-08-17 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page