2
我不知道我想要做什麼是可能的,但我只是好奇而已。UDT的多種輸入類型
我有發言權的MyType的用戶定義類型是一個TINYINT與規定允許的值必須是我想什麼3,介於0和
-- 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')
,然後做一些類型轉換的從「選擇A」或「A」至1的值,「選擇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