Snowflake Scripting์˜ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•œ ์˜ˆยถ

์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ๋‹ค๋ฃจ๋Š” ์†”๋ฃจ์…˜์— ๋Œ€ํ•ด Snowflake Scripting ์–ธ์–ด ์š”์†Œ, ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ต๋ช… ๋ธ”๋ก ๋ฐ ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•ญ๋ชฉ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•œ Snowflake Scripting ์ฝ”๋“œ์˜ ์˜ˆ์ œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•œ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธยถ

๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” ์‚ฌ์šฉ์ž ์ž…๋ ฅ์œผ๋กœ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ์‹œ์ €๋Š” FOR ๋ฃจํ”„ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์˜ RESULTSET ์— ์žˆ๋Š” ํ–‰์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. FOR ๋ฃจํ”„์—๋Š” ์กฐ๊ฑด๋ถ€ ๋…ผ๋ฆฌ ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ž…๋ ฅ์— ๊ธฐ๋ฐ˜ํ•œ ๋ฐ”์ธ๋“œ ๋ณ€์ˆ˜ ๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ €์—์„œ ์ˆ˜ํ–‰๋˜๋Š” ์ •ํ™•ํ•œ ์—…๋ฐ์ดํŠธ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ์—์„œ๋Š” ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

CREATE OR REPLACE TABLE bonuses (
  emp_id INT,
  performance_rating INT,
  salary NUMBER(12, 2),
  bonus NUMBER(12, 2)
);

INSERT INTO bonuses (emp_id, performance_rating, salary, bonus) VALUES
  (1001, 3, 100000, NULL),
  (1002, 1, 50000, NULL),
  (1003, 4, 75000, NULL),
  (1004, 4, 80000, NULL),
  (1005, 5, 120000, NULL),
  (1006, 2, 60000, NULL),
  (1007, 5, 40000, NULL),
  (1008, 3, 140000, NULL),
  (1009, 1, 95000, NULL);

SELECT * FROM bonuses;
Copy
+--------+--------------------+-----------+-------+
| EMP_ID | PERFORMANCE_RATING |    SALARY | BONUS |
|--------+--------------------+-----------+-------|
|   1001 |                  3 | 100000.00 |  NULL |
|   1002 |                  1 |  50000.00 |  NULL |
|   1003 |                  4 |  75000.00 |  NULL |
|   1004 |                  4 |  80000.00 |  NULL |
|   1005 |                  5 | 120000.00 |  NULL |
|   1006 |                  2 |  60000.00 |  NULL |
|   1007 |                  5 |  40000.00 |  NULL |
|   1008 |                  3 | 140000.00 |  NULL |
|   1009 |                  1 |  95000.00 |  NULL |
+--------+--------------------+-----------+-------+

๋‹ค์Œ ์ €์žฅ ํ”„๋กœ์‹œ์ €๋Š” FOR ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ bonuses ํ…Œ์ด๋ธ”์˜ RESULTSET์— ์žˆ๋Š” ํ–‰์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ์ €์žฅ ํ”„๋กœ์‹œ์ €๋Š” ์ง€์ •๋œ ์„ฑ๊ณผ ๋“ฑ๊ธ‰์„ ๋ฐ›์€ ๊ฐ ์ง์›์˜ ๊ธ‰์—ฌ์—์„œ ์ง€์ •๋œ ๋น„์œจ๋กœ ๋ณด๋„ˆ์Šค๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ €์žฅ ํ”„๋กœ์‹œ์ €๋Š” ์กฐ๊ฑด ๋…ผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ ์„ฑ๊ณผ ๋“ฑ๊ธ‰์ด ์žˆ๋Š” ์ง์›์—๊ฒŒ๋งŒ ๋ณด๋„ˆ์Šค๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž…๋ ฅ(bonus_percentage ๋ฐ performance_value)์„ ๋ฐ”์ธ๋“œ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

CREATE OR REPLACE PROCEDURE apply_bonus(bonus_percentage INT, performance_value INT)
  RETURNS TEXT
  LANGUAGE SQL
