存储过程

Posted jacob-wu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储过程相关的知识,希望对你有一定的参考价值。

技术分享图片
if(exists (select 1 from sys.objects where name=proc_out)) --用于判断改存储过程是否存在
    drop proc proc_out
GO    
CREATE PROC proc_out
    @id1 int out,     --out    可以传进,可以传出
    @id2 int output   --output 不用传进,也不可传进,可以传出,
AS
declare @id3 int
BEGIN
    if @id1 = 0
        set @id1=1
    select @id2=2;
    select @id3=3;
    return @id3;    -- 返回值
END        
--调用方法
declare @id1 int,@id2 int,@id3 int
set @id1=100
exec @id3=proc_out @id1 out, @id2 output
select @id1,@id2,@id3
out,output,return 区别
技术分享图片
if(exists (select 1 from sys.objects where name=Proc_UPDATE))
    drop proc Proc_UPDATE
GO
Create Proc Proc_UPDATE
    @Id1 int,
    @Id2 int
AS
BEGIN
    BEGIN tran;   --开启事务
    BEGIN try
        update [studentinfo_2019] set Name=Name + @Id1 where Id=@Id1
        update [studentinfo_20191] set Name=Name + @Id2 where Id=@Id2
        commit;    --完成事务
    END try
    BEGIN catch
    rollback;      --回滚事务
    END catch
END    
事务的使用
技术分享图片
if (exists(select * from  sys.objects where name=GetStudentByPage))
    drop proc GetStudentByPage
go 
create proc GetStudentByPage
    @pageIndex int,
    @pageSize int
as 
declare 
@startIndex int,
@endIndex int;
set @startIndex =  (@pageIndex-1)*@pageSize+1;
set @endIndex = @startIndex + @pageSize -1 ;
begin 
    select * from 
    (
        select *,row_number()over (order by Id)as number from studentinfo_2019  
    )t where t.number>=@startIndex and t.number<=@endIndex
end
分页

Set 和 Select  的区别

  set select
同时对多个变量同时赋值 不支持 支持
表达式返回多个值时 出错 将返回的最后一个值赋给变量
表达式未返回值 变量被赋null值 变量保持原值

以上是关于存储过程的主要内容,如果未能解决你的问题,请参考以下文章

Java调用SQL Server的存储过程详解(转)

如何将 r ggplot 图存储为 html 代码片段

sql 这些代码片段将演示如何逐步使用PolyBase。你应该有一个blob存储和存储秘密方便

从Oracle存储过程Oracle 11g发送邮件

mybatis 存储过程

Sublime Text自定制代码片段(Code Snippets)