Monday, March 12, 2012

Report Services - capture userid of person running the report

I have a report which executes a stored procedure when the report is opened. What function will capture the identity ( name or user id ) of the person opening the report and therefore executing the procedure?

At what point in the stack are you trying to get the userid? Within the SP, or within the report expressions?

From within the report, you can use User!UserId. If you want to get the login that is currently executing the SP from within the SP, then you can use system_user (http://msdn2.microsoft.com/en-us/library/ms179930.aspx).

|||More specifics please|||

the first parameter of the report is UserId, string, hidden

the available value is label UserId with value =User!UserID. The default value is non-queried =User!UserID

the data source is: exec ReportBase.dbo.proc_TestUser '01/01/2006','01/02/2006',@.UserID

the procedure is:

-- =============================================

-- Create basic stored procedure template

-- =============================================

use ReportBase

GO

-- Drop stored procedure if it already exists

IF EXISTS (

SELECT *

FROM INFORMATION_SCHEMA.ROUTINES

WHERE SPECIFIC_SCHEMA = N'dbo'

AND SPECIFIC_NAME = N'proc_TestUser'

)

DROP PROCEDURE dbo.proc_TestUser

GO

CREATE PROCEDURE dbo.proc_TestUser

@.Date1 as datetime,

@.Date2 as datetime,

@.UserID as varchar(100)

AS

-- drop table ReportBase.dbo.t_UserTest

/*

create table ReportBase.dbo.t_UserTest (

[first_date] [datetime] NULL ,

[second_date] [datetime] NULL ,

[user_name] [varchar](100) NULL )

*/

insert into ReportBase.dbo.t_UserTest

select @.Date1, @.Date2, @.UserID

select * from ReportBase.dbo.t_UserTest

go

No comments:

Post a Comment