[[["容易理解","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 (世界標準時間)。"],[[["\u003cp\u003eThis document guides users on defining custom SQL operations within SQLX files in Dataform for BigQuery.\u003c/p\u003e\n"],["\u003cp\u003eCustom SQL operations, which can include any valid BigQuery SQL statement, are defined in SQLX files of \u003ccode\u003etype: operations\u003c/code\u003e, allowing for actions beyond standard table or assertion creation.\u003c/p\u003e\n"],["\u003cp\u003eUsers can create referenceable output tables from custom SQL operations by setting \u003ccode\u003ehasOutput: true\u003c/code\u003e in the configuration and using the \u003ccode\u003eself\u003c/code\u003e function to align the output table's name with the file name.\u003c/p\u003e\n"],["\u003cp\u003eEmpty tables can be created via custom SQL operations using \u003ccode\u003eCREATE TABLE\u003c/code\u003e, and they can be made referenceable in other scripts by including \u003ccode\u003ehasOutput: true\u003c/code\u003e in the config.\u003c/p\u003e\n"],["\u003cp\u003eMultiple operations in the same SQLX file are executed sequentially, and in open source, they can be separated with \u003ccode\u003e---\u003c/code\u003e, while in BigQuery, they are implicitly separated with semicolons.\u003c/p\u003e\n"]]],[],null,["# Create operations\n\nThis document shows you how to define custom SQL operations in a dedicated SQLX file.\n\n\u003cbr /\u003e\n\nDataform can run custom SQL operations that don't fit into the\nDataform model of publishing a table or writing an assertion. You can\ndefine custom SQL commands for Dataform to run in BigQuery.\n\nBefore you begin\n----------------\n\n1. In the Google Cloud console, go to the **Dataform** page.\n\n [Go to the Dataform page](https://console.cloud.google.com/bigquery/dataform)\n2. Select or [create a repository](/dataform/docs/create-repository).\n\n3. Select or [create a development workspace](/dataform/docs/create-workspace).\n\n### Required roles\n\n\nTo get the permissions that\nyou need to define a custom SQL operation,\n\nask your administrator to grant you the\n\n\n[Dataform Editor](/iam/docs/roles-permissions/dataform#dataform.editor) (`roles/dataform.editor`)\nIAM role on workspaces.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYou might also be able to get\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n\nCreate a file for custom operation definition\n---------------------------------------------\n\nStore custom operation definition SQLX files in the `definitions/` directory.\nTo create a new SQLX file in the `definitions/` directory, follow these steps:\n\n1. Go to your development workspace.\n2. In the **Files** pane, next to `definitions/`, click the **More** menu.\n3. Click **Create file**.\n4. In the **Add a file path** field, enter the name of the file followed by\n `.sqlx` after `definitions/`. For example, `definitions/sample-operation.sqlx`.\n\n Filenames can only include numbers, letters, hyphens, and underscores.\n5. Click **Create file**.\n\nDefine a custom SQL operation\n-----------------------------\n\nYou can define a custom SQL operation in a SQLX file of `type: operations`.\nYou can write any BigQuery SQL statement in an `operations` file.\nDataform runs your custom SQL operations in BigQuery\nwithout modification.\n\nYou can define multiple custom SQL operations in one SQLX file.\nBigQuery runs all the operations in a file in the same context, and\ncreates the run SQL by joining all operations with a semicolon (`;`).\n\nTo define multiple custom SQL operations when using the [open source\nDataform framework](https://github.com/dataform-co/dataform)\nwith a data warehouse other than BigQuery,\nseparate operations with `---`.\n\nTo define a custom SQL operation in a dedicated SQLX file, follow these steps:\n\n1. In your development workspace, select the SQLX file for custom operation definition.\n2. In the file, enter the following code snippet:\n\n config { type: \"operations\" }\n\n3. Outside the `config` block, write your SQL operation.\n\n4. Optional: Click **Format**.\n\nThe following code sample shows multiple custom SQL operations defined in an\n`operations` file: \n\n config { type: \"operations\" }\n\n DELETE FROM dataset.table WHERE country = 'GB';\n\n DELETE FROM dataset.table WHERE country = 'FR';\n\nThe following code sample shows a custom SQL operation that manually\ncreates a view: \n\n config { type: \"operations\" }\n CREATE OR REPLACE VIEW dataset.table AS (SELECT 1 AS TEST)\n\nCreate a referenceable output table\n-----------------------------------\n\nYou can manually create a table in a custom SQL operation that you can\nreference in other scripts. To create a table available to other scripts, you\nneed to declare that the operation has an output.\n\nTo make the name of the output table match the name of the `operations` file,\nyou can use the `self` function in the `CREATE` operation.\n\nTo create a table in a custom operation and make it available to other scripts,\nfollow these steps:\n\n1. In your development workspace, select the SQLX file for custom operation definition.\n2. In the SQLX file, enter the following code snippet:\n\n config {\n type: \"operations\",\n hasOutput: true\n }\n\n | **Important:** When `hasOutput` is set to `true`, this action must create a table that has the same name as the action. Use the [`self()` context function](/dataform/docs/reference/dataform-core-reference#commoncontext) to ensure that the name is the same.\n3. To match the name of the output table with the name of the file,\n write a SQL `CREATE` operation with the `self` function in the following format:\n\n CREATE OR REPLACE TABLE ${self()} AS (\u003cvar translate=\"no\"\u003eCUSTOM_SQL_QUERY\u003c/var\u003e)\n\n Replace \u003cvar translate=\"no\"\u003eCUSTOM_SQL_QUERY\u003c/var\u003e with your table definition SQL `SELECT`statement.\n4. Optional: Click **Format**.\n\n### Reference a custom SQL operation output table\n\n- To reference a custom SQL operation output table in a SQLX definition of a different table, enter the output table filename in the `ref` function.\n\nThe following code sample shows a custom SQL operation in a\n`custom_SQL_operation_table.sqlx` file that creates a referenceable table\ncalled `custom_SQL_operation_table`: \n\n // filename is custom_SQL_operation_table.sqlx\n config {\n type: \"operations\",\n hasOutput: true\n }\n CREATE OR REPLACE VIEW ${self()} AS (SELECT 1 AS TEST)\n\nThe following code sample shows referencing the\n`custom\\_SQL\\_operation\\_table table` in a table definition SQLX file: \n\n config { type: \"table\" }\n SELECT * FROM ${ref(\"custom_SQL_operation_table\")}\n\nCreate an empty table\n---------------------\n\nYou might want to create an empty table so that a different service can\npopulate it with data. You can create an empty table in a custom SQL operation\nwith the `CREATE TABLE` function. To be able to reference the empty table in\nother workflow action definitions, for example, tables and views, you can\nadd the `hasOutput:true` property to the `config` block of the empty table operation.\n\n- To create an empty table, use the `CREATE TABLE` function in a `type: \"operations\"` file in the following format:\n\n config {\n type: \"operations\",\n hasOutput: true // optional, lets you reference the empty table\n }\n\n CREATE TABLE ${self()} (\n\n )\n\nThe following code sample shows a custom SQL operation that creates an empty\ntable with an integer and a string column. The created empty table cannot be\nreferenced by other workflow actions: \n\n config {\n type: \"operations\"\n }\n\n CREATE TABLE ${self()} (\n x INT64,\n y STRING\n )\n\nWhat's next\n-----------\n\n- To learn how to create custom SQL operations with JavaScript, see [Create workflows exclusively with JavaScript](/dataform/docs/javascript-in-dataform#create-workflows-with-javascript).\n- To learn how to trigger runs manually, see [Manually trigger runs](/dataform/docs/trigger-execution).\n- To learn how to use BigQuery DML, see [Using data manipulation language (DML)](/bigquery/docs/reference/standard-sql/data-manipulation-language).\n- To learn more about the `self` function and other Dataform built-in functions, see [Dataform API](/dataform/reference/rest)."]]