以下示例是一个联合查询,该查询联接 SAP Datasphere 中名为 ORDERS 的表和 BigQuery 中名为 mydataset.customers 的表。
SELECTc.customer_id,c.name,rq.first_order_dateFROMmydataset.customersAScLEFTOUTERJOINEXTERNAL_QUERY('connection_id','''SELECT CUSTOMER_ID, MIN(ORDER_DATE) AS first_order_date FROM ORDERS GROUP BY CUSTOMER_ID''')ASrqONrq.customer_id=c.customer_idGROUPBYc.customer_id,c.name,rq.first_order_date;
-- List all views in a schema.SELECT*FROMEXTERNAL_QUERY('connection_id','''SELECT VIEW_NAME FROM SYS.VIEWS WHERE SCHEMA_NAME = 'MY_SCHEMA'''');
-- List all columns in a view.SELECT*FROMEXTERNAL_QUERY('connection_id','''SELECT COLUMN_NAME, DATA_TYPE_NAME FROM SYS.VIEW_COLUMNS WHERE SCHEMA_NAME = 'MY_SCHEMA' AND VIEW_NAME = 'my_view' ORDER BY POSITION''');
价格
运行联合查询的费用取决于三个因素:
在 SAP Datasphere 中执行查询的计算费用。
将查询结果从 SAP Datasphere 转移到 BigQuery 的带宽费用。
在 BigQuery 中执行查询的计算费用。
任何与 SAP Datasphere 相关的费用取决于您使用的 SAP 服务类型。为了限制带宽费用,我们建议您在 EXTERNAL_QUERY 中编写查询,使其排除所有不需要计算最终结果的列和行。
[[["易于理解","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-09-04。"],[[["\u003cp\u003eBigQuery's SAP Datasphere federation allows real-time querying of data in SAP Datasphere without data replication.\u003c/p\u003e\n"],["\u003cp\u003eFederated queries to SAP Datasphere are executed using the \u003ccode\u003eEXTERNAL_QUERY\u003c/code\u003e function within BigQuery, with results transferred back to BigQuery.\u003c/p\u003e\n"],["\u003cp\u003eThis feature is currently in a Pre-GA phase and has limitations, including querying only relational views exposed for consumption, potential higher latency, and no SQL pushdown support.\u003c/p\u003e\n"],["\u003cp\u003eUsers require the BigQuery Connection User IAM role to query SAP Datasphere and must have an existing shared connection.\u003c/p\u003e\n"],["\u003cp\u003eThe cost of federated queries is based on compute in both SAP Datasphere and BigQuery, and bandwidth for transferring results.\u003c/p\u003e\n"]]],[],null,["# SAP Datasphere federated queries\n================================\n\n|\n| **Preview**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n| **Note:** To get support or provide feedback for this preview feature, contact [bq-sap-federation-support@google.com](mailto:bq-sap-federation-support@google.com).\n\nAs a data analyst, you can query relational data in SAP Datasphere from BigQuery using federated queries.\n\nBigQuery SAP Datasphere federation lets BigQuery query data residing in SAP Datasphere in real time, without copying or moving data.\n\nTo run a SQL query in SAP Datasphere, specify that SQL query within BigQuery in a `EXTERNAL_QUERY` function. The results are then transferred from SAP Datasphere to BigQuery.\n\nLimitations\n-----------\n\n- You can only query relational views that are [exposed for consumption](https://help.sap.com/docs/SAP_DATASPHERE/43509d67b8b84e66a30851e832f66911/d7d56284bb5148c887ac4054689bfbca.html?locale=en-US). Other objects in SAP Datasphere are not accessible to the query federated through `EXTERNAL_QUERY`.\n- The federated query latency might be noticeably higher than the same query if it was executed directly in SAP Datasphere.\n- The first query that uses SAP Datasphere connection in a given project might take more than a minute to run.\n- No additional [SQL pushdowns](/bigquery/docs/federated-queries-intro#sql_pushdowns) are supported for SAP Datasphere.\n- The SAP Datasphere SQL query must specify aliases for columns that contain function results.\n- When the usage of Compute Engine API in the query project is restricted by [VPC Service Controls](/vpc-service-controls/docs/overview), the federated query will fail.\n\nBefore you begin\n----------------\n\nEnsure that your BigQuery administrator has [created a SAP Datasphere connection](/bigquery/docs/connect-to-sap-datasphere)\nand [shared](/bigquery/docs/connect-to-sap-datasphere#share_connections)\nit with you.\n\n### Required roles\n\n\nTo get the permissions that\nyou need to query SAP Datasphere,\n\nask your administrator to grant you the\n\n\n[BigQuery Connection User](/iam/docs/roles-permissions/bigquery#bigquery.connectionUser) (`roles/bigquery.connectionUser`)\nIAM role on the project.\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\nQuery data\n----------\n\nTo send a federated query to SAP Datasphere from a GoogleSQL query, use the\n[EXTERNAL_QUERY function](/bigquery/docs/reference/standard-sql/federated_query_functions#external_query).\n\nThe following example is a federated query that joins a table in SAP Datasphere named `ORDERS` and a table in BigQuery named `mydataset.customers`. \n\n SELECT c.customer_id, c.name, rq.first_order_date\n FROM mydataset.customers AS c\n LEFT OUTER JOIN EXTERNAL_QUERY(\n 'connection_id',\n '''SELECT CUSTOMER_ID, MIN(ORDER_DATE) AS first_order_date\n FROM ORDERS\n GROUP BY CUSTOMER_ID''') AS rq\n ON rq.customer_id = c.customer_id\n GROUP BY c.customer_id, c.name, rq.first_order_date;\n\nView a SAP Datasphere table schema\n----------------------------------\n\nThe following examples use the [EXTERNAL_QUERY function](/bigquery/docs/reference/standard-sql/federated_query_functions#external_query) to retrieve database metadata from the\n`SYS` schema in SAP Datasphere. \n\n -- List all views in a schema.\n SELECT * FROM EXTERNAL_QUERY(\n 'connection_id',\n '''SELECT VIEW_NAME FROM SYS.VIEWS\n WHERE SCHEMA_NAME = 'MY_SCHEMA'''');\n\n -- List all columns in a view.\n SELECT * FROM EXTERNAL_QUERY(\n 'connection_id',\n '''SELECT COLUMN_NAME, DATA_TYPE_NAME\n FROM SYS.VIEW_COLUMNS\n WHERE SCHEMA_NAME = 'MY_SCHEMA' AND\n VIEW_NAME = 'my_view'\n ORDER BY POSITION''');\n\nPricing\n-------\n\nThe cost of running a federated query is based on three factors:\n\n- The compute cost of executing the query in SAP Datasphere.\n- The bandwidth cost of transferring the query results from SAP Datasphere to BigQuery.\n- The compute cost of executing the query in BigQuery.\n\nAny SAP Datasphere related costs depend on the type of SAP service you use. To\nlimit the bandwidth cost, we recommend that you write the query in the\n`EXTERNAL_QUERY` so that it excludes all columns and rows that are not needed to\ncompute the final result.\n\nThere is no additional cost for running federated queries in BigQuery.\nFor more information about BigQuery pricing, see [Pricing](/bigquery/pricing).\n\nWhat's next\n-----------\n\n- Learn about [federated queries](/bigquery/docs/federated-queries-intro).\n- Learn about [unsupported data types](/bigquery/docs/reference/standard-sql/federated_query_functions#unsupported_data_types)."]]