SQL语言学习20170810

来源:互联网 发布:java数组怎么添加元素 编辑:程序博客网 时间:2024/04/29 02:52

关于数据库的学习,其实比较核心的一点就是存储过程,和事务处理,今天我们就详细的讲解一下,存储过程和事务处理。

存储过程

在大型数据库系统中,存储过程和触发器具有很重要的作用。无论是存储过程还是触发器,都是SQL 语句和流程控制语句的集合。就本质而言,触发器也是一种存储过程。存储过程在运算时生成执行方式,所以,以后对其再运行时其执行速度很快。

所谓的一次编译,到处运行!

这个定义,应该是比较官方的定义了:

存储过程(Stored Procedure)是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库。中用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。

在SQL Server 的系列版本中存储过程分为两类:

  • 系统提供的存储过程 ,主要在master库当中
  • 用户自定义存储过程

存储过程优点:

  1. (1)存储过程允许标准组件式编程
  2. (2)存储过程能够实现较快的执行速度
  3. (3)存储过程能够减少网络流量
  4. (4)存储过程可被作为一种安全机制来充分利用

进一步的定义:

创建存储过程,存储过程是保存起来的可以接受和返回用户提供的参数的 Transact-SQL 语句的集合。

系统存储过程的执行方法:

exec sp_databases;

上述方法,将返回所有的数据库。

用户自定义存储过程

1、 创建语法

create proc | procedure pro_name    [{@参数数据类型} [=默认值] [output],     {@参数数据类型} [=默认值] [output],     ....    ]asbegin    SQL_statementsend

2、 创建不带参数存储过程

--创建存储过程if (exists (select * from sys.objects where name = 'proc_get_student'))    drop proc proc_get_studentgocreate proc proc_get_studentas    select * from student;--调用、执行存储过程exec proc_get_student;

3、 修改存储过程

--修改存储过程alter proc proc_get_studentasselect * from student;

4、 带参存储过程

if (object_id('proc_find_stu', 'P') is not null)    drop proc proc_find_stugocreate proc proc_find_stu(@startId int, @endId int)as    select * from student where id between @startId and @endIdgoexec proc_find_stu 2, 4;

检查proc_find_stu这个对象是否存在
用的OBJECT_ID() 这个函数 ,存在对象呢就会返回唯一的对象表示号,不存在就是null
p是存储过程,对象类型的的意思

5、 带输出参数存储过程

f (object_id('proc_getStudentRecord', 'P') is not null)    drop proc proc_getStudentRecordgocreate proc proc_getStudentRecord(    @id int, --默认输入参数    @name varchar(20) out, --输出参数    @age varchar(20) output--输入输出参数)as    select @name = name, @age = age  from student where id = @id and sex = @age;godeclare @id int,        @name varchar(20),        @temp varchar(20);set @id = 7; set @temp = 1;exec proc_getStudentRecord @id, @name out, @temp output;select @name, @temp;print @name + '#' + @temp;

6、加密存储过程

--加密WITH ENCRYPTION if (object_id('proc_temp_encryption', 'P') is not null)    drop proc proc_temp_encryptiongocreate proc proc_temp_encryptionwith encryptionas    select * from student;goexec proc_temp_encryption;exec sp_helptext 'proc_temp';exec sp_helptext 'proc_temp_encryption';

exec sp_helptext proc_temp_encrytion
会提出,存储过程已经加密保存,从而避免存储过程的意外泄露。

存储过程的基本语法

一、定义变量

–简单赋值
declare @a int
set @a=5
print @a

–使用select语句赋值
declare @user1 nvarchar(50)
select @user1=’张三’
print @user1
declare @user2 nvarchar(50)
select @user2 = Name from ST_User where ID=1
print @user2

–使用update语句赋值
declare @user3 nvarchar(50)
update ST_User set @user3 = Name where ID=1
print @user3

二、表、临时表、表变量

–创建临时表1
create table #DU_User1
(
[ID] [int] NOT NULL,
[Oid] [int] NOT NULL,
[Login] nvarchar NOT NULL,
[Rtx] nvarchar NOT NULL,
[Name] nvarchar NOT NULL,
[Password] nvarchar NULL,
[State] nvarchar NOT NULL
);
–向临时表1插入一条记录
insert into #DU_User1 (ID,Oid,[Login],Rtx,Name,[Password],State) values (100,2,’LS’,’0000’,’临时’,’321’,’特殊’);

–从ST_User查询数据,填充至新生成的临时表
select * into #DU_User2 from ST_User where ID<8

–查询并联合两临时表
select * from #DU_User2 where ID<3 union select * from #DU_User1

–删除两临时表
drop table #DU_User1
drop table #DU_User2

–创建临时表
CREATE TABLE #t
(
[ID] [int] NOT NULL,
[Oid] [int] NOT NULL,
[Login] nvarchar NOT NULL,
[Rtx] nvarchar NOT NULL,
[Name] nvarchar NOT NULL,
[Password] nvarchar NULL,
[State] nvarchar NOT NULL,
)

–将查询结果集(多条数据)插入临时表
insert into #t select * from ST_User
–不能这样插入
–select * into #t from dbo.ST_User

–添加一列,为int型自增长子段
alter table #t add [myid] int NOT NULL IDENTITY(1,1)
–添加一列,默认填充全球唯一标识
alter table #t add [myid1] uniqueidentifier NOT NULL default(newid())

select * from #t
drop table #t
–给查询结果集增加自增长列

–无主键时:
select IDENTITY(int,1,1)as ID, Name,[Login],[Password] into #t from ST_User
select * from #t

–有主键时:
select (select SUM(1) from ST_User where ID<= a.ID) as myID,* from ST_User a order by myID
–定义表变量
declare @t table
(
id int not null,
msg nvarchar(50) null
)
insert into @t values(1,’1’)
insert into @t values(2,’2’)
select * from @t

三、循环

–while循环计算1到100的和

declare @a int
declare @sum int

set @a=1
set @sum=0

while @a<=100
begin
set @sum+=@a
set @a+=1
end

print @sum

四、条件语句

–if,else条件分支

if(1+1=2)
begin
print ‘对’
end
else
begin
print ‘错’
end

–when then条件分支

declare @today int
declare @week nvarchar(3)
set @today=3

set @week=case
when @today=1 then ‘星期一’
when @today=2 then ‘星期二’
when @today=3 then ‘星期三’
when @today=4 then ‘星期四’
when @today=5 then ‘星期五’
when @today=6 then ‘星期六’
when @today=7 then ‘星期日’
else ‘值错误’
end

print @week

五、游标

declare @ID int
declare @Oid int
declare @Login varchar(50)

–定义一个游标
declare user_cur cursor for select ID,Oid,[Login] from ST_User
–打开游标
open user_cur
while @@fetch_status=0
begin
–读取游标
fetch next from user_cur into @ID,@Oid,@Login
print @ID
–print @Login
end
close user_cur
–摧毁游标
deallocate user_cur

六、触发器

  触发器中的临时表:

  Inserted
  存放进行insert和update 操作后的数据
  Deleted
  存放进行delete 和update操作前的数据

–创建触发器
Create trigger User_OnUpdate
On ST_User
for Update
As
declare @msg nvarchar(50)
–@msg记录修改情况
select @msg = N’姓名从“’ + Deleted.Name + N’”修改为“’ + Inserted.Name + ‘”’ from Inserted,Deleted
–插入日志表
insert into LOGvalues(@msg)

–删除触发器
drop trigger User_OnUpdate

原创粉丝点击