In the following query I am using a stored procedure to create a table output_tbl_name from a source_tbl. The name of the output_tbl_name is timestamped with today's date.
DECLARE
creation_date STRING := to_varchar(current_date(), 'YYYYMMDD');
output_tbl_name STRING := concat('my_database.my_schema.', 'output_', :creation_date);
QUERY STRING;
BEGIN
QUERY:= REPLACE(
'create or replace table <output_tbl_name>(col1 varchar, col2 varchar) as
select * from source_tbl;'
,'<output_tbl_name>', :output_tbl_name); ;
EXECUTE IMMEDIATE :QUERY;
RETURN :QUERY;
END;
However, I would like to set a dynamic criteria for the data that is copied over. E.g., the source_tbl has a date column, and I want to only copy over records for dates > 6 months ago:
declare start_date date := add_months(current_date(), -6)
create or replace output_tbl_name as select * from source_tbl where date > <start_date>
How can I incorporate this into my above query? Placing it directly into my QUERY in my BEGIN statement isn't working. Thanks for your help!