创建规则以限制sql server中表中的特殊字符

我想创建一个规则来限制将特殊字符输入到列中.我尝试了以下方法.但是没用.

I want to create a rule to restrict special characters to be entered into a column. I have tried the following. But it didnt work.

CREATE RULE rule_spchar
AS
@make LIKE '%[^[^*|":<>[]{}`( );@&$]+$]%'

我不知道我在这里做错了什么.任何帮助将不胜感激.

I dont know what I am doing wrong here. Any help would be appreciated.

推荐答案

你可以在这个列上创建一个Check Constraint,并且只允许NumbersAlphabets 插入此列,见下文:

Your can create a Check Constraint on this column and only allow Numbersand Alphabets to be inserted in this column, see below:

ALTER TABLE Table_Name 
ADD CONSTRAINT ck_No_Special_Characters 
       CHECK (Column_Name NOT LIKE '%[^A-Z0-9]%') 

检查约束以仅允许数字

ALTER TABLE Table_Name 
ADD CONSTRAINT ck_Only_Numbers 
       CHECK (Column_Name NOT LIKE '%[^0-9]%') 

检查约束以仅允许字母

ALTER TABLE Table_Name 
ADD CONSTRAINT ck_Only_Alphabets 
       CHECK (Column_Name NOT LIKE '%[^A-Z]%') 

相关文章