存储过程

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值 变量保持原值
原文地址:https://www.cnblogs.com/Jacob-Wu/p/10251526.html