Home > Net >  How to pass a string of column name as a parameter into a CREATE TABLE FUNCTION in BigQuery
How to pass a string of column name as a parameter into a CREATE TABLE FUNCTION in BigQuery

Time:03-21

I want to create a table function that takes two arguments, fieldName and parameter, where I can later use this function to create tables in other fieldName and parameter pairs. I tried multiple ways, and it seems like the fieldName(column name) is always parsed as a string in the where clause. Wondering how should I be doing this in the correct way.

CREATE OR REPLACE TABLE FUNCTION dataset.functionName( fieldName ANY TYPE, parameter ANY TYPE)
as
(SELECT *
    FROM `dataset.table` 
    WHERE  format("%t",fieldName ) = parameter 
)

Later call the function as

SELECT * 
from dataset.functionName( 'passed_qa', 'yes')

(passed_qa is a column name and assume it only has 'yes' and 'no' value)

I tried using EXECUTE IMMEDIATE, it works, but I just want to know if there's a way to approach this in a functional way.

Thanks for any help!

CodePudding user response:

Sorry, it is not possible to access a field by their name given by a parameter in a function. enter image description here

Then try below

select * 
from dataset.functionName('division_code', '0')     

with output

enter image description here

  • Related