版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/smallbabylong/article/details/79008629
sql触发器的建立以及选择数据默认值设置、获取当日0点时间
建立触发器
- 建立触发器
//触发器存在则删除
if (object_id('T_addNotice', 'tr') is not null)
drop trigger T_addNotice
go
create trigger T_addNotice
on notice
for insert --插入触发可选delete,update
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的情况