开发者

Probleme with id increment

开发者 https://www.devze.com 2022-12-31 08:07 出处:网络
when i do this request i have an error INSERT INTO FR_METIERPUBLI( D_NIDMTR, D_NI开发者_JAVA技巧DPUBLI

when i do this request i have an error

INSERT INTO FR_METIERPUBLI(
D_NIDMTR,
D_NI开发者_JAVA技巧DPUBLI
)
VALUES (
'SELECT MAX( D_NIDMTR ) FROM FR_METIERPUBLI + 1', 1000

i want to increment my id


try

INSERT INTO FR_METIERPUBLI(
D_NIDMTR,
D_NIDPUBLI)
SELECT MAX( D_NIDMTR ) +1, 1000 FROM FR_METIERPUBLI

However..be very careful with this..if 2 operations do this at the same time you will get a duplicate

you could do (on SQL Server at least) wrap it in a transaction and specify these locks

 INSERT INTO FR_METIERPUBLI(
    D_NIDMTR,
    D_NIDPUBLI)
    SELECT MAX( D_NIDMTR ) +1, 1000 FROM FR_METIERPUBLI with (UPDLOCK, HOLDLOCK)

Why don't you use a sequence or identity?


without knowing the database, this is just a guess, but try this:

INSERT INTO FR_METIERPUBLI
        (D_NIDMTR,D_NIDPUBLI)
    SELECT 
        MAX( D_NIDMTR )+ 1, 1000
        FROM FR_METIERPUBLI

for SQL Server, try protecting against no rows existing by using this:

INSERT INTO FR_METIERPUBLI
        (D_NIDMTR,D_NIDPUBLI)
    SELECT 
        ISNULL(MAX(D_NIDMTR),0)+ 1, 1000
        FROM FR_METIERPUBLI
0

精彩评论

暂无评论...
验证码 换一张
取 消