sql触发器的建立以及选择数据默认值设置、获取当日0点时间

sql触发器的建立以及选择数据默认值设置、获取当日0点时间

建立触发器

  • 建立触发器
//触发器存在则删除
 if (object_id('T_addNotice', 'tr') is not null)
    drop trigger T_addNotice
go
create trigger T_addNotice 
on notice  
for insert   --插入触发可选deleteupdate
as
---处理代码
   update notice set
   --获取序列号如果当日获取结果为空则值置1否则加1
    SerialNum=
(select case
 when (select SerialNum from notice where AddTime=(select max(AddTime) from notice where AddTime>(select cast(convert(varchar(10),getdate(),120)+' 00:00:00' as datetime)) and ExtendedCode is not null and SerialNum is not null))
  is null then 1 
  else 
  (1+(select SerialNum from notice where AddTime=(select max(AddTime) from notice where AddTime>(select cast(convert(varchar(10),getdate(),120)+' 00:00:00' as datetime)) and ExtendedCode is not null and SerialNum is not null))
  )
  end
  ),
  --获取扩展码100000截取后5位位扩展码
    ExtendedCode=(select right(cast(100000+(select ExtendedCode from notice where AddTime=(select max(AddTime) from notice where ExtendedCode is not null and SerialNum is not null))+1 as varchar),5))
 where Id=(select Id from notice where AddTime=(select max(AddTime) from notice));
   go

数据为null设置默认

  • 数据为null设置默认
select case
 when  字段名
  is null then 默认值 
  else  字段名
  end

sql语句当日0点

  • sql语句当日0点
select cast(convert(varchar(10),getdate(),120)+' 00:00:00' as datetime)

数字转字符

  • 数字转字符
convert(int,"0001"// 1

格式化字符串的一中方式如:1变成00001

  • 格式化字符串的一中方式如:1变成00001
select right(cast(100000+1 as varchar),5)//截取1000001的右边5位,其中1为你自己的任意数(可替换你选出来的数据),这种情况适应于最多5位数如99999的情况
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林深人不知

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值