Best practices: .NET: How to return PK against an oracle database?

前端 未结 3 1244
不知归路
不知归路 2020-12-30 14:42

With SQLServer, it seems to be generally accepted that adding a SELECT SCOPE_IDENTITY() to the end of your insert is the best way to return the PK of the newly-inserted reco

相关标签:
3条回答
  • 2020-12-30 14:57

    The stored procedure and the returning clause have the distinct benefit of a single database call any other solution is inferior. Whether you do it via a stored procedure or you use a returning clause is a whole can of worms in itself.

    0 讨论(0)
  • 2020-12-30 14:58

    You can use the RETURNING clause to do this in Oracle stored procs.

    For example:

    TABLEA has NAME and EMP_ID. EMP_ID is populated internally when records are inserted.

    INSERT INTO TABLEA(NAME) VALUES ('BOB') RETURNING EMP_ID INTO o_EMP_ID;

    That's assuming that line is in a stored proc with an output parameter of o_EMP_ID.

    Hope that helps... if not, here's a more detailed example:

    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/returninginto_clause.htm

    0 讨论(0)
  • 2020-12-30 14:59

    The RETURNING clause is intended for just this kind of usage, so I would call it a best practice to use it.

    An alternative would be to select seq.CURRVAL after the insert. That returns the last value obtained from the sequence by this session.

    0 讨论(0)
提交回复
热议问题