我需要创建这样的自定义约束逻辑在一段时间内没有重复的组合
CREATE FUNCTION [dbo].[CheckPriceListDuplicates](
@priceListId uniqueidentifier,
@supplierId uniqueidentifier,
@transportModeId uniqueidentifier,
@currencyId uniqueidentifier,
@departmentTypeId uniqueidentifier,
@consolidationModeId uniqueidentifier,
@importerId uniqueidentifier,
@exporterId uniqueidentifier,
@validFrom datetimeoffset(7),
@validTo datetimeoffset(7))
RETURNS int
AS
BEGIN
DECLARE @result int
IF EXISTS (SELECT * FROM [dbo].[PriceListEntries] AS [Extent1]
WHERE ([Extent1].[Id] <> @priceListId) AND
((([Extent1].[SupplierAddressBook_Id] IS NULL) AND (@supplierId IS NULL)) OR ([Extent1].[SupplierAddressBook_Id] = @supplierId)) AND
([Extent1].[TransportMode_Id] = @transportModeId) AND
([Extent1].[Currency_Id] = @currencyId) AND
([Extent1].[DepartmentType_Id] = @departmentTypeId) AND
((([Extent1].[ConsolidationMode_Id] IS NULL) AND (@consolidationModeId IS NULL)) OR ([Extent1].[ConsolidationMode_Id] = @consolidationModeId)) AND
((([Extent1].[Importer_Id] IS NULL) AND (@importerId IS NULL)) OR ([Extent1].[Importer_Id] = @importerId)) AND
((([Extent1].[Exporter_Id] IS NULL) AND (@exporterId IS NULL)) OR ([Extent1].[Exporter_Id] = @exporterId)) AND
((@validFrom >= [Extent1].[ValidFrom]) OR (@validTo <= [Extent1].[ValidTo]))
)
BEGIN
SET @result = 0
END
ELSE
BEGIN
SET @result = 1
END
RETURN @result
END
ALTER TABLE [dbo].[PriceListEntries]
ADD CONSTRAINT UniquCombinations CHECK ([dbo].[CheckPriceListDuplicates](
Id,
SupplierAddressBook_Id,
TransportMode_Id,
Currency_Id,
DepartmentType_Id,
ConsolidationMode_Id,
Importer_Id,
Exporter_Id,
ValidFrom,
ValidTo) = 1)
任何想法如何处理out函数?
答案 0 :(得分:0)
查询表中多个列并查看它们是否全部(一起)唯一的一种方法是将测试数据连接在一起作为字符串。
Select CONCAT ( Col1, Col2, Col3) AS ConcateString
from [TABLE_NAME]
WHERE ConcateString = 'All_of_your_data_in_one_string';
https://msdn.microsoft.com/en-us/library/hh231515.aspx
如果结果产生多个结果,则数据组合不是唯一的。