简体   繁体   中英

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 ?

在此处输入图片说明

Try to concate the two column values and Use UPDATE statement to update the value in the related column.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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