使用绑定规则和默认值替换已弃用的 SQL Server 用户定义类型

发布于 2024-10-16 14:31:24 字数 471 浏览 4 评论 0原文

我们有一个用户定义的数据类型 YesNo,它有一个 char(1) 的别名。该类型具有绑定规则(必须是 Y 或 N)和默认值 (N)。

这样做的目的是,当任何开发团队创建 YesNo 类型的新字段时,规则和默认值会自动绑定到新列。

规则和默认值已被弃用,并且在 SQL Server 的下一个未来版本中不可用,是否有其他方法可以实现相同的功能?

我应该补充一点,我知道我可以使用 CHECK 和 DEFAULT 约束来复制绑定 Rule 和 Defalut 对象的功能,但是这些必须在每次使用该类型时应用,而不是“免费”获得功能' 通过使用具有绑定规则和默认值的 UDT。

这篇文章涉及支持现有应用程序的数据库,而不是新开发的数据库,因此我知道我们对 UDT 的使用并不是最佳的。

我怀疑这个问题的答案是“否”,但是通常当功能被弃用时,通常会有一种替代语法可以用作替换,所以我想提出这个问题,以防有人知道替代方案。

We have a User Defined Data Type of YesNo which has an which is an alias for char(1). The type has a bound Rule (must be Y or N) and a Default (N).

The aim of this is that when any of the development team create a new field of type YesNo the rule and default are automatically bound to the new column.

Rules and Defaults have been deprecated and won't be available in the next a future version of SQL Server, is there another way to achieve the same functionality?

I should add that I'm aware that I could use CHECK and DEFAULT constraints to replicate the functionality of the bound Rule and Defalut objects, however these would have to be applied at each usage of the type, rather than getting the functionality 'for free' by using a UDT which has a bound Rule and Default.

The post relates to a database that backs an existing application, rather than a new development, so I'm aware that our use of UDT's is less than optimal.

I suspect the answer to the question is 'No', however normally when features are deprecated there's usually an alternative syntax that can be used as a drop in replacement so I wanted to pose the question in-case someone knew of an alternative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山人契 2024-10-23 14:31:24

默认和检查约束...

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 for SQL Server 2005。所以,我们被告知 6 年以及 3 个版本之前的

SQL Server 2000。 ..

“规则是一种向后兼容功能,执行一些与检查约束相同的功能。使用 ALTER 或 CREATE TABLE 的 CHECK 关键字创建的 CHECK 约束是首选的标准方式...”

这同样适用于 创建默认值对象而不是约束< /em>

那是11年前的事了

Default and check constraints...

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

Personally, I tend not to use UDTs (last time was SQL Server 6.5 IIRC) because there is no ALTER TYPE in case anything changes...

As for deprecation..

First mentioned in CREATE RULE for SQL Server 2005. So, we were told 6 years and 3 releases ago

For SQL Server 2000...

"Rules, a backward compatibility feature, perform some of the same functions as check constraints. CHECK constraints, created using the CHECK keyword of ALTER or CREATE TABLE, are the preferred, standard way..."

The same applies to CREATE DEFAULT, the object not the constraint

That's 11 years ago

对风讲故事 2024-10-23 14:31:24

“规则和默认值已被弃用,并且在下一版本的 SQL Server 中将不再可用”

1) 据我所知,这不是真的。你不可能突然破坏 99.9% 的 TSQL!

2)此外,即使这是真的(我坚信不是),弃用并不意味着在下一个版本中删除,只是一个功能不应再在新代码中使用。

您有此类公告的官方链接吗?

用户@gbn 似乎认为我正在捍卫已弃用的结构的使用。我不是。

"Rules and Defaults have been deprecated and won't be available in the next version of SQL Server"

1) As far as I know that is not true. You cannot suddenly break 99.9% of TSQL out there!

2) Also, even if it were true (and I strongly believe it is not), deprecated does not mean removed in the next release, simply that a feature should no longer be used in new code.

Do you have an official link to any such announcement?

User @gbn seems to think I am defending the use of deprecated constructs. I am not.

∞觅青森が 2024-10-23 14:31:24

对于如此简单的事情使用 Xquery 有点迟钝。我通常将它用于更复杂的数据输入,但它确实(我认为)回答了您有关替换的问题。以下是我如何将参数实现为类型化 xml,该参数必须为 true 或 false。您可以将其扩展为是/否或小狗/小猫或任何您想要的。

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  

Xquery is a bit obtuse to use for something this simple. I typically use it for more complex data typing, but it does (I think) answer your question regarding a replacement. Here is how I might implement a parameter as typed xml which must be either true or false. You can extend it to be yes/no or puppy/kitten or whatever you'd like.

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  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文