テーブル関数を作成するには、CREATE TABLE FUNCTION ステートメントを使用します。テーブル関数には、テーブルを生成するクエリが含まれています。この関数は、クエリ結果を返します。次のテーブル関数は、INT64 パラメータを取得し、その値を bigquery-public-data.usa_names.usa_1910_current という一般公開データセットに対するクエリの WHERE 句で使用します。
+------+--------+-------+| year | name | total |+------+--------+-------+| 1950 | James | 86447 || 1950 | Robert | 83717 || 1950 | Linda | 80498 || 1950 | John | 79561 || 1950 | Mary | 65546 |+------+--------+-------+
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eTable functions, or table-valued functions (TVFs), are user-defined functions that return a table, usable anywhere a table is valid, and can take parameters, unlike views.\u003c/p\u003e\n"],["\u003cp\u003eTable functions are created using the \u003ccode\u003eCREATE TABLE FUNCTION\u003c/code\u003e statement, which contains a query that produces a table, and the function returns the query's result.\u003c/p\u003e\n"],["\u003cp\u003eYou can call table functions within \u003ccode\u003eSELECT\u003c/code\u003e statements, join their output with other tables, and use them in subqueries.\u003c/p\u003e\n"],["\u003cp\u003eTable functions are subject to certain limitations, such as parameters having to be scalar values, and the query body must be a \u003ccode\u003eSELECT\u003c/code\u003e statement without any modifications.\u003c/p\u003e\n"],["\u003cp\u003eTable functions are classified as a type of routine in big query, which are authorized to share query results with users without giving direct access to the underlying data.\u003c/p\u003e\n"]]],[],null,["# Table functions\n===============\n\nA table function, also called a table-valued function (TVF), is a user-defined\nfunction that returns a table. You can use a table function anywhere that you\ncan use a table. Table functions behave similarly to views, but a table function\ncan take parameters.\n\nCreate table functions\n----------------------\n\nTo create a table function, use the\n[`CREATE TABLE FUNCTION`](/bigquery/docs/reference/standard-sql/data-definition-language#create_table_function_statement)\nstatement. A table function contains a query that produces a table. The function\nreturns the query result. The following table function takes an `INT64`\nparameter and uses this value inside a `WHERE` clause in a query over a\n[public dataset](/bigquery/public-data) called\n`bigquery-public-data.usa_names.usa_1910_current`: \n\n```googlesql\nCREATE OR REPLACE TABLE FUNCTION mydataset.names_by_year(y INT64)\nAS (\n SELECT year, name, SUM(number) AS total\n FROM `bigquery-public-data.usa_names.usa_1910_current`\n WHERE year = y\n GROUP BY year, name\n);\n```\n\nTo filter in other ways, you can pass multiple parameters to a table function.\nThe following table function filters the data by year and name prefix: \n\n```googlesql\nCREATE OR REPLACE TABLE FUNCTION mydataset.names_by_year_and_prefix(\n y INT64, z STRING)\nAS (\n SELECT year, name, SUM(number) AS total\n FROM `bigquery-public-data.usa_names.usa_1910_current`\n WHERE\n year = y\n AND STARTS_WITH(name, z)\n GROUP BY year, name\n);\n```\n\n### Parameter names\n\nIf a table function parameter matches the name of a table column, it can create\nan ambiguous reference. In that case, BigQuery interprets the\nname as a reference to the table column, not the parameter. The recommended\npractice is to use parameter names that are distinct from the names of any\nreferenced table columns.\n\nUse table functions\n-------------------\n\nYou can call a table function in any context where a table is valid. The following\nexample calls the `mydataset.names_by_year` function in the `FROM` clause of\na `SELECT` statement: \n\n SELECT * FROM mydataset.names_by_year(1950)\n ORDER BY total DESC\n LIMIT 5\n\nThe results look like the following: \n\n +------+--------+-------+\n | year | name | total |\n +------+--------+-------+\n | 1950 | James | 86447 |\n | 1950 | Robert | 83717 |\n | 1950 | Linda | 80498 |\n | 1950 | John | 79561 |\n | 1950 | Mary | 65546 |\n +------+--------+-------+\n\nYou can join the output from a table function with another table: \n\n SELECT *\n FROM `bigquery-public-data.samples.shakespeare` AS s\n JOIN mydataset.names_by_year(1950) AS n\n ON n.name = s.word\n\nYou can also use a table function in a\n[subquery](/bigquery/docs/reference/standard-sql/subqueries#array_subquery_concepts): \n\n SELECT ARRAY(\n SELECT name FROM mydataset.names_by_year(1950)\n ORDER BY total DESC\n LIMIT 5)\n\nList table functions\n--------------------\n\nTable functions are a type of routine. To list all of the routines in a dataset,\nsee [List routines](/bigquery/docs/routines#list_routines).\n\nDelete table functions\n----------------------\n\nTo delete a table function, use the\n[`DROP TABLE FUNCTION`](/bigquery/docs/reference/standard-sql/data-definition-language#drop_table_function)\nstatement: \n\n DROP TABLE FUNCTION mydataset.names_by_year\n\nAuthorize routines\n------------------\n\nYou can authorize table functions 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.\nFor more information, see [Authorized routines](/bigquery/docs/authorized-routines).\n\nLimitations\n-----------\n\n- Parameters must be scalar values. Table functions in BigQuery\n cannot take tables as input parameters.\n\n- The query body must be a `SELECT` statement and cannot modify anything. For\n example, data definition language (DDL) and data manipulation language (DML)\n statements are not allowed in table functions. If you need side-effects,\n consider writing a\n [procedure](/bigquery/docs/reference/standard-sql/data-definition-language#create_procedure)\n instead.\n\n- Table functions must be stored in the same location as the tables they\n reference.\n\nQuotas\n------\n\nFor more information about table function quotas and limits, see\n[Quotas and limits](/bigquery/quotas#table_function_limits)."]]