Tôi có một cái gì đó như thế nàytồn tính cột với subquery
create function Answers_Index(@id int, @questionID int)
returns int
as begin
return (select count([ID]) from [Answers] where [ID] < @id and [ID_Question] = @questionID)
end
go
create table Answers
(
[ID] int not null identity(1, 1),
[ID_Question] int not null,
[Text] nvarchar(100) not null,
[Index] as [dbo].[Answers_Index]([ID], [ID_Question]),
)
go
insert into Answers ([ID_Question], [Text]) values
(1, '1: first'),
(2, '2: first'),
(1, '1: second'),
(2, '2: second'),
(2, '2: third')
select * from [Answers]
Những công trình vĩ đại, tuy nhiên nó có xu hướng chậm các truy vấn khá một chút. Làm thế nào tôi có thể làm cho cột Index
tiếp tục tồn tại? Tôi đã thử sau:
create table Answers
(
[ID] int not null identity(1, 1),
[ID_Question] int not null,
[Text] nvarchar(100) not null,
)
go
create function Answers_Index(@id int, @questionID int)
returns int
with schemabinding
as begin
return (select count([ID]) from [dbo].[Answers] where [ID] < @id and [ID_Question] = @questionID)
end
go
alter table Answers add [Index] as [dbo].[Answers_Index]([ID], [ID_Question]) persisted
go
insert into Answers ([ID_Question], [Text]) values
(1, '1: first'),
(2, '2: first'),
(1, '1: second'),
(2, '2: second'),
(2, '2: third')
select * from [Answers]
Nhưng đó ném lỗi sau: Computed column 'Index' in table 'Answers' cannot be persisted because the column does user or system data access.
Hoặc nên tôi chỉ quên nó đi và sử dụng [Index] int not null default(0)
và điền nó trong on insert
kích hoạt?
chỉnh sửa: cảm ơn bạn, giải pháp cuối cùng:
create trigger [TRG_Answers_Insert]
on [Answers]
for insert, update
as
update [Answers] set [Index] = (select count([ID]) from [Answers] where [ID] < a.[ID] and [ID_Question] = a.[ID_Question])
from [Answers] a
inner join [inserted] i on a.ID = i.ID
go
Thành thật mà nói, tôi không hoàn toàn chắc chắn tôi hiểu những gì bạn đang cố gắng để giải quyết - là chọn truy vấn chậm Nó không chạm vào cột "chỉ mục" của bạn, vì vậy tôi không thấy mức độ liên quan - mặc dù bạn có thể muốn thêm một hoặc hai chỉ mục ... –