Answer the question
In order to leave comments, you need to log in
How to insert a value into a table and immediately use identy in the adjacent column?
Have a nice day, everyone!
How can inserting a row into the table at the same time get the id of the record to use it in the adjacent column?
For example,
CREATE TABLE [dbo].[Product]([Id] [int] IDENTITY(1,1) NOT NULL,[Name] [nvarchar](50) NOT NULL)
insert into dbo.Product (Name) values ('Новый продукт_')
insert into dbo.Product (Name) values ('Новый продукт_' + cast(IDENT_CURRENT('dbo.Product') as varchar(5)))
Answer the question
In order to leave comments, you need to log in
CREATE TABLE [dbo].[Product](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[NamePower] AS (([Name]+'_')+CONVERT([varchar],[ID])),
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
in the Name field, id should be attached at the end of the nameViolation of the normal form.
can there be solutions in this case?With a calculated field, you can cast everything to the same form using the CASE statement.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question