1

I am attempting to make a view of a table that uses the current user's user name (System_user) with their log in information ([permission path]) which tells me which subset of the data they are allowed to see in the view. Views cannot be created with variables or temporary tables so made an attempt at user defined functions to return a table but it appears to be a bit beyond my current skills.

It works as a Query

DECLARE @user as varchar(50)
 SELECT @user = SYSTEM_USER

CREATE TABLE LoginInformation(
      [account name] varchar(50),
      [type] varchar(50),
      [privilege] varchar(50),
      [mapped login name] varchar(50),
      [permission path] varchar(50)
)

INSERT LoginInformation
 EXEC xp_LoginInfo @AcctName = @user, @Option = 'all';

SELECT * FROM SomeTableName
WHERE Permission IN (SELECT [permission path] FROM LoginInformation)

When creating a view I get stuck because I cannot declare a variable:

DECLARE @user as varchar(50)
SELECT @user = SYSTEM_USER

When I tried user defined functions i got held up because I could not Insert an Executed statement

INSERT LoginInformation
 EXEC xp_LoginInfo @AcctName = @user, @Option = 'all';

I also do not appear to be able to do the simplest solution:

INSERT LoginInformation
     EXEC xp_LoginInfo @AcctName = SYSTEM_USER, @Option = 'all';

Ideally i would have a function that took System_user as a parameter and gave me a table of just that user's [permission path]. I could then use that in my view's creation.

 WHERE Permission IN (MyFunctionName(SYSTEM_USER))
2
  • Why don't you just use INSERT LoginInformation EXEC xp_LoginInfo @AcctName = SYSTEM_USER, @Option = 'all'; inside your view instead of declaring a variable? Mar 2, 2016 at 15:03
  • I am not sure but I get a message: Incorrect syntax near the keyword 'SYSTEM_USER'. Mar 2, 2016 at 15:05

2 Answers 2

0

xp_LoginInfo is a stored procedure. You can see its underlying code by running the following T-SQL statement:

sp_helptext xp_LoginInfo

Which produces the output:

create procedure sys.xp_logininfo
    @acctname       sysname = null,             -- IN: NT login name
    @option         varchar(10) = null,         -- IN: 'all' | 'members' | null
    @privilege      varchar(10) = 'Not wanted' OUTPUT   -- OUT: 'admin' | 'user' | null
as
set nocount on
etc ...

My suggestion is to take the underlying SQL, tweak it if needed and use it in your view.

1
  • Great suggestion, I was looking for a way to open up the function and look at how it worked. I'll definitely be putting sp_helptext in my SQL arsenal. In this case I ran into trouble with OpenRowset(TABLE NETGROUPGETMEMBERS, @acctname) Mar 8, 2016 at 14:08
0

While doing additional research I found a much easier and different way of accomplishing my goal. I went with the Is_Member function. It accesses the permission path without having to work with SYSTEM_USER. I have done a few tests and it appears to do what I want.

CREATE VIEW [dbo].[vw_SomeTableName]
SELECT * FROM SomeTableName 
where WHERE Is_Member( UserName )  = 1 )

The Is_Member function indicates whether the current user is a member of the specified Microsoft Windows group or SQL Server database role.

IS_MEMBER ( { 'group' | 'role' } )

Return value:

  • Current user is not a member of group or role, returns 0.
  • Current user is a member of group or role, returns 1.
  • Either group or role is not valid. When queried by a SQL Server login or a login using an application role, returns NULL for a Windows group.

For more on Is_Member check out msdn.microsoft.com on Is_Member or look at some other stack overflow questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.