简体   繁体   English

如何在插入前获取自动生成的ID?

[英]How to get auto generated Id before insertion?

I have a table like the below picture, and I want to get IDwithChar column value concatenating the value with image address, and inserting that into ImageCover column at same query, how can I achieve that ? 我有一个如下图所示的表,我想获取IDwithChar列值以将该值与图像地址连接起来,并在同一查询中将其插入ImageCover列,我如何实现呢?

在此处输入图片说明

Try to concate the two column values and Use UPDATE statement to update the value in the related column. 尝试串联两个列的值,并使用UPDATE语句更新相关列中的值。

CREATE TABLE Test (
IdWithChar NVARCHAR(MAX),
ImageAddress NVARCHAR(MAX),
CoverImage NVARCHAR(MAX)
)
INSERT INTO Test VALUES ('B00001','Test Address','Test-Image-Url')

Query: 查询:

UPDATE TEST 
SET CoverImage= IdWithChar +' '+ ImageAddress 
WHERE Your_condition

first you must insert values and get the last identity and then update the field in the record. 首先,您必须插入值并获取最后的身份,然后更新记录中的字段。

insert into item values ('test', 'test')
declare @itemId int =  @@IDENTITY
update item set item_code = @itemId  where item_id = @itemId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM