SQL学习(时间,存储过程,触发器)
SQL学习
- 几个操作时间的函数
--datapart 获取时间中的年月日时分秒等部分
select DATEPART(year,current_timestamp);
select DATEPART(DAY,current_timestamp);
select DATEPART(MONTH,current_timestamp); --dateadd 在相应时间上加上年月日时分秒等
select CURRENT_TIMESTAMP,DATEADD(DAY,10,CURRENT_TIMESTAMP);
select DATEADD(month,11,'2001-2-28 12:00:00') as 上帝时刻; --datediff 获取两时间段的差值并换算为时分秒年月日等
select DATEDIFF(month,'2014-8-3','2015-9-10'); --转换函数 转换类型
--cast convert select ''+456;
select ''+cast(456 as varchar);
select ''+CONVERT(varchar,456); --convert时间类型转换 后面的数字即不同地区的时间表示方式
select CURRENT_TIMESTAMP,
CONVERT(VARCHAR,CURRENT_TIMESTAMP,111);--中国时间表示 select CURRENT_TIMESTAMP,
CONVERT(VARCHAR,CURRENT_TIMESTAMP,110);--美国时间表示 - 简单练习
--练习题
use TextDB
create table TB_CallRecord
(
Id int not null identity(1,1),
CallNumber nvarchar(50),
TelNum varchar(50),
StartDateTime datetime null,
EndDateTime datetime null
)
--主键约束
alter table TB_CallRecord
add constraint PK_CallRecord primary key (Id);
--检查约束
alter table TB_CallRecord
add constraint CK_CallRecords check(CallNumber like '[0-9][0-9][0-9]') alter table TB_CallRecord
add constraint CK_CallRecords_EndDateTime check(EndDateTime > StartDateTime) --默认约束
alter table TB_CallRecord
add constraint DF_CallRecords default(getdate()) for EndDateTime INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DAF00A4CB80 AS DateTime), CAST(0x00009DAF00A62E94 AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB000D63BC0 AS DateTime), CAST(0x00009DB000D68DC8 AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB000E85C60 AS DateTime), CAST(0x00009DB000E92F50 AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB2015BB7A0 AS DateTime), CAST(0x00009DB2015C4DA0 AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DA4014C9C70 AS DateTime), CAST(0x00009DA4014E0308 AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB400DAA0C0 AS DateTime), CAST(0x00009DB400DD5FE0 AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB200B9AB40 AS DateTime), CAST(0x00009DB200B9FC1C AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB8014042B8 AS DateTime), CAST(0x00009DB80141804C AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime)); --查看全表
select * from TB_CallRecord; --输出通话时间最长的五条记录
select top 5 *,DATEDIFF(SECOND,StartDateTime,EndDateTime) as 通话时长 from TB_CallRecord order by DATEDIFF(SECOND,StartDateTime,EndDateTime) DESC; --输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like、sum
SELECT SUM(DATEDIFF(SECOND,StartDateTime,EndDateTime)) from TB_CallRecord WHERE TelNum like '0%'; --输出通话总时长最多的前三个呼叫员的编号。
select top 3 Id,DATEDIFF(SECOND,StartDateTime,EndDateTime) as 通话时长 from TB_CallRecord order by DATEDIFF(SECOND,StartDateTime,EndDateTime) DESC; --输出本月拨打电话次数最多的前三个呼叫员的编号.group by,count(*)
select top 3 CallNumber,Id,StartDateTime From TB_CallRecord where DATEPART(MONTH,StartDateTime) = 7 order by CallNumber DESC; --表序列化row_number()over(order by 字段) 就是将不连续的表依据某一列值排序
select ROW_NUMBER()over(order by Id) as 序列化 ,*from TB_CallRecord; - 事务
--事务:SQL中每一条语句都是一个事务,任何错误都会导致整个事务失败
--语法:
/*
begin transaction
代码
end
*/
begin transaction
declare @myError int;
update TextDB..TB_CallRecord set CallNumber = 0127897789 where CallNumber = '';
set @myError = (select @@ERROR);
update TextDB..TB_CallRecord set CallNumber = '' where Id = 10;
set @myError += (select @@ERROR);
if(@myError!=0)
begin
rollback transaction --回滚当前的操作
end
else
begin
commit transaction --执行当前的操作
end
--事务的特征:如果一个事务满足原子性,持久性,隔离性,一致性,那么这个操作则称为事务。 --begin transaction select *from TextDB..TB_CallRecord; - 存储过程
--存储过程
--语法:
/*
create proc[edure] 存储过程名字
参数 as 类型 [默认值|out]
as
begin
代码
end
*/
--例如:
go
create proc usp_text
as
begin
begin transaction
declare @myError int;
update TextDB..TB_CallRecord set CallNumber = '' where CallNumber = '';
set @myError = (select @@ERROR);
update TextDB..TB_CallRecord set CallNumber = '' where Id = 10;
set @myError += (select @@ERROR);
if(@myError!=0)
begin
rollback transaction
end
else
begin
commit transaction
end end
--执行存储过程
exec usp_text; select * from TextDB..TB_CallRecord; --带参数的存储过程
go
create proc usp_text2
@oldnum as nvarchar(50)
,@newnum as nvarchar(50)
as
begin
update TextDB..TB_CallRecord set CallNumber = @newnum where CallNumber = @oldnum;
end --调用带参数的存储过程
exec usp_text2 '','';
select * from TextDB..TB_CallRecord --带参数和返回值的存储过程
go
create proc usp_text3
@oldnum as nvarchar(50)
,@newnum as nvarchar(50)
,@isSuccess int output --使用output将函数内参数抛出给外部
as
begin
declare @myError int
update TextDB..TB_CallRecord set CallNumber = @newnum where CallNumber = @oldnum;
set @myError = (select @@ERROR);
if(@myError=0)
begin
commit
set @isSuccess = 1
end
else
begin
rollback
set @isSuccess = 0;
end
end --调用带参数和返回值的存储过
declare @result int
exec usp_text3 '','',@result output;
select @result; --使用try catch
go
create proc usp_text4
@oldnum as nvarchar(50)
,@newnum as nvarchar(50)
,@IsSucess int output
as
begin
begin transaction
update TextDB..TB_CallRecord
set CallNumber = @newnum
where CallNumber = @oldnum
begin try
commit
set @IsSucess = 1;
end try
begin catch
rollback
set @IsSucess = 0;
end catch
end --调用带有try,catch的存储过程
declare @result int
exec usp_text4 '','',@result output;
select @result; - 触发器
--触发器
--语法
/*
create trigger tr_类型触发器名字 on 表名
触发类型:after|instea of
操作类型:inser|delete|update
as
begin
代码
end
*/ --案例
--插入数据的同时获得自动增长的Id
select * from TextDB..TB_USER; insert INTO TextDB..TB_USER (userID,[password],code,lastTime)
OUTPUT inserted.userID
VALUES ('aaa','DDD','','2007-03-12'); -----------
go
create trigger tr_delete_不会删除的表 on TextDB..TB_USER
after
delete
as
insert into TextDB..TB_USER(userID,[password],code,lastTime)
select userID,[password],code,lastTime from deleted;
go select * from TextDB..TB_USER;
delete TB_USER
select SUSER_NAME();学习于蒋坤老师视频教程
SQL学习(时间,存储过程,触发器)的更多相关文章
- sql学习笔记--存储过程
存储过程(stored procedure)有时也称sproc,它是真正的脚本,更准确地说,它是批处理(batch),但都不是很确切,它存储与数据库而不是单独的文件中. 存储过程中有输入参数,输出参数 ...
- PL/SQL学习笔记之触发器
一:触发器响应的事件 数据库操作(DML)语句(DELETE,INSERT,UPDATE) 数据库定义(DDL)语句(CREATE,ALTER或DROP) 数据库操作(SERVERERROR,登录,注 ...
- 1.8(SQL学习笔记)触发器
一.触发器简介 当需要某些操作在某些语句执行之前或之后执行就需要使用触发器. 例如每次插入数据时进行数据校对,每次删除数据后将删除内容备份到新表. 这些操作我们希望它(某些语句)在满足某些条件时自动执 ...
- SQL学习笔记七之MySQL视图、触发器、事务、存储过程、函数
阅读目录 一 视图 二 触发器 三 事务 四 存储过程 五 函数 六 流程控制 一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名 ...
- Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器
---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...
- 查看SQL SERVER 加密存储过程,函数,触发器,视图
原文:查看SQL SERVER 加密存储过程,函数,触发器,视图 create PROCEDURE sp_decrypt(@objectname varchar(50))ASbeginset noc ...
- Sql Server 查看存储过程最后修改时间
Sql Server 查看存储过程最后修改时间 select * from sys.procedures order by modify_date desc
- 2020重新出发,MySql基础,MySql视图&索引&存储过程&触发器
@ 目录 视图是什么 视图的优点 1) 定制用户数据,聚焦特定的数据 2) 简化数据操作 3) 提高数据的安全性 4) 共享所需数据 5) 更改数据格式 6) 重用 SQL 语句 MySQL创建视图 ...
- MySQL笔记---视图,存储过程, 触发器的使用入门
大二学数据库的时候,只是隐约听到老师提起过视图啊,存储过程啊,触发器啊什么的,但只是淡淡的记住了名字,后来自己做些小项目,小程序,也没有用上过,都只是简单的建表,关联表之类的,导致我对这些东西的理解只 ...
随机推荐
- Xcode命令行作用
问题:Command Line Tools for Xcode有什么用 答案: Command Line Tools里面有git, xcrun, xcodebuild, gcc, gdb, make等 ...
- 解决SVN UUID客户端和服务器不一致的问题
下面是从别的文章中COPY过来的两篇文章,可以完美的解决这个问题: 一. 重新定位SVN的时候,遇到uuid不一致的问题. Google得知可以使用以下命令 有到svnadmin命令:(位于 SVN安 ...
- Etherlab debian安装记录
debian wheezy 7.11(虚拟机安装选择桥接网卡) #set ustc source #apt-get install sudo #nano /etc/sudoers;add userNa ...
- Many-to-many relationships in EF Core 2.0 – Part 4: A more general abstraction
In the last few posts we saw how to hide use of the join entity from two entities with a many-to-man ...
- 微信小程序禁止刷新之后苹果端还可以下拉的问题
一.问题描述 最近在做一个小程序项目,需要禁止下拉刷新,于是在page.json里面添加了这段话 "enablePullDownRefresh":false 全局关闭下拉刷新,这段 ...
- python 爬虫简介以及使用方法
阶段大纲: 一. 爬虫 1. 基本操作 - 登录任意网站(伪造浏览器的任何行为) 2. 性能相关 - 并发方案: - 异步IO: gevent/Twisted/asyncio/aiohttp - 自定 ...
- anyconnect connection attempt has failed
anyconnect connection attempt has failed 在控制面板-网络与Internet-网络连接,右键AnyConnect secure连接适配器,点击属性 在连接项目中 ...
- MySQL的空值和NULL区别
从本质上区别: 1.空值不占空间 2.null值占空间 通俗的讲: 空值就像是一个真空转态杯子,什么都没有,而null值就是一个装满空气的杯子,虽然看起来都是一样的,但是有着本质的区别. ...
- CentOS 6.8安装Ceph
机器规划 IP 主机名 角色 10.101.0.1 ceph01 mon admin mds 10.101.0.2 ceph02 ods 10.101.0.3 ceph03 ods 10.101.0. ...
- 如何利用Linux去油管下载带字幕的优质英文资料提升英文听力和词汇量
非常方便地从油管下载你需要的任何英文视频资料,并且带字幕,方便你学习某个特定领域的词汇: [step1,Centos6系统安装youtbe-dl下载带英文字幕的视频] 1.首先需要安装youtube- ...