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)
  • 相关阅读:
    Random 种子问题
    Matrix issue
    Two sum.
    Best Time to Buy and Sell Stock
    Maximum difference between two elements
    二分查找法的实现和应用汇总
    Why you want to be restrictive with shrink of database files [From karaszi]
    Palindrome
    NetBeans vs Eclipse 之性能参数对比 [java060515]
    国内各大互联网公司相关技术站点不完全收录[转]
  • 原文地址:https://www.cnblogs.com/hsw-2013/p/sqlserver.html
Copyright © 2011-2022 走看看