zoukankan      html  css  js  c++  java
  • SQL存储过程事务和优化方法(包括查询方式语句结合)

    判断插入值是否重复:

    CREATE PROCEDURE  IR_rb_User_UpdateUser
    (
        @UserID        int,
        @Name    nvarchar(50),
        @Email           nvarchar(100),
        @Password    nvarchar(20),
        @RoleID        int,
        @iSuccess    int output
    )
    AS
    
    set @iSuccess = -1    --判断是否成功有错误应为插入重复键
    
    IF EXISTS (SELECT Email  FROM rb_Users WHERE Email = @Email and UserID<>@UserID)
    BEGIN
      set @iSuccess = -2
      return
    END
    
    BEGIN TRAN
    
        UPDATE    rb_Users  SET    Name   = @Name,    Email    = @Email,    Password = @Password  WHERE    UserID   = @UserID
    
        IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN
            return
        END
        set @iSuccess = -3
    
        update rb_UserRoles set RoleID=@RoleID  where UserID=@UserID
    
        IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN
            return
        END
    
    COMMIT TRAN
    
    set @iSuccess = 1
    GO
    
    
    条件查询语句:
    CREATE PROCEDURE IR_SysAuthorization_GetAllSysAuthorization
    (
        @Type int,
        @UserID varchar(20)
    )
     AS
    
    declare     @strsql    varchar(1500)        --临时sql语句
    declare     @strwhere    varchar(1000)        --临时sql的条件语句
    
    set @strsql = ''
    set @strsql = @strsql + 'select UserID,Role,(select typename from SysAuthorizationType where type=t.type) as typeTmp, '
    set @strsql = @strsql + '(CASE WHEN State = 1 THEN ' + '''有效''' + ' ELSE ' + '''无效''' + ' END) AS StateTmp '
    set @strsql = @strsql + 'FROM SysAuthorization t '
    
    set @strwhere = ''
    
    
    IF  @UserID <> '' 
    BEGIN
        set @strwhere = @strwhere + ' and (UserID LIKE ' + '''' + '%' + @UserID + '%' + '''' + ')'
    END
    
    IF  @Type >0
    BEGIN
        set @strwhere = @strwhere + ' and Type =  ' + CAST(@Type AS varchar(12))
    END
    
    if @strwhere <>''
    BEGIN
        set @strsql = @strsql + ' where ' + right(@strwhere, len(@strwhere)-4)
    END
    
    --按用户名排序
    set  @strsql = @strsql + ' ORDER BY Type,UserID'
    
    exec (@strsql)
    GO
    
    
     

    http://hanguoji.cnblogs.com/archive/2006/05/18/403533.html

  • 相关阅读:
    端口被占用
    启动Windows防火墙提示“0x8007042c"
    vue创建全局组件
    vue中过度动画之列表添加删除动画实现
    vue中过渡动画(类名结合动画实现方式)
    vue中过渡动画(类名实现方式)
    this.$nextTick()方法的使用
    利用axios获取数据并渲染到视图层
    axios的简单使用
    watch深度监听
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1933726.html
Copyright © 2011-2022 走看看