从中选择时是否可以有一个 table/view return 标量值?
Is it possible to have a table/view return a scalar value when selecting from it?
我正在 运行 生成 SSRS 报告,我的数据源是 FetchXML。
很遗憾,使用 FetchXML,您无法 运行 存储过程或函数。
这个问题可以用几种不同的方式提出:
- 如何将函数的值传递给 FetchXML?
- 是否可以只创建一个 table 或视图 tbl_finduserGUID 而不是执行上述操作,从而 return 值函数 dbo.fn_finduserguid()?
- 或者有更好的方法通过SSRS/FetchXML获取当前用户的数据库角色?
这是我的查询,将转换为 fetchxml:
SELECT systemuser.fullname AS 'fullname',
usersettings.calendartype AS 'calendartype',
usersettings.uilanguageid AS 'uilanguageid'
FROM filteredsystemuser AS systemuser
INNER JOIN filteredusersettings AS usersettings
ON systemuser.systemuserid = usersettings.systemuserid
WHERE systemuser.systemuserid = dbo.Fn_finduserguid()
这是dbo.fn_finduserguid()
的来源
SET ansi_nulls ON
go
SET quoted_identifier ON
go
ALTER FUNCTION [dbo].[Fn_finduserguid] ()
returns UNIQUEIDENTIFIER
AS
BEGIN
DECLARE @userGuid UNIQUEIDENTIFIER
--- test whether the query is runing by priviledged user with user role of CRMReaderRole
--- if it is dbo, we trust it as well.
--- There is an issue in SQL. If the user is a dbo, if it not member of any role
IF ( Is_member('CRMReaderRole') | Is_member('db_owner') ) = 1
BEGIN
SELECT @userGuid = Cast(Context_info() AS UNIQUEIDENTIFIER)
END
IF @userGuid IS NULL
BEGIN
SELECT @userGuid = s.systemuserid
FROM systemuserbase s
WHERE s.domainname = Suser_sname()
END
RETURN @userGuid
END
创建一个 select 函数作为列的视图应该没有问题。
Create view vFindUserGuid
As
Select dbo.Fn_finduserguid() as userguid
您的查询然后可以像正常一样使用视图 table
SELECT systemuser.fullname AS 'fullname',
usersettings.calendartype AS 'calendartype',
usersettings.uilanguageid AS 'uilanguageid'
FROM filteredsystemuser AS systemuser
INNER JOIN filteredusersettings AS usersettings
ON systemuser.systemuserid = usersettings.systemuserid
WHERE systemuser.systemuserid = (select userguid from vfinduserguid)
我正在 运行 生成 SSRS 报告,我的数据源是 FetchXML。
很遗憾,使用 FetchXML,您无法 运行 存储过程或函数。
这个问题可以用几种不同的方式提出:
- 如何将函数的值传递给 FetchXML?
- 是否可以只创建一个 table 或视图 tbl_finduserGUID 而不是执行上述操作,从而 return 值函数 dbo.fn_finduserguid()?
- 或者有更好的方法通过SSRS/FetchXML获取当前用户的数据库角色?
这是我的查询,将转换为 fetchxml:
SELECT systemuser.fullname AS 'fullname',
usersettings.calendartype AS 'calendartype',
usersettings.uilanguageid AS 'uilanguageid'
FROM filteredsystemuser AS systemuser
INNER JOIN filteredusersettings AS usersettings
ON systemuser.systemuserid = usersettings.systemuserid
WHERE systemuser.systemuserid = dbo.Fn_finduserguid()
这是dbo.fn_finduserguid()
的来源SET ansi_nulls ON
go
SET quoted_identifier ON
go
ALTER FUNCTION [dbo].[Fn_finduserguid] ()
returns UNIQUEIDENTIFIER
AS
BEGIN
DECLARE @userGuid UNIQUEIDENTIFIER
--- test whether the query is runing by priviledged user with user role of CRMReaderRole
--- if it is dbo, we trust it as well.
--- There is an issue in SQL. If the user is a dbo, if it not member of any role
IF ( Is_member('CRMReaderRole') | Is_member('db_owner') ) = 1
BEGIN
SELECT @userGuid = Cast(Context_info() AS UNIQUEIDENTIFIER)
END
IF @userGuid IS NULL
BEGIN
SELECT @userGuid = s.systemuserid
FROM systemuserbase s
WHERE s.domainname = Suser_sname()
END
RETURN @userGuid
END
创建一个 select 函数作为列的视图应该没有问题。
Create view vFindUserGuid
As
Select dbo.Fn_finduserguid() as userguid
您的查询然后可以像正常一样使用视图 table
SELECT systemuser.fullname AS 'fullname',
usersettings.calendartype AS 'calendartype',
usersettings.uilanguageid AS 'uilanguageid'
FROM filteredsystemuser AS systemuser
INNER JOIN filteredusersettings AS usersettings
ON systemuser.systemuserid = usersettings.systemuserid
WHERE systemuser.systemuserid = (select userguid from vfinduserguid)