zoukankan      html  css  js  c++  java
  • 增删改查存储过程

    select * from UserInfo
    
    ---------------------------------添加存储过程------------------------------
    if OBJECT_ID('P_add') is not null
    drop proc P_add
    go
    create proc P_add
    --参数
    @UserName varchar(100),
    @UserPwd varchar(100),
    @UserSex int,
    @UserDate datetime,
    @msg varchar(max) out
    as
    --变量
    declare @bcount int,@acount int
    --赋值
    set @bcount=(select COUNT(1) from UserInfo)--添加之前条数
    insert into UserInfo values(@UserName,@UserPwd,@UserSex,@UserDate)
    set @acount=(select COUNT(1) from UserInfo)--添加之后的条数
    if    @acount>@bcount
    set @msg='添加成功'
    
    -----------------------------------删除存储过程------------------------------
    if OBJECT_ID('P_Delete') is not null
    drop proc P_Delete
    go
    create proc P_Delete
    @id int
    as
    delete from UserInfo where id in (@id)
    -----------------------------------修改存储过程------------------------------
    if OBJECT_ID('P_Update') is not null
    drop proc P_Update
    go
    create proc P_Update
    --参数
    @UserName varchar(100),
    @UserPwd varchar(100),
    @UserSex int,
    @UserDate datetime,
    @Id int,
    @msg varchar(max) out
    as
    update UserInfo set 
    UserName=@UserName,
    UserPwd=@UserPwd,
    UserSex=@UserSex,
    UserDate=@UserDate
    where Id=@Id
    set @msg='修改成功'
    ------------------------------------批删除存储过程-------------------------------------
    if OBJECT_ID('P_dels') is not null
    drop proc P_dels
    go
    
    create proc P_dels 
    @id varchar(max)--定义变量要删除数据的id    1 ,2 , 3
    as 
    declare @temp table (id int)--临时表
    declare @index int --下标
    
    set @index=CHARINDEX(',',@id)--查找逗号下标
    print @index
    while @index>1  --判断逗号下标
    begin
    
    insert into @temp values(LEFT(@id,@index-1))--LEFT(要截取的字符串,截取个数) 字符串下标从1开始
    set @id=SUBSTRING(@id,@index+1,LEN(@id)-@index)
    set @index=CHARINDEX(',',@id)--查找逗号下标
    
    end
    if @id<>'' --最后一个
    insert into @temp values(@id)
    
    delete from UserInfo WHERE ID in(select id from @temp)
    
    
    ----------------------------------查询分页------------------------------
    if OBJECT_ID('P_Show') is not null
    drop proc P_Show
    GO
    CREATE proc P_Show
    @pageIndex int,
    @pageSize int,
    @UserName varchar(50)=null,
    @totalCount int out
    as
    
    declare @sql varchar(max),--sql语句
            @sqlWhere varchar(max),--查询条件
            @rid int --序列号
    
    ---初始化变量
    set @sql=''
    set @sqlWhere=''
    set @rid=(@pageIndex-1)*@pageSize
    set @totalCount=(select COUNT(1) from UserInfo)
    
    if @UserName is not null
       begin 
       set @sqlWhere=' and UserName like ''%'+@UserName+'%'''
       set @totalCount=(select COUNT(1) from UserInfo where UserName like '%'+@UserName+'%')
    end
    
    set @sql='select top '+STR(@pageSize)+' * from 
    (
     select *,ROW_NUMBER() over(order by UserDate) as rid from UserInfo where 1=1 '+@sqlWhere+'
    ) temp
    where rid>'+STR(@rid)
    exec (@sql)
    
       

    2020-01-07

  • 相关阅读:
    第一周任务Largest Submatrix of All 1’s
    第一周 Largest Rectangle in a Histogram
    第二次作业
    B. Light bulbs(2019 ICPC上海站)
    二维平面最近点-分治
    python生成器,推导式
    python函数名的运用,闭包,迭代器
    python函数(二)
    python函数(一)
    python基础(七)
  • 原文地址:https://www.cnblogs.com/2018cjx/p/12159650.html
Copyright © 2011-2022 走看看