Hi @smoortema ,
Thank you for reaching out! You are very close to getting the “start_version”; you just need to include “INTO start_version” after the “EXECUTE IMMEDIATE”. Here is the updated code
BEGIN
DECLARE sum INT DEFAULT 0;
DECLARE start_version INT;
DECLARE sql STRING;
testCycle: FOR SELECT * FROM range(1, 2) DO
SET sql = 'SELECT 5';
EXECUTE IMMEDIATE sql iNTO start_version;
SELECT start_version;
END FOR testCycle;
END;
Now, If I understand correctly, the idea is to leverage a widget within a notebook that holds a table name. Then use this table name to get a specific version, run transformations, and then restore it again.
First, we define our widget
dbutils.widgets.text(
name='table_name',
defaultValue='',
)
Then declare a cursor, build your sql while leveraging the widget's value using ":table_name" to get latest or first version of your table, here is a starter script:
%sql
BEGIN
-- Declare variables if needed
DECLARE sum INT DEFAULT 0;
DECLARE table_version INT DEFAULT 0;
DECLARE dynamic_sql STRING;
-- Declare a cursor
testCycle: FOR row AS
SELECT num FROM range(1, 10) AS t(num)
DO
-- Reference columns using the cursor variable (here 'row')
SET sum = sum + row.num;
-- Get Specific Table Version
SET dynamic_sql = CONCAT('(SELECT version FROM (DESCRIBE HISTORY ',:table_name,' ) ORDER BY version LIMIT 1)');
EXECUTE IMMEDIATE dynamic_sql INTO table_version;
-- Transformations Section Here
-- Restore table using another sql string variable Here
END FOR testCycle;
VALUES (sum, table_version, :table_name);
END;
I hope this helps! And if this solution works for you, I would appreciate it if you could please click the "Accept as Solution" button.
Thank you!