使用IS_MEMBER存储过程问题

时间:2022-08-19 16:39:34

I have a table Users with columns

我有一个表用户列

ID nvarchar(4000)
GroupRank int
Definition nvarchar(4000)

ID can be a userid (in which case groupRank is NULL), a domain group with a rank (in which case grouprank is not null) or a reserved default group called #DefaultGroup.

ID可以是用户标识(在这种情况下,groupRank为NULL),具有排名的域组(在这种情况下,grouprank不为空)或称为#DefaultGroup的保留默认组。

I need a stored procedure that will:

我需要一个存储过程:

If ID=SYSTEM_USER, return that Definition

如果ID = SYSTEM_USER,则返回该定义

Otherwise - foreach record in Users with GroupRank NOT NULL in order of group rank, if IS_MEMBER(ID) =1, that definition (if any)

否则 - 具有GroupRank的用户的foreach记录按组排名顺序为NOT NULL,如果IS_MEMBER(ID)= 1,则该定义(如果有)

Otherwise - the #DefaultGroup definition (if it's there)

否则 - #DefaultGroup定义(如果它在那里)

Otherwise return NULL.

否则返回NULL。

Is there an easy way to do this?

是否有捷径可寻?

2 个解决方案

#1


If I'm understanding you correctly, you might be able to use some sort of case statement similar to the one below:

如果我正确理解你,你可以使用类似下面的一种case语句:

SELECT
  ID,
  GroupRank,
  Definition = CASE
                WHEN ID = SYSTEM_USER THEN Definition
                WHEN GroupRank IS NOT NULL AND IS_MEMBER(ID) = 1 THEN Definition
                WHEN ID = '#DefaultGroup' THEN Definition
                ELSE NULL
               END
FROM
     [YourTable]

#2


This seems to work as a table function

这似乎是一个表函数

CREATE FUNCTION dbo.GetDefinition()
RETURNS @definition TABLE 
(
    Defn nvarchar(4000) NULL 
)
AS 
BEGIN
    DECLARE @sys_usr nvarchar(4000);
    DECLARE @def_usr nvarchar(4000);
    SET @sys_usr = SYSTEM_USER;
    SET @def_usr = '#Default';

    IF(EXISTS(SELECT * FROM dbo.User WHERE ID = @sys_usr))
    BEGIN
        INSERT @definition SELECT Definition FROM User WHERE ID = @sys_usr
    END ELSE
    BEGIN
        IF(EXISTS(SELECT TOP 1 * FROM dbo.User WHERE IS_MEMBER(ID) = 1 AND GroupRank IS NOT NULL ORDER BY GroupRank))
        BEGIN
            INSERT @definition SELECT TOP 1 Definition FROM dbo.User 
                WHERE IS_MEMBER(ID) = 1 AND GroupRank IS NOT NULL ORDER BY GroupRank
        END ELSE
        BEGIN
            IF(EXISTS(SELECT * FROM dbo.User WHERE ID = @def_usr))
            BEGIN
                INSERT @definition SELECT Definition FROM User WHERE ID = @def_usr
            END
        END
    END

    RETURN;
END

#1


If I'm understanding you correctly, you might be able to use some sort of case statement similar to the one below:

如果我正确理解你,你可以使用类似下面的一种case语句:

SELECT
  ID,
  GroupRank,
  Definition = CASE
                WHEN ID = SYSTEM_USER THEN Definition
                WHEN GroupRank IS NOT NULL AND IS_MEMBER(ID) = 1 THEN Definition
                WHEN ID = '#DefaultGroup' THEN Definition
                ELSE NULL
               END
FROM
     [YourTable]

#2


This seems to work as a table function

这似乎是一个表函数

CREATE FUNCTION dbo.GetDefinition()
RETURNS @definition TABLE 
(
    Defn nvarchar(4000) NULL 
)
AS 
BEGIN
    DECLARE @sys_usr nvarchar(4000);
    DECLARE @def_usr nvarchar(4000);
    SET @sys_usr = SYSTEM_USER;
    SET @def_usr = '#Default';

    IF(EXISTS(SELECT * FROM dbo.User WHERE ID = @sys_usr))
    BEGIN
        INSERT @definition SELECT Definition FROM User WHERE ID = @sys_usr
    END ELSE
    BEGIN
        IF(EXISTS(SELECT TOP 1 * FROM dbo.User WHERE IS_MEMBER(ID) = 1 AND GroupRank IS NOT NULL ORDER BY GroupRank))
        BEGIN
            INSERT @definition SELECT TOP 1 Definition FROM dbo.User 
                WHERE IS_MEMBER(ID) = 1 AND GroupRank IS NOT NULL ORDER BY GroupRank
        END ELSE
        BEGIN
            IF(EXISTS(SELECT * FROM dbo.User WHERE ID = @def_usr))
            BEGIN
                INSERT @definition SELECT Definition FROM User WHERE ID = @def_usr
            END
        END
    END

    RETURN;
END