Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Export / Import Sequences

What is the best method of exporting SQL Server 2012 Sequence objects from one instance to another instance? Is there something similar to the bcp utility that is used for table data?

We have several hundred to transfer. I need to maintain the next value since we are moving table data as well.

like image 366
Simie Avatar asked Nov 16 '25 18:11

Simie


1 Answers

One possibility would be to have your system create SQL scripts that you can then execute on the other system - something like this:

SELECT 
    'CREATE SEQUENCE ' + seq.name + ' AS ' + t.name +
    ' START WITH ' + CAST(seq.current_value AS VARCHAR(20)) + 
    ' INCREMENT BY ' + CAST(seq.increment AS VARCHAR(10))
FROM 
    sys.sequences seq
INNER JOIN 
    sys.types t ON seq.system_type_id = t.system_type_id

Running this SQL on a given database produces an output that contains CREATE SEQUENCE statement to re-create the sequences, using the current_value as the starting point, and using the defined Increment By value.

Of course, if you need additional options like CYCLE or NO CYCLE etc. - everything that's contained in the sys.sequences catalog view can be scripted out as needed :-)

like image 136
marc_s Avatar answered Nov 18 '25 19:11

marc_s