1. 程式人生 > >sql標量函數與表值函數

sql標量函數與表值函數

tab dmi arc ember eat active varchar div ive

標量函數
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)

sql標量函數與表值函數