AS
DECLARE
  -- Use input to calculate the bonus percentage
  updated_bonus_percentage NUMBER(2,2) DEFAULT (:bonus_percentage/100);
  --  Declare a result set
  rs RESULTSET;
BEGIN
  -- Assign a query to the result set and execute the query
  rs := (SELECT * FROM bonuses);
  -- Use a FOR loop to iterate over the records in the result set
  FOR record IN rs DO
    -- Assign variable values using values in the current record
    LET emp_id_value INT := record.emp_id;
    LET performance_rating_value INT := record.performance_rating;
    LET salary_value NUMBER(12, 2) := record.salary;
    -- Determine whether the performance rating in the record matches the user input
    IF (performance_rating_value = :performance_value) THEN
      -- If the condition is met, update the bonuses table using the calculated bonus percentage
      UPDATE bonuses SET bonus = ( :salary_value * :updated_bonus_percentage )
        WHERE emp_id = :emp_id_value;
    END IF;
  END FOR;
  -- Return text when the stored procedure completes
  RETURN 'Update applied';
END;
Copy

์ฐธ๊ณ : Python Connector ์ฝ”๋“œ์—์„œ Snowflake CLI, SnowSQL, Classic Console, ๋˜๋Š” execute_stream ๋˜๋Š” execute_string ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ด ์˜ˆ์ œ๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค(Snowflake CLI, SnowSQL, Classic Console ๋ฐ Python Connector์—์„œ Snowflake Scripting ์‚ฌ์šฉํ•˜๊ธฐ ์ฐธ์กฐ).

CREATE OR REPLACE PROCEDURE apply_bonus(bonus_percentage INT, performance_value INT)
  RETURNS TEXT
  LANGUAGE SQL
AS
$$
DECLARE
  -- Use input to calculate the bonus percentage
  updated_bonus_percentage NUMBER(2,2) DEFAULT (:bonus_percentage/100);
  --  Declare a result set
  rs RESULTSET;
BEGIN
  -- Assign a query to the result set and execute the query
  rs := (SELECT * FROM bonuses);
  -- Use a FOR loop to iterate over the records in the result set
  FOR record IN rs DO
    -- Assign variable values using values in the current record
    LET emp_id_value INT := record.emp_id;
    LET performance_rating_value INT := record.performance_rating;
    LET salary_value NUMBER(12, 2) := record.salary;
    -- Determine whether the performance rating in the record matches the user input
    IF (performance_rating_value = :performance_value) THEN
      -- If the condition is met, update the bonuses table using the calculated bonus percentage
      UPDATE bonuses SET bonus = ( :salary_value * :updated_bonus_percentage )
        WHERE emp_id = :emp_id_value;
    END IF;
  END FOR;
  -- Return text when the stored procedure completes
  RETURN 'Update applied';
END;
$$
;
Copy

์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ณด๋„ˆ์Šค ๋น„์œจ๊ณผ ์„ฑ๊ณผ ๋“ฑ๊ธ‰์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์„ฑ๊ณผ ํ‰๊ฐ€๊ฐ€ 5์ธ ์ง์›์—๊ฒŒ 3%์˜ ๋ณด๋„ˆ์Šค๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

CALL apply_bonus(3, 5);
Copy

๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

SELECT * FROM bonuses;
Copy
+--------+--------------------+-----------+---------+
| EMP_ID | PERFORMANCE_RATING |    SALARY |   BONUS |
|--------+--------------------+-----------+---------|
|   1001 |                  3 | 100000.00 |    NULL |
|   1002 |                  1 |  50000.00 |    NULL |
|   1003 |                  4 |  75000.00 |    NULL |
|   1004 |                  4 |  80000.00 |    NULL |
|   1005 |                  5 | 120000.00 | 3600.00 |
|   1006 |                  2 |  60000.00 |    NULL |
|   1007 |                  5 |  40000.00 | 1200.00 |
|   1008 |                  3 | 140000.00 |    NULL |
|   1009 |                  1 |  95000.00 |    NULL |
+--------+--------------------+-----------+---------+

๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง ๋ฐ ์ˆ˜์ง‘ยถ

๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๊ณ  ์ˆ˜์ง‘ํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ์‹œ์ €์—์„œ๋Š” ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ํ–‰์„ ์‚ฝ์ž…ํ•˜์—ฌ ๊ณผ๊ฑฐ ์ถ”์„ธ๋ฅผ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ€์ƒ ๋จธ์‹ (VMs)์˜ ์†Œ์œ ๊ถŒ ๋ฐ ์„ค์ •์„ ์ถ”์ ํ•˜๋Š” ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

CREATE OR REPLACE TABLE vm_ownership (
  emp_id INT,
  vm_id VARCHAR
);

INSERT INTO vm_ownership (emp_id, vm_id) VALUES
  (1001, 1),
  (1001, 5),
  (1002, 3),
  (1003, 4),
  (1003, 6),
  (1003, 2);

CREATE OR REPLACE TABLE vm_settings (
  vm_id INT,
  vm_setting VARCHAR,
  value NUMBER
);

INSERT INTO vm_settings (vm_id, vm_setting, value) VALUES
  (1, 's1', 5),
  (1, 's2', 500),
  (2, 's1', 10),
  (2, 's2', 600),
  (3, 's1', 3),
  (3, 's2', 400),
  (4, 's1', 8),
  (4, 's2', 700),
  (5, 's1', 1),
  (5, 's2', 300),
  (6, 's1', 7),
  (6, 's2', 800);

CREATE OR REPLACE TABLE vm_settings_history (
  vm_id INT,
  vm_setting VARCHAR,
  value NUMBER,
  owner INT,
  date DATE
);
Copy

ํšŒ์‚ฌ์—์„œ ์„ค์ • ๊ฐ’์ด ํŠน์ • ์ž„๊ณ„๊ฐ’์„ ์ดˆ๊ณผํ•  ๋•Œ ์ด ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๊ฐ„ ๊ฒฝ๊ณผ์— ๋”ฐ๋ผ ์ถ”์ ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ €์žฅ ํ”„๋กœ์‹œ์ €๋Š” vm_settings ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ํ•„ํ„ฐ๋งํ•œ ํ›„, ๋‹ค์Œ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜๋ฉด vm_settings_history ํ…Œ์ด๋ธ”์— ํ–‰์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

  • ๊ฐ’์ด s1 ์ธ vm_setting ์€ 5 ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

  • ๊ฐ’์ด s2 ์ธ vm_setting ์€ 500 ๋ณด๋‹ค ํฐ ๊ฐ’์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

vm_settings_history ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…๋œ ํ–‰์—๋Š” vm_settings ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์—ด ๊ฐ’๊ณผ ํ•จ๊ป˜ VM์„ ์†Œ์œ ํ•œ ์ง์›์˜ emp_id ๋ฐ ํ˜„์žฌ ๋‚ ์งœ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

CREATE OR REPLACE PROCEDURE vm_user_settings()
  RETURNS VARCHAR
  LANGUAGE SQL
AS
DECLARE
  -- Declare a cursor and a variable
  c1 CURSOR FOR SELECT * FROM vm_settings;
  current_owner NUMBER;
