zoukankan      html  css  js  c++  java
  • sql标量函数与表值函数

    标量函数
    CREATE function [dbo].[UserIDFromName](@UserName nvarchar(20),@UserPass nvarchar(64))
    returns int
    as
    begin
        return (select UserID from UserInfo where UserName=@UserName and UserPass=@UserPass)
        
    end;

    调用:

    create procedure [dbo].[GetUserRole] 
        @UserName nvarchar(20),
        @PassWord nvarchar(64),
        @FuncID int
    AS
        set nocount on
     
        declare @UserID int
        set @UserID=Admin.dbo.UserIDFromName(@UserName,@PassWord)
    
        if @UserID is null
        begin
            select ret=1,msg='用户名或密码错误!'
            return -1
        end
        if not exists (select UserID from UserInfo where UserID=@UserID and IsActive=1)
        begin
            select ret=1,msg='该用户尚未激活,请联系管理员!'
            return -1
        end
    
        declare @isAdmin bit
        set @isAdmin=(select IsAdmin from UserInfo where UserName=@UserName)
        if @isAdmin=1
        begin
            if @FuncID is not null
                return 4
            else begin
                select ret=1,msg='权限不存在!'
                return -1
            end
        end
    
        if @FuncID is not null
        begin
            declare @role int
            set @role=(select role from UserRole where UserID=@UserID and FuncID=@FuncID)
            if @role is null 
                set @role=0
            return @role
        end 
        else begin
            select ret=1,msg='权限不存在!'
            return -1
        end

     表值函数:

    CREATE FUNCTION [dbo].[Get_BindMaxMemberOrder](@dwRcvUserID as INT)
    RETURNS TABLE
    AS
        -- 绑定会员,(会员期限与切换时间)
        return (SELECT MAX(MemberOrder) as MaxmemberOrder,MAX(MemberOverDate) as MaxmemberOverDate
        FROM MemberInfo WHERE UserID=@dwRcvUserID)

    调用:

    -- 绑定会员,(会员期限与切换时间)
            SELECT @MaxMemberOrder=MaxMemberOrder,@MemberOverDate=MaxMemberOverDate
            FROM UserDB.dbo.Get_BindMaxMemberOrder(@dwRcvUserID)
  • 相关阅读:
    Shell与if相关参数
    Linux盘符漂移问题
    shell脚本,每5个字符之间插入"|",行末不插入“|”
    paste:linux合并两个文件中的列(左右合并)
    关于bc 的scale .
    RxJS与观察者模式
    什么是虚拟DOM
    JS设计模式
    JS自定义事件
    原生js实现拖拽功能
  • 原文地址:https://www.cnblogs.com/hsw-2013/p/sqlserver.html
Copyright © 2011-2022 走看看