admin管理员组文章数量:1122826
I want to set the value of the generator Dest to the actual value of generator Src:
ALTER SEQUENCE Dest RESTART WITH GEN_ID(Src, 0);
This doesn't work:
Invalid token. Dynamic SQL Error. SQL error code = -104. Token unknown
- line 1, column 39. GEN_ID. ---------------------------------- SQLCODE: -104 SQLSTATE: 42000 GDSCODE: 335544569
How to do that?
I want to set the value of the generator Dest to the actual value of generator Src:
ALTER SEQUENCE Dest RESTART WITH GEN_ID(Src, 0);
This doesn't work:
Invalid token. Dynamic SQL Error. SQL error code = -104. Token unknown
- line 1, column 39. GEN_ID. ---------------------------------- SQLCODE: -104 SQLSTATE: 42000 GDSCODE: 335544569
How to do that?
Share Improve this question edited Nov 22, 2024 at 7:54 Mark Rotteveel 109k224 gold badges155 silver badges218 bronze badges asked Nov 21, 2024 at 18:26 TomCat500TomCat500 1744 silver badges14 bronze badges1 Answer
Reset to default 1You can't do that with ALTER SEQUENCE
, because like any other DDL, it only accepts literals, not expressions.
There are two methods you can use:
Use
GEN_ID
to modify the generator to the desired value:select gen_id(dest, gen_id(src, 0) - gen_id(dest, 0)) from RDB$DATABASE
This will change the value of
dest
by the difference between the current values ofsrc
anddest
, which will makedest
equal tosrc
(assuming there are no other changes to eitherdest
orsrc
concurrently).Use PSQL
EXECUTE STATEMENT
(e.g. in anEXECUTE BLOCK
) to execute theALTER SEQUENCE
; although not recommended, you can execute DDL that way with dynamic values. For example:execute block as begin execute statement 'alter sequence dest restart with ' || gen_id(src, 0); end
NOTE: With Firebird 4 or higher, you need to use
(gen_id(src, 0) + 1)
(if the sequence has a different increment, use that value instead of1
), asRESTART WITH
was changed to conform to the SQL standard to set the sequence so the next value generated withNEXT VALUE FOR
is the restart-with value, instead of setting the sequence equal to the restart-with value.
本文标签: sqlSet generator to value of another generatorStack Overflow
版权声明:本文标题:sql - Set generator to value of another generator - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308084a1933536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论