SQL Server默认值:为什么有一个或两个括号?

dee*_*hao 14 t-sql sql-server

如果您运行此脚本以检索数据库中的所有默认值定义:

select 
    c.name as columnname, t.name as tablename, 
    d.definition as value, d.name as constraintname
from
    sys.default_constraints d
    join sys.columns c
        on d.parent_column_id = c.column_id
        and d.parent_object_id = c.object_id
    join sys.tables t
        on c.object_id = t.object_id
Run Code Online (Sandbox Code Playgroud)

你会得到很多默认值,如:

(getdate())
((0))
('')
('2099-12-31')
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么有括号?他们有必要吗?为什么有些值有一对,有些有两个?在脚本编写T-SQL时是否完全遵循计数?

gbn*_*gbn 8

这就是SQL在内部存储它们的方式.

除了由于某种原因何时IN扩展之外,不需要它们(.. OR ...).

您应该尝试使用AND和OR进行检查约束.勋爵.


nei*_*zan 6

这个答案gbn 的答案就足够了。然而,FWIW,我注意到一种模式,至少在我的数据库中,SQL Server 将默认值存储在单括号和双括号中。

  • 数字用双括号(在我的例子中都是整数)。
  • 非数字(即字符串和函数)的单括号。
  • 所有定义至少存储在一组括号中。
  • 没有定义存储在超过两组括号中。

虽然这可能不会影响功能等,但作为程序员,知道存在一种模式感觉很好。

下面是一个查询,用于根据这些规则检查数据库中的默认值(它可能并不完美,但它是一个开始)。

-- Find number of distinct defaults. This should match the number of records in following query 
-- to ensure all are accounted for.
select count(*) as NumberOfDistinctConstraints 
from (select distinct [definition] from sys.default_constraints) t
;

-- Find defaults that follow and don't follow the rules.
;with c1 as (
    select [definition] as DefaultValue, 3 as NumberOfParentheses
    from sys.default_constraints dc
    where [definition] like '(((%'

    union

    select [definition], 2
    from sys.default_constraints dc
    where [definition] like '(([^(]%'

    union

    select [definition], 1
    from sys.default_constraints dc
    where [definition] like '([^(]%' --and ([definition] like '(N''%' or [definition] like '(''%')

    union

    select [definition], 0
    from sys.default_constraints dc
    where [definition] like '[^(]%'
)
, c2 as (
    select 
        DefaultValue
        , NumberOfParentheses
        , case
            when 
                NumberOfParentheses >= 3 -- None exists.
                or NumberOfParentheses = 0 -- None exists.
                or (
                    -- Any double parentheses not followed by a digit or negative sign.
                    NumberOfParentheses = 2 
                    and substring(DefaultValue, 3, 1) not like ('[0-9]') 
                    and substring(DefaultValue, 3, 1) not like ('-')
                )
                or (
                    -- Any single parenthesis followed by a digit or negative sign.
                    NumberOfParentheses = 1 
                    and (
                        substring(DefaultValue, 2, 1) like ('[0-9]') 
                        and substring(DefaultValue, 2, 1) like ('-')
                    )
                )
            then
                0
            else 1
        end as FollowsTheRules
    from c1
)
select *
from c2
--where FollowsTheRules = 0
order by FollowsTheRules asc, NumberOfParentheses desc, DefaultValue asc
;
Run Code Online (Sandbox Code Playgroud)

我检查了一些数据库,这些规则仍然成立。但是,我很想看看其他人是否看到相同的结果。