检查规则是否已存在

时间:2017-11-13 14:15:19

标签: sql sql-server tsql

为用户定义的数据类型创建规则时,如何检查规则是否已存在?

-- does rule exist?
CREATE RULE MyRule
AS @MyVar IN ('foo1', 'foo2')

1 个答案:

答案 0 :(得分:1)

if not  exists(Select 1 from sys.all_objects where type='r')
begin

create rule syntax

end

您也可以使用sys.objects

 if not  exists(Select 1 from sys.objects where type='r')
    begin

    create rule syntax

    end

<强>参考文献:
https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-all-objects-transact-sql