テーブルを持つ:Table1
列が null 許容値を受け入れる場合、最大 2 回複製できるCode
コードで始まるコードを除いて、値が null 非許容値に対して一意であることをどのように保証できますか?'A'
表1
Id | Code
----------
1 | NULL --[ok]
2 | A123 --[ok]
3 | A123 --[ok]
4 | B100 --[ok]
5 | C200 --[ok]
6 | B100 --[not ok already used]
7 | NULL --[ok]
私が試したのは、インデックス付きビューを作成することです。ソリューションはNULL
値に対しては正常に機能しますが、私が言及した2番目のケースでは機能しません(実際にはスキップされました)
Create view v_Table_unq with schemabinding as(
select code from
dbo.Table1
where code is not null and code not like 'A%'
)
go
create unique clustered index unq_code on v_Table_unq(code)
手伝ってくれてありがとう