English 中文(简体)
@ 立即使用
原标题:@@identity immediate use

我有这份千年发展倡议声明:

<insert id="insertValue" resultType="car" > 
   BEGIN TRANSACTION
      INSERT INTO table1 ( somefield,... ) VALUES ( #{value},... ); 

      <selectKey keyProperty="carId" resultClass="int"> 
         SELECT @@IDENTITY as carId 
      </selectKey>

      UPDATE table2
      set fieldx = #{carId}
      where table2Id = 500;
   COMMIT
</insert>

我正在拿到零件,应该成为表1中刚刚增加的记录的关键。

我如何抓住最近产生的供立即使用的钥匙?

最佳回答

我认为,你想要做的是:

insert table1 ......
declare @var int
set @var= scope_identity()
update table2 set ... where id = @var
问题回答

暂无回答




相关问题
Compound IDENTITY column in SQL SERVER 2008

An Orders table has a CustomerId column and an OrderId column. For certain reasons it s important that an order s id is no longer than 2-bytes. There will be several million orders in total, which ...

SQL Identity column increases by 2 or 3

I have a new clustered database and I ve been moving data into the tables from our old database. When I import the data, everything works fine, but when I manually insert a record the Identity column ...

Identity Column as Primary Key

Could you please opine if having identity column as primary key is a good practise? For ORM tools, having identity column on tables helps. But there are other side effects such as accidental duplicate ...

Is a guid as identity field better in domain-driven design?

Is it easier to implement domain-driven design when using guids as identity fields instead of auto incrementing integers? With guids you don t have to jump to the database to get the actual value.

热门标签