简体   繁体   中英

How to use system variable @@dataset_id in BigQuery view

BigQuery has session-bound (or script-bound) system variables, described here . Within a session, I can declare the value of one of those variables, eg with something like:

set @@dataset_id = 'my_dataset_id';

Now, I'd like to have a view (which I plan on running within a session) that includes something like:

create view foo
as
select ...
from @@dataset_id.my_table

... this doesn't work. Nor does any form of quoting around that variable. It appears use of that variable simply isn't allowed to help identify the namespace of my_table .

If that's true, I'm struggling to see the value of that variable at all. Does anyone know if I can use those variables as so, or how to prevent needing to namespace-bound all instances of my_table ? I'd like to manage these query scripts outside of BQ itself, and ideally without templating in everywhere (eg {dataset_id}.my_table )

I have tried to replicate your concern, Please try below:

set @@dataset_id = "SampleDataSetID";

EXECUTE IMMEDIATE format("""
CREATE VIEW sampleView1 as (
  SELECT
    *
  FROM
    %s.sampleTable
  
);
""", @@dataset_id);

I used EXECUTE IMMEDIATE to run the sql dynamically ( using the variable that we set to the session.)

Output: 在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM