SQL用check约束实现非工作时间不得修改数据

/*非工作时间不得修改数据*/
  
  create table lecky
  (
  lid int primary key identity(1000,1),
  lname varchar(20),
  ldate datetime default getdate()
  --默认值为当前时间
  )
  
  alter table lecky add constraint ck_lecky
  --添加check约束
  check
  (
   (
   (
   (
   (datepart(hour,ldate)>=9) and (datepart(hour,ldate)<12)
   --小时数为上午9点至12点
   )
   or
   (
   (datepart(hour,ldate)=12) and (datepart(minute,ldate)<=30)
   --12点00至12点30
   )
   or
   (
   (datepart(hour,ldate)=8) and (datepart(minute,ldate)>=30)
   --8:30至8:59
   )
   )
   or
   (
   (
   (datepart(hour,ldate)>=14) and (datepart(hour,ldate)<17)
   --同上,为下午的工作时间
   )
   or
   (
   (datepart(hour,ldate)=17) and (datepart(minute,ldate)<=30)
   )
   or
   (
   (datepart(hour,ldate)=13) and (datepart(minute,ldate)>=30)
   )
   )
   )
   and
   (
   (datepart(weekday,ldate)<>1) and (datepart(weekday,ldate)<>7)
   --非周六,日的工作时间
   )
   and
   (
   (
   (datepart(month,ldate)<>5) and (datepart(month,ldate)<>10)
   --除5,10月的工作时间
   )
   or
   (
   (datepart(month,ldate)=5) and (datepart(day,ldate)>=8)
   --5月8日至31日,即5月除黄金周以外的工作时间
   )
   or
   (
   (datepart(month,ldate)=10) and (datepart(day,ldate)>=8)
   )
   )
  )
  
  insert into lecky(lname) values('lecky')
  --插入数据测试,测试时双击屏幕右下角的时间,修改系统时间,日期
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值