I am trying to do something like in 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)?
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.
If you want to select the next value from sequence object, you can use this SQL statement. If you want to select multiple next values from SQL Sequence, you have to loop calling the above SQL statement and save the "next value" got in a storage. You can loop using (while loop) or by (cursor).
The syntax to a view the properties of a sequence in SQL Server (Transact-SQL) is: SELECT * FROM sys. sequences WHERE name = 'sequence_name'; sequence_name.
Sequences are integer values and can be of any data type that returns an integer. The data type cannot be changed by using the ALTER SEQUENCE statement. To change the data type, drop and create the sequence object.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With