从中选择表/视图是否可以返回标量值?

时间:2015-01-09 18:07:10

标签: sql sql-server reporting-services dynamics-crm-2011

我正在运行SSRS报告,我的数据源是FetchXML。

不幸的是,使用FetchXML,您无法运行存储过程或函数。

可以通过几种不同的方式提出这个问题:

  1. 如何将函数的值传递给FetchXML?
  2. 可以只创建一个表格或视图 tbl_finduserGUID 来返回函数 dbo.fn_finduserguid()的值,而不是执行上述操作。
  3. 或者也许有更好的方法通过SSRS / FetchXML获取当前用户的数据库角色?
  4. 这是我的查询,将转换为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 
    

1 个答案:

答案 0 :(得分:1)

创建一个选择函数作为列的视图应该没有问题。

Create view vFindUserGuid
As
    Select dbo.Fn_finduserguid() as userguid

您的查询可以像普通表一样使用视图

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)