2

等效声明和设置变量如下。

DECLARE @name varchar(255)
       ,@address varchar(255)
       ,@country varchar(255)

SELECT @name = name
      ,@address = address
      ,@country = country
    FROM tbl_address
    WHERE id =5

将此代码转换为雪花 SP 时,处理声明和从表中设置变量的最佳方法是什么?

在此处输入图像描述

4

1 回答 1

1

可以一次分配多个变量:

CREATE OR REPLACE TABLE tbl_address AS
SELECT 5 AS id, 'a' AS name, 'b' AS address, 'c' AS country;

SET (name, address, country) = (SELECT name, address, country 
                                FROM tbl_address WHERE id = 5);

SELECT $name, $address, $country;
-- a, b, c

要求是用于获取值的 suqbquery 不能返回超过 1 行。

于 2021-08-26T13:50:37.523 回答