A common functionality many developers need at one time or another is to get the ID of the row they just inserted into a database. When the database's primary key is a number automatically generated by the database, it can be difficult.
Different database packages use different methods.
Microsoft Access
With Access's AutoNumber columns, the only method to use is to use two queries, one to insert the row and another to pull the maximum value of the primary key in that table. Both queries should be within a tag to ensure the returned value belongs to the row just inserted:
INSERT INTO Table ...
valueS ...
SELECT MAX(ID_Field) AS ThisID
FROM Table
SQL Server
If you are using SQL Server and its Identity column, you can perform the insert and ID retrieval in just one query:
SET NOCOUNT ON
INSERT INTO Table ...
valueS ...
SELECT ThisID = @@Identity
SET NOCOUNT OFF
NOCOUNT is set to ON to reduce network traffic during the multiple queries, then set back to OFF when complete. @@Identity contains the value of the last inserted ID.
Oracle
Oracle doesn't have an AutoNumber or Identity column like the Microsoft databases and instead uses Sequences. A sequence must first be created for a table before it can be accessed. After you create the sequence, you'll need a query to obtain the next value before the actual insert. You can use that value in the insert itself. So unlike Access and SQL Server, you must specify the value of the ID in the insert query.
SELECT Table_Sequence.NextVal AS ThisID
FROM Dual
INSERT INTO Table (ID, ...)
valueS (#LastID#, ...)
Different database packages use different methods.
Microsoft Access
With Access's AutoNumber columns, the only method to use is to use two queries, one to insert the row and another to pull the maximum value of the primary key in that table. Both queries should be within a tag to ensure the returned value belongs to the row just inserted:
INSERT INTO Table ...
valueS ...
SELECT MAX(ID_Field) AS ThisID
FROM Table
SQL Server
If you are using SQL Server and its Identity column, you can perform the insert and ID retrieval in just one query:
SET NOCOUNT ON
INSERT INTO Table ...
valueS ...
SELECT ThisID = @@Identity
SET NOCOUNT OFF
NOCOUNT is set to ON to reduce network traffic during the multiple queries, then set back to OFF when complete. @@Identity contains the value of the last inserted ID.
Oracle
Oracle doesn't have an AutoNumber or Identity column like the Microsoft databases and instead uses Sequences. A sequence must first be created for a table before it can be accessed. After you create the sequence, you'll need a query to obtain the next value before the actual insert. You can use that value in the insert itself. So unlike Access and SQL Server, you must specify the value of the ID in the insert query.
SELECT Table_Sequence.NextVal AS ThisID
FROM Dual
INSERT INTO Table (ID, ...)
valueS (#LastID#, ...)
开发者常需获取刚插入数据库行的ID,当主键为数据库自动生成的数字时较难实现。不同数据库有不同方法,如Microsoft Access需用两个查询,SQL Server可在一个查询中完成,Oracle则需先创建序列并获取下一个值再插入。
2662

被折叠的 条评论
为什么被折叠?



