版權聲明:本文爲博主原創文章,轉載請註明出處。 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設置默認
select case
when 字段名
is null then 默認值
else 字段名
end
sql語句當日0點
select cast(convert(varchar(10),getdate(),120)+' 00:00:00' as datetime)
數字轉字符
convert(int,"0001")
格式化字符串的一中方式如:1變成00001
select right(cast(100000+1 as varchar),5)//截取1000001的右邊5位,其中1爲你自己的任意數(可替換你選出來的數據),這種情況適應於最多5位數如99999的情況