--- Create a new customer record.DECLAREidSTRING;CALLmydataset.create_customer("alice",id);--- Display the record.SELECT*FROMmydataset.customersWHEREcustomer_id=id;
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-17。"],[[["\u003cp\u003eStored procedures are collections of SQL statements that can be called from other queries or procedures, and they can accept input arguments and return output values.\u003c/p\u003e\n"],["\u003cp\u003eSystem procedures are built-in procedures that do not need to be created manually, and you can access them through the system procedures reference.\u003c/p\u003e\n"],["\u003cp\u003eYou can create stored procedures using the \u003ccode\u003eCREATE PROCEDURE\u003c/code\u003e statement, defining the procedure's logic within \u003ccode\u003eBEGIN\u003c/code\u003e and \u003ccode\u003eEND\u003c/code\u003e blocks, and it can have multiple parameters, such as input, output, and input/output.\u003c/p\u003e\n"],["\u003cp\u003eStored procedures can be invoked using the \u003ccode\u003eCALL\u003c/code\u003e statement, such as \u003ccode\u003eCALL mydataset.create_customer();\u003c/code\u003e, and system procedures can also be called using the same method.\u003c/p\u003e\n"],["\u003cp\u003eAuthorized routines allow you to share query results without granting access to the underlying tables, enabling the creation, dropping, and manipulation of tables.\u003c/p\u003e\n"]]],[],null,["# Work with SQL stored procedures\n===============================\n\nA *stored procedure* is a collection of statements that can be called from other\nqueries or other stored procedures. A procedure can take input arguments and\nreturn values as output. You name and store a procedure in a\nBigQuery dataset. A stored procedure can access or modify data\nacross multiple datasets by multiple users. It can also contain a\n[multi-statement query](/bigquery/docs/multi-statement-queries).\n\nSome stored procedures are built into BigQuery and don't need to\nbe created. These are called *system procedures* and you can learn more about\nthem in the [System procedures reference](/bigquery/docs/reference/system-procedures).\n\nStored procedures support *procedural language statements* , which let you do\nthings like define variables and implement control flow. You can learn more\nabout procedural language statements in the\n[Procedural language reference](/bigquery/docs/reference/standard-sql/procedural-language).\n\nCreate a stored procedure\n-------------------------\n\nTo create a procedure, use the\n[`CREATE PROCEDURE`](/bigquery/docs/reference/standard-sql/data-definition-language#create_procedure)\nstatement.\n\nIn the following conceptual example, `procedure_name` represents\nthe procedure and the body of the procedure appears between\n[`BEGIN`](/bigquery/docs/reference/standard-sql/procedural-language#begin) and\n`END` statements: \n\n CREATE PROCEDURE dataset_name.procedure_name()\n BEGIN\n -- statements here\n END\n\nThe following example shows a procedure that contains a multi-statement query.\nThe multi-statement query sets a variable, runs an `INSERT` statement, and\ndisplays the result as a formatted text string. \n\n CREATE OR REPLACE PROCEDURE mydataset.create_customer()\n BEGIN\n DECLARE id STRING;\n SET id = GENERATE_UUID();\n INSERT INTO mydataset.customers (customer_id)\n VALUES(id);\n SELECT FORMAT(\"Created customer %s\", id);\n END\n\nIn the preceding example, the name of the procedure is\n`mydataset.create_customer`, and the body of procedure appears between\n[`BEGIN`](/bigquery/docs/reference/standard-sql/procedural-language#begin) and\n`END` statements.\n\nTo call the procedure, use the\n[`CALL`](/bigquery/docs/reference/standard-sql/procedural-language#call)\nstatement: \n\n CALL mydataset.create_customer();\n\n### Pass a value in with an input parameter\n\nA procedure can have input parameters. An input parameter allows input for\na procedure, but does not allow output. \n\n CREATE OR REPLACE PROCEDURE mydataset.create_customer(name STRING)\n BEGIN\n DECLARE id STRING;\n SET id = GENERATE_UUID();\n INSERT INTO mydataset.customers (customer_id, name)\n VALUES(id, name);\n SELECT FORMAT(\"Created customer %s (%s)\", id, name);\n END\n\n### Pass a value out with an output parameter\n\nA procedure can have output parameters. An output parameter returns a value\nfrom the procedure, but does not allow input for the procedure. To create an\noutput parameter, use the `OUT` keyword before the name of the parameter.\n\nFor example, this version of the procedure returns the new customer ID through\nthe `id` parameter: \n\n CREATE OR REPLACE PROCEDURE mydataset.create_customer(name STRING, OUT id STRING)\n BEGIN\n SET id = GENERATE_UUID();\n INSERT INTO mydataset.customers (customer_id, name)\n VALUES(id, name);\n SELECT FORMAT(\"Created customer %s (%s)\", id, name);\n END\n\nTo call this procedure, you must use a variable to receive the output value: \n\n --- Create a new customer record.\n DECLARE id STRING;\n CALL mydataset.create_customer(\"alice\",id);\n\n --- Display the record.\n SELECT * FROM mydataset.customers\n WHERE customer_id = id;\n\n### Pass a value in and out with an input/output parameter\n\nA procedure can also have input/output parameters. An input/output parameter\nreturns a value from the procedure and also accepts input for the procedure. To\ncreate an input/output parameter, use the `INOUT` keyword before the name of the\nparameter. For more information, see\n[Argument mode](/bigquery/docs/reference/standard-sql/data-definition-language#argument_mode).\n\nAuthorize routines\n------------------\n\nYou can authorize stored procedures as *routines*.\nAuthorized routines let you share query results with specific users or groups\nwithout giving them access to the underlying tables that generated the results.\nFor example, an authorized routine can compute an aggregation\nover data or look up a table value and use that value in a computation.\n\nAuthorized routines can\n[create](/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement),\n[drop](/bigquery/docs/reference/standard-sql/data-definition-language#drop_table_statement),\nand [manipulate tables](/bigquery/docs/reference/standard-sql/dml-syntax),\nas well as\n[invoke other stored procedures](/bigquery/docs/procedures#call_a_stored_procedure)\non the underlying table.\n\nFor more information, see [Authorized routines](/bigquery/docs/authorized-routines).\n\nCall a stored procedure\n-----------------------\n\nTo call a stored procedure after it's been created, use the `CALL` statement.\nFor example, the following statement calls the stored procedure\n`create_customer`: \n\n CALL mydataset.create_customer();\n\n| **Note:** Calling a stored procedure rather than including the procedure's SQL statements directly in your query introduces a small performance overhead.\n\nCall a system procedure\n-----------------------\n\nTo call a built-in system procedure, use the `CALL` statement.\nFor example, the following statement calls the system procedure\n`BQ.REFRESH_MATERIALIZED_VIEW`: \n\n CALL BQ.REFRESH_MATERIALIZED_VIEW;"]]