如何获得具有多个插入的惟一ID ?

时间:2021-05-29 16:45:39

I had two text boxes in frond end which is productname and No.of.batches...For example I selected a product 'X' and No.of batches as 10..It will get 10 rows and user will insert the data when user clicks submit it will reflect in database too...My requirement is generate an ID automatically PER transaction...I had to get an unique ID for the whole operation...what should i do to get that?

我在frond端有两个文本框,它是productname和no .批量…例如,我选择了一个产品“X”和“No”。的批次10 . .它将得到10行,当用户单击submit时,用户将插入数据,它也将反映在数据库中……我的要求是每笔交易自动生成一个ID。整个手术过程中,我必须得到唯一的ID……我该怎么做呢?

2 个解决方案

#1


2  

I assume you are talking about financial as opposed to database transactions but the same applies to both.

我假设您谈论的是财务事务,而不是数据库事务,但这两者都适用。

  1. Per prdb's ansswer above, create a table for storing the transaction header information (don't call it transaction since that is a reserved word). Call it something else.
  2. 根据上面prdb的ansswer,创建一个表来存储事务头信息(不要调用它事务,因为它是一个保留字)。称它为别的东西。
  3. Add appropriate foreign keys referencin that table.
  4. 在该表中添加适当的外键。

Then your insert looks like this:

然后插入如下:

  1. insert into the transaction header table and use OUTPUT or another select to get the id out
  2. 插入到事务头表中,并使用输出或另一个select获取id
  3. Insert into the other tables adding the transaction header id as a foreign key as needed.
  4. 插入到其他表中,在需要时添加事务头id作为外键。

Again, that's pretty standard with financial transactions. For db transactions, the same approach could be used.

这也是金融交易的标准。对于db事务,可以使用相同的方法。

#2


2  

Create a new table called transactions with auto generated column.

使用自动生成的列创建一个名为transactions的新表。

Create table transactions 
(
transaction_id int identity(1,1) Primary key,
product_name varchar(50),
No_of_batches int
)

Whenever there is a new transaction then first add a entry in transaction table and refer the auto generated value(transaction_id) in your target table for all the X records

每当有新事务时,首先在事务表中添加一个条目,并引用目标表中为所有X记录生成的自动生成值(transaction_id)

#1


2  

I assume you are talking about financial as opposed to database transactions but the same applies to both.

我假设您谈论的是财务事务,而不是数据库事务,但这两者都适用。

  1. Per prdb's ansswer above, create a table for storing the transaction header information (don't call it transaction since that is a reserved word). Call it something else.
  2. 根据上面prdb的ansswer,创建一个表来存储事务头信息(不要调用它事务,因为它是一个保留字)。称它为别的东西。
  3. Add appropriate foreign keys referencin that table.
  4. 在该表中添加适当的外键。

Then your insert looks like this:

然后插入如下:

  1. insert into the transaction header table and use OUTPUT or another select to get the id out
  2. 插入到事务头表中,并使用输出或另一个select获取id
  3. Insert into the other tables adding the transaction header id as a foreign key as needed.
  4. 插入到其他表中,在需要时添加事务头id作为外键。

Again, that's pretty standard with financial transactions. For db transactions, the same approach could be used.

这也是金融交易的标准。对于db事务,可以使用相同的方法。

#2


2  

Create a new table called transactions with auto generated column.

使用自动生成的列创建一个名为transactions的新表。

Create table transactions 
(
transaction_id int identity(1,1) Primary key,
product_name varchar(50),
No_of_batches int
)

Whenever there is a new transaction then first add a entry in transaction table and refer the auto generated value(transaction_id) in your target table for all the X records

每当有新事务时,首先在事务表中添加一个条目,并引用目标表中为所有X记录生成的自动生成值(transaction_id)