2011-02-06 44 views
7

我們有一個用戶定義的數據類型爲YesNo,其中有一個是char(1)的別名。該類型具有綁定規則(必須是Y或N)和默認(N)。替換爲具有綁定規則和默認值的不建議使用的SQL Server用戶定義類型

這樣做的目的是,當任何開發團隊創建一個類型爲YesNo的新字段時,規則和默認會自動綁定到新列。

規則和默認值已被棄用,將不會在 下一個 未來版本的SQL Server中可用,是否有另一種方法來實現相同的功能?

我應該補充一點,我知道我可以使用CHECK和DEFAULT約束來複制綁定規則和Defalut對象的功能,但是這些必須應用於每種類型的使用,而不是獲取通過使用具有綁定規則和默認值的UDT功能「免費」。

這篇文章涉及支持現有應用程序而不是新開發的數據庫,所以我意識到我們對UDT的使用並不理想。

我懷疑問題的答案是'不',但是通常情況下,當功能被棄用時,通常會有一種替代語法可以用作替代語句,所以我想提出這個問題以防萬一有人知道替代。

+0

我想你誤讀了這篇文章。不建議使用CREATE RULE和CREATE DEFAULT,而是在CREATE TABLE或ALTER TABLE語句中指定DEFAULT和CHECK規則。 – Bill 2011-02-06 01:10:26

+0

這是我一直在申報的地方。 – 2011-02-06 01:16:57

+0

...默認值爲 – 2011-02-06 09:42:07

回答

2

默認和檢查約束...

CREATE TABLE foo (
    col1 int... 
    YesNo char(1) NOT NULL DEFAULT ('N') 
        CONSTRAINT CK_foo_YesNo CHECK (YesNo IN 'Y', 'N')) 
    col2 ... 
    ) 

就個人而言,我傾向於不使用的UDT(最後一次是SQL Server 6.5中IIRC),因爲在任何情況下,沒有ALTER TYPE變更...

至於棄用..

CREATE RULE首先提到的SQL Server 2005。所以,我們被告知,6年,3分年前發佈

SQL Server 2000 ...

「規則,一種向後兼容性功能,執行一些與檢查約束相同的功能。使用CHECK關鍵字ALTER或CREATE TABLE創建的CHECK約束是首選的標準方法...「

這同樣適用於CREATE DEFAULT,則對象不是約束

那是11年前的

0

「規則和缺省值已過時,在SQL Server的下一個版本將不提供」

1)據我知道這是不是真的。你不能突破99.9%的TSQL! 2)此外,即使它是真實的(我堅信它不是),不推薦在下一個版本中刪除它,只是不應該在新代碼中使用某個功能。

你有任何此類公告的官方鏈接?

User @gbn似乎認爲我正在捍衛不推薦使用的構造。我不是。

0

XQuery是有點遲鈍了,用的東西這麼簡單。我通常用它對於更復雜的數據類型,但它確實(我認爲)回答你關於替換的問題,下面是我如何實現一個類型爲xml的參數,它必須是true或false,可以將它擴展爲yes/no或puppy /小貓或任何你想要的。

if schema_id(N'chamomile') is null 
    execute (N'create schema chamomile'); 
go 
set nocount on; 
go 
/* 
    All content is licensed as [chamomile] (http://www.katherinelightsey.com/#!license/cjlz) and 
    copyright Katherine Elizabeth Lightsey, 1959-2014 (aka; my life), all rights reserved, 
    and as open source under the GNU Affero GPL (http://www.gnu.org/licenses/agpl-3.0.html). 
    --------------------------------------------- 
*/ 
if exists 
    (select xml_collection_id 
    from sys.xml_schema_collections as true_false 
    where true_false.name = 'true_false' 
      and true_false.schema_id = schema_id(N'chamomile')) 
    drop xml schema collection [chamomile].[true_false]; 
go 
/* 
    -- 
    -- License 
    ---------------------------------------------------------------------- 
    Katherine E. Lightsey 
    http://www.katherinelightsey.com 

    All content is copyright Katherine Elizabeth Lightsey, 1959-2014 (aka; my life), all rights reserved, 
    licensed as [chamomile] (http://www.katherinelightsey.com/#!license/cjlz) and copyright Katherine Elizabeth Lightsey, 1959-2014 (aka; my life), all rights reserved, 
    and as open source under the GNU Affero GPL (http://www.gnu.org/licenses/agpl-3.0.html). 

    -- 
    -- to view documentation 
    ----------------------------------------------------------------------------------------------- 
    select objtype 
     , objname 
     , name 
     , value 
    from fn_listextendedproperty (null 
        , 'schema' 
        , 'chamomile' 
        , 'xml schema collection' 
        , 'true_false' 
        , default 
        , default); 
*/ 
create xml schema collection [chamomile].[true_false] as N'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:chamomile="http://www.katherinelightsey.com/" targetNamespace="http://www.katherinelightsey.com/"> 

    <xsd:element name="true_false" type="chamomile:true_false_type" /> 

    <xsd:complexType name="true_false_type"> 
     <xsd:complexContent> 
     <xsd:restriction base="xsd:anyType"> 
      <xsd:attribute name="true_false" type="chamomile:pass_fail_enumeration" default="false" /> 
     </xsd:restriction> 
     </xsd:complexContent> 
    </xsd:complexType> 

    <xsd:simpleType name="pass_fail_enumeration"> 
    <xsd:restriction base="xsd:NMTOKEN"> 
     <xsd:enumeration value="true" /> 
     <xsd:enumeration value="false" /> 
    </xsd:restriction> 
    </xsd:simpleType> 

</xsd:schema>'; 
go 
declare @true_false xml([chamomile].[true_false]) = N'<chamomile:true_false xmlns:chamomile="http://www.katherinelightsey.com/" true_false="true" />'; 
if (select @true_false.value(N'(/*/@true_false)[1]', N'[sysname]')) 
    = N'true' 
    select N'true'; 
go 
declare @true_false xml([chamomile].[true_false]) = N'<chamomile:true_false xmlns:chamomile="http://www.katherinelightsey.com/" true_false="false" />'; 
go 
declare @true_false xml([chamomile].[true_false]) = N'<chamomile:true_false xmlns:chamomile="http://www.katherinelightsey.com/" true_false="not_valid" />'; 
go 
相關問題