Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Table User(id identity, name , windowid unique)

we insert one row in table user , and autogenerated id would be 1234.

insert value (shubham, 1234_Shubham)

i want windowid =id+name,

Can we do this in SQL.

What I have tried:

Scope identity, but its for insert newly inserted ID in other table.
Posted
Updated 26-Jul-21 0:00am

As far as I know you cannot do this in a single insert. You would have to insert the row then follow up with an update to the same row once the id is known.

Alternatives:
1. Why bother storing the [windowid] as it is clearly constructed from a combination of the [id] and [name] columns? Just create a unique index on both those columns.

2. Consider using a SEQUENCE instead of an IDENTITY column for your id. E.g. see SQL Sequence vs Identity Column – SQLServerCentral[^]. That way you will know the id before the insert and can construct the other column as part of the same insert
 
Share this answer
 
v2
To add to what CHill60 has suggested, consider using a COMPUTED column which means that is is worked out only when you request it - it takes no space, and can include the IDENTITY value: Specify Computed Columns in a Table - SQL Server | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900