Pink_boston
Pink_boston

Reputation: 11

Are Macros variables in big query possible?

I am new to big query. In my code I want to be able to declare variable names that I can reference in my table names and where clauses. Is this possible in big query. Just so when I rerun the code I don’t have to find the parts to change. So I would like to have this at the start of my code that I can change every time I run the code

enter code here
Start_date=‘2020-05-01’
End_date=‘2020-06-01’
Selection_Date=‘20200602’
Create table test_selection_date as Select * from sales Where date>= start_date and date<=End_date;

Anybody know how this is possible and how I can code it in big query? Thanks

Upvotes: 1

Views: 2044

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 173106

Below is for BigQuery Standard SQL and should give you good start

DECLARE Start_date, End_date, Selection_Date STRING;
SET (Start_date, End_date, Selection_Date) = ('2020-05-01', '2020-06-01', '20200602');

EXECUTE IMMEDIATE FORMAT(
  "Create table test_%s as Select * from sales Where date>= '%s' and date < = '%s'", 
  Selection_Date, Start_date, End_date
);

See more about Scripting to tune above to your real use-case

Upvotes: 2

Related Questions