BEGIN
  -- Open the cursor to execute the query and retrieve the rows into the cursor
  OPEN c1;
  -- Use a FOR loop to iterate over the records in the result set
  FOR record IN c1 DO
    -- Assign variable values using values in the current record
    LET current_vm_id NUMBER := record.vm_id;
    LET current_vm_setting VARCHAR := record.vm_setting;
    LET current_value NUMBER := record.value;
    -- Assign a value to the current_owner variable by querying the vm_ownership table
    SELECT emp_id INTO :current_owner
      FROM vm_ownership
      WHERE vm_id = :current_vm_id;
    -- If the record has a vm_setting equal to 's1', determine whether its value is less than 5
    IF (current_vm_setting = 's1' AND current_value < 5) THEN
      -- If the condition is met, insert a row into the vm_settings_history table
      INSERT INTO vm_settings_history VALUES (
        :current_vm_id,
        :current_vm_setting,
        :current_value,
        :current_owner,
        SYSDATE());
    -- If the record has a vm_setting equal to 's2', determine whether its value is greater than 500
    ELSEIF (current_vm_setting = 's2' AND current_value > 500) THEN
      -- If the condition is met, insert a row into the vm_settings_history table
      INSERT INTO vm_settings_history VALUES (
        :current_vm_id,
        :current_vm_setting,
        :current_value,
        :current_owner,
        SYSDATE());
    END IF;
  END FOR;
  -- Close the cursor
  CLOSE c1;
  -- Return text when the stored procedure completes
  RETURN 'Success';
END;
Copy

์ฐธ๊ณ : Python Connector ์ฝ”๋“œ์—์„œ Snowflake CLI, SnowSQL, Classic Console, ๋˜๋Š” execute_stream ๋˜๋Š” execute_string ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ด ์˜ˆ์ œ๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค(Snowflake CLI, SnowSQL, Classic Console ๋ฐ Python Connector์—์„œ Snowflake Scripting ์‚ฌ์šฉํ•˜๊ธฐ ์ฐธ์กฐ).

CREATE OR REPLACE PROCEDURE vm_user_settings()
  RETURNS VARCHAR
  LANGUAGE SQL
AS
$$
DECLARE
  -- Declare a cursor and a variable
  c1 CURSOR FOR SELECT * FROM vm_settings;
  current_owner NUMBER;
BEGIN
  -- Open the cursor to execute the query and retrieve the rows into the cursor
  OPEN c1;
  -- Use a FOR loop to iterate over the records in the result set
  FOR record IN c1 DO
    -- Assign variable values using values in the current record
    LET current_vm_id NUMBER := record.vm_id;
    LET current_vm_setting VARCHAR := record.vm_setting;
    LET current_value NUMBER := record.value;
    -- Assign a value to the current_owner variable by querying the vm_ownership table
    SELECT emp_id INTO :current_owner
      FROM vm_ownership
      WHERE vm_id = :current_vm_id;
    -- If the record has a vm_setting equal to 's1', determine whether its value is less than 5
    IF (current_vm_setting = 's1' AND current_value < 5) THEN
      -- If the condition is met, insert a row into the vm_settings_history table
      INSERT INTO vm_settings_history VALUES (
        :current_vm_id,
        :current_vm_setting,
        :current_value,
        :current_owner,
        SYSDATE());
    -- If the record has a vm_setting equal to 's2', determine whether its value is greater than 500
    ELSEIF (current_vm_setting = 's2' AND current_value > 500) THEN
      -- If the condition is met, insert a row into the vm_settings_history table
      INSERT INTO vm_settings_history VALUES (
        :current_vm_id,
        :current_vm_setting,
        :current_value,
        :current_owner,
        SYSDATE());
    END IF;
  END FOR;
  -- Close the cursor
  CLOSE c1;
  -- Return text when the stored procedure completes
  RETURN 'Success';
END;
$$;
Copy

์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

CALL vm_user_settings();
Copy

๋‹ค์Œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ”„๋กœ์‹œ์ €๊ฐ€ vm_settings_history ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT * FROM vm_settings_history ORDER BY vm_id;
Copy
+-------+------------+-------+-------+------------+
| VM_ID | VM_SETTING | VALUE | OWNER | DATE       |
|-------+------------+-------+-------+------------|
|     2 | s2         |   600 |  1003 | 2024-04-01 |
|     3 | s1         |     3 |  1002 | 2024-04-01 |
|     4 | s2         |   700 |  1003 | 2024-04-01 |
|     5 | s1         |     1 |  1001 | 2024-04-01 |
|     6 | s2         |   800 |  1003 | 2024-04-01 |
+-------+------------+-------+-------+------------+