故如虹,知恩;故如月,知明
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

sqlserver存储过程

4213人阅读 2020/5/20 17:20 总访问:3919675 评论:0 收藏:0 手机
分类: 数据库

什么是存储过程:

   预编译的sql语句,可以放很多sql语句,里边可以写条件,循环,可以把一些逻辑放到存储过程里边处理

   比如转账。


优点:

   1:减少sql语句长度,本身需要很多条sql才能解决的问题,现在只需要使用存储过程名称代替。

   2:提高查询效率,因为存储是预编译的sql语句,存储过程创建的时候会变成二进制,下次在执行的时候不需要重新编译直接执行(*****)

   3:封装,把很多逻辑封装起来

   4:安全性


缺点:

   维护困难,有些公司喜欢把一些复杂的逻辑写到存储过程,调试麻烦,出错之后不好找问题。


   

   存储过程分类:

         1:系统自带的存储过程(数据库自带)

               查询所有数据库的存储过程:sp_databases

               查询表字段的存储过程    :sp_columns

          

         2:用户自定义存储过程(自己写的)

               语法:

               create procedure 存储过程名字 [参数列表]  输出参数需要加 out

               as

               begin

                  .........TSQL语句...........

               end

               

         3:执行存储过程

               execute 存储过程名称 [参数列表]  输出参数需要加 out

               

               传递参数两种方法:

               

               1:execute 存储过程名称 参数名=value,参数名2=value,参数名3 = 输出参数 out ........(指定参数名)

               

               2: execute 存储过程名称 value,value2,输出参数 out  (省略参数名)

          

         

         4: 写一个没有参数的存储过程,就输出一句固定值

               create proc proc_show

               as 

               begin

                  print('hello proc')

               end;

          

          

               exec proc_show

   

         5: 输出一个变量的值,有一个参数的存储过程

               create proc proc_show2(@value nvarchar(64))

               as

               begin

                  print(@value);

               end

               

               exec proc_show @value ='hello'

               exec proc_show 'proc'

               

               

         6: 求合存储过程,两个输入参数

              create proc proc_sum(@p1 int,@p2 int)

              as

              begin

                 print(@p1+@p2);

              end

         

         7:   求和存储过程,两个输入参数,还有一个输出参数(输出参数为两个输入参数的合)

              create proc proc_sum2(@p1 int,@p2 int,@sum int out)

              as

              begin

                 @sum = @p1+@p2;

              end

              

存储过程例子:

exec sp_databases
exec sp_columns 'userinfo'
select * from dbo.UserInfo


create proc proc_show
as 
begin
  print('hello proc')
end;

exec proc_show




create proc proc_show2(@value nvarchar(64))
as
begin
      print(@value);
end

exec proc_show2 @value = 'hello'
exec proc_show2 'proc'



--求和存储过程
create proc proc_sum(@p1 int,@p2 int)
as
begin
 print(@p1+@p2);
end

execute proc_sum @p1=6,@p2 =7
execute proc_sum 8,9


go

--求和存储过程2  (含有输出参数的存储过程)        
-- @p1 =8,@p2=8  , @sum = @result out  = @p1+@p2
create proc proc_sum2(@p1 int,@p2 int,@sum int out)
as
begin
  set @sum = @p1+@p2;
end

go

--定义一个变量,作用:用于接收存储过程的输出参数
declare @result int
exec proc_sum2 @p1 =3,@p2=5,@sum = @result out
print @result

declare @result int
exec proc_sum2 8,8,@result out
print @result



--用户名和学号去查询用户表,为空忽略条件
go
alter proc proc_select(@username nvarchar(64),@number nvarchar(64))
as
begin
      --为空就查询全部
      if(@username ='' and @number ='')
          select * from UserInfo    
      --用户名不为空,学生不为空,说明需要根据这两个条件去查询
      else if(@username !='' and @number !='')
          select * from UserInfo where username =@username and number = @number
      --用户名为空,学号不为空就根据学号查询
      else if (@username ='' and @number !='') 
          select * from UserInfo where number =@number
      else
        select * from UserInfo where username =@username
end

--当两个条件为空查询全表
exec proc_select '',''

exec proc_select '刘备',''

exec proc_select '刘备','001'

exec proc_select '','NS008'

exec proc_select '诸葛亮','NS008'


--动态执行sql语句
--declare @sql nvarchar(64)
--set @sql='select * from UserInfo where id=1';
--exec(@sql)





--查询用户表存储过程
alter proc proc_select(@username nvarchar(64),@number nvarchar(64))
as
begin
  select * from userinfo where username = @username or number = @number
end

exec proc_select '吕布','ns008'


欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价