如何在SQL Server中设置序列的'start with'来选择查询结果?

时间:2021-09-02 23:32:19

I am trying to do something like in SQL server 2012

我正在尝试像在SQL Server 2012中那样做

CREATE SEQUENCE item_seq
  START WITH (SELECT MAX(i_item_sk)
     FROM item) 
  INCREMENT BY 1;

Is it possible? What are the other ways if this is not possible? Can we do it like how we do it in PostgreSQL(given below)?

可能吗?如果不可能,还有哪些其他方法?我们可以像在PostgreSQL中一样(如下所示)吗?

create sequence item_seq 
select setval('item_seq', (select max(i_item_sk)+1 from item), false);

I would be further using this sequence variable in Kettle 'Add sequence' step.

我将在Kettle'添加序列'步骤中进一步使用此序列变量。

1 个解决方案

#1


5  

It does not look like you can declare a variable amount in the syntax. However, you can wrap it in an EXEC statement, like so:

看起来您不能在语法中声明可变数量。但是,您可以将其包装在EXEC语句中,如下所示:

DECLARE @max int;
SELECT @max = MAX(i_item_sk)
     FROM item

exec('CREATE SEQUENCE item_seq 
    START WITH ' + @max +
'   INCREMENT BY 1;')

select * from sys.sequences

#1


5  

It does not look like you can declare a variable amount in the syntax. However, you can wrap it in an EXEC statement, like so:

看起来您不能在语法中声明可变数量。但是,您可以将其包装在EXEC语句中,如下所示:

DECLARE @max int;
SELECT @max = MAX(i_item_sk)
     FROM item

exec('CREATE SEQUENCE item_seq 
    START WITH ' + @max +
'   INCREMENT BY 1;')

select * from sys.sequences