Best way to get identity of inserted row?

后端 未结 14 2447
醉梦人生
醉梦人生 2020-11-21 07:06

What is the best way to get IDENTITY of inserted row?

I know about @@IDENTITY and IDENT_CURRENT and SCOPE_IDENTITY

14条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 07:46

    Create a uuid and also insert it to a column. Then you can easily identify your row with the uuid. Thats the only 100% working solution you can implement. All the other solutions are too complicated or are not working in same edge cases. E.g.:

    1) Create row

    INSERT INTO table (uuid, name, street, zip) 
            VALUES ('2f802845-447b-4caa-8783-2086a0a8d437', 'Peter', 'Mainstreet 7', '88888');
    

    2) Get created row

    SELECT * FROM table WHERE uuid='2f802845-447b-4caa-8783-2086a0a8d437';
    

提交回复
热议问题