我不知道我想做的事情是否可能,我只是好奇。
我有一个用户定义的类型,比如 MyType,它是一个 TINYINT 以及一个规定允许的值必须在 0 到 3 之间的规则。
-- Existing UDT & Rule which work
CREATE TYPE [MySchema].[MyTypes]
FROM [TINYINT]
CREATE RULE [MySchema].[MyTypes_Rule] AS
@Range BETWEEN 0 AND 3
sp_bindrule 'MySchema.MyTypes_Rule', 'MySchema.MyTypes'
我想知道的是是否可以添加一个额外的规则/功能,这将允许我创建一个额外的规则,该规则将采用 NVARCHAR 值,如果它在该范围内,则将其转换为适当的 TINYINT 值。
CREATE RULE [MySchema].[MyTypes_NVARCHAR_Rule1] AS
@InValues IN (N'N', N'A', N'B', N'C')
CREATE RULE [MySchema].[MyTypes_NVARCHAR_Rule1] AS
@InValues IN (N'No Choice', N'Choice A', N'Choice B', N'Choice C')
然后进行某种类型的转换,从“Choice A”或“A”转换为 1,“Choice B”或“B”转换为 2,等等等等。
下面的脚本类似于我想要将字符串转换为允许值的功能。
CREATE TABLE [MyTable]
( [MyValue] BIT,
[Description] NVARCHAR(20) )
GO
INSERT INTO [MyTable]
( [MyValue], [Description] )
SELECT 0, 'Enterered as 0' -- false
UNION
SELECT 1, 'Enterered as 1' -- true
UNION
SELECT CAST(0 AS BIT), 'Enterered as CAST(0 AS BIT)' -- false
UNION
SELECT CAST(1 AS BIT), 'Enterered as CAST(1 AS BIT)' -- true
UNION
SELECT CAST('false' AS BIT), 'Enterered as CAST(''false'' AS BIT)' -- false
UNION
SELECT CAST('true' AS BIT), 'Enterered as CAST(''true'' AS BIT)' -- true