如何在VS2008中插入用户名(报告版)

How to insert username in VS2008(report edition)

我正在创建一个新报告 (*.rdl),我想在其中添加运行脚本的用户名(插入)。

我已经在 VS2008 上尝试通过 "built-in-fields" 函数 "User ID",但没有成功:

CREATE TABLE #Some_Table
(
Plan_date date null,
Plan_customer int null,
creator_id nvarchar(55) null
)

INSERT INTO Some_Table
(

[Plan_date] ,
[Plan_customer],
[creator_id]

)
SELECT
@p_plan_monthly,
@p_plan_clients,
@creator_id ="user id" --from built-in-fields

预期结果是:第 creator_id 列填充了通过我的报告进行插入的活动目录中的用户名值。

问:"and there I want to add username who runs the script (insert)"

您可以使用这些功能。

-- database user name
SELECT USER_NAME() 
-- login identification name
SELECT SUSER_NAME()

重申我的评论,因为它非常重要: "You need to use a different account to access your data @whitefang. The sa account should never be used for something as mundane as a report. In truth it should never be used unless you really need sysadmin privileges, or you're doing something like recovering the server. You should have a service account that can do the respective tasks it needs to. If you can suffer injection through those reports, you're service is like an open book to whomever has access."

现在,进入你的问题。我会在您的报告中添加一个进一步的内部参数。更改参数的值使其具有默认值 =User!UserID;这将是用户 运行 报告的 ID(可能类似于 Whosebug\Larnu)。

然后将该报告参数映射到您的数据集参数 @creator_id 并将您的 INSERT 语句更改为:

INSERT INTO Some_Table ([Plan_date],
                        [Plan_customer],
                        [creator_id])
VALUES (@p_plan_monthly, @p_plan_clients, @creator_id);