我有以下约束,应该只允许插入 0 到 9 的数字,而不是任何特殊字符和字母字符。但情况并非如此,例如在使用此更新语句时:
update MyDB.dbo.MyTable
set MyTestPhoneExt = '23&' where ID = 1;
目标是确保源数据和写入 MyTable 的数据只有数字,但 MyTestPhoneExt 字段必须为VARCHAR(15) NULL。
ALTER TABLE MyDB.dbo.MyTable WITH CHECK ADD CONSTRAINT [CHK_MyDB_MyTable _MyTestPhoneExt]
CHECK ((MyTestPhoneExt IS NULL OR LEN(MyTestPhoneExt)>=(1) AND
LEN(MyTestPhoneExt)<=(15) AND MyTestPhoneExt LIKE '%[0-9]%'
--OR len(MyTestPhoneExt)>=(1) AND len(MyTestPhoneExt)<=(15)
AND NOT MyTestPhoneExt LIKE '%[a-zA-Z]%'
AND NOT (MyTestPhoneExt=' ' OR MyTestPhoneExt='' OR MyTestPhoneExt='&' OR
MyTestPhoneExt='`' OR MyTestPhoneExt='~' OR MyTestPhoneExt='>' OR
MyTestPhoneExt='<' OR MyTestPhoneExt='.' OR MyTestPhoneExt=',' OR
MyTestPhoneExt=';' OR MyTestPhoneExt=':' OR MyTestPhoneExt='?' OR
MyTestPhoneExt='_' OR MyTestPhoneExt='=' OR MyTestPhoneExt='+' OR
MyTestPhoneExt='!' OR MyTestPhoneExt='@' OR MyTestPhoneExt='#' OR
MyTestPhoneExt='%' OR MyTestPhoneExt='$' OR MyTestPhoneExt='^' OR
MyTestPhoneExt='*' OR MyTestPhoneExt=',' OR MyTestPhoneExt='}' OR
MyTestPhoneExt='{' OR MyTestPhoneExt=')' OR MyTestPhoneExt='(' OR
MyTestPhoneExt=']' OR MyTestPhoneExt='[' OR MyTestPhoneExt='|' OR
MyTestPhoneExt='\' OR MyTestPhoneExt='/' OR MyTestPhoneExt='-' OR MyTestPhoneExt='@')))