UDF作为雪花中的默认值

时间:2020-08-24 20:41:50

标签: snowflake-cloud-data-platform

我正在尝试使用范围有限的序列作为表中的ID列。目前,snowfalke不支持序列的上限,因此我正在考虑使用UDF来解决此问题:

create or replace sequence seq1 with start = 1 ;

create or replace function seq1_with_max()
returns number
as 
$$
    select case when a.s < 10  then a.s else null end as id  from ( select seq1.nextval as s from dual ) a
$$
;

select seq1_with_max() ;

create or replace table f (
  id number not null default seq1_with_max(),
  c varchar 
) ;

insert into f(c) values ('a') ;

返回

SQL compilation error:    
syntax error line 1 at position 1 unexpected 'SELECT'.
syntax error line 1 at position 12 unexpected 'A'.

我不太明白为什么这行不通。如何修改UDF以实现相同的目标?

1 个答案:

答案 0 :(得分:0)

您是对的。看起来就像雪花创建语句,我们不能使用UDF。我尝试了这个。

create or replace table f (
  id number not null ,
  c varchar 
) ;

insert into f(id,c) select seq1_with_max(),'hello';

select * from f;