Microsoft Visual Studio 创建临时表

Microsoft Visual Studio Create temporary Tables

美好的一天,我正在尝试在 SSRS 中创建一个数据集,不幸的是我需要创建一个临时 table 才能创建我的报告。我的代码如下

create table #Tax_Admin (AWD varchar (255), Name varchar(255))  
insert into #Tax_Admin  
values  ('MDSTM','Agent 1'),
        ('MDSAK','Agent 2'),    
        ('ITDPM','Agent 3'),    
        ('MASEL','Agent 4'),    
        ('ITDEK','Agent 5'),    
        ('ITDFM','Agent 6');    

with #transactions_workingwith  
as  
(   
SELECT distinct 
      [TRANSACTION_ID]  

  FROM [mmcenvsqlprd01].[MOMDWL1].[ibrststdatEQ].[MOVEMENT_DATA] A

    where   

  (a.TO_QUEUE like '%FINTAX%'   
  OR  a.TO_QUEUE like '%TAXQ%'  
  OR  a.TO_QUEUE like '%TAXCERT%')  

  and a.TO_STATUS_DATE >= '2017-06-01') 



  , User_Movements as   

  ( 


  Select    
  a.TRANSACTION_ID  
 ,a.POLICY_NUMBER   
  ,a.USER_ID    
  ,a.BILLING_CYCLE_ID   
  ,a.TRANSACTION_TYPE   
  ,a.FROM_STATUS    
  ,a.TO_STATUS  
  ,a.TO_STATUS_DATE 
  ,a.TO_QUEUE   
  , ROW_NUMBER() over (Partition by a.TRANSACTION_ID order by a.TO_STATUS_DATE) as User_Movement_Nr 

  ,case when   (a.TO_QUEUE like '%FINTAX%'  
  OR  a.TO_QUEUE like '%TAXQ%'  
  OR  a.TO_QUEUE like '%TAXCERT%') then 'Tax Queue' else 'Other Queue' end as Queue_Taype   

  FROM [mmcenvsqlprd01].[MOMDWL1].[ibrststdatEQ].[MOVEMENT_DATA] A  
  inner join #transactions_workingwith b on a.TRANSACTION_ID Collate SQL_Latin1_General_CP1_CI_AS = b.TRANSACTION_ID Collate SQL_Latin1_General_CP1_CI_AS   

    where   

   a.TO_STATUS_DATE >= '2017-06-01')    


   Select distinct  

     a.TRANSACTION_ID   
     ,a.POLICY_NUMBER
     ,convert(varchar,a.TO_STATUS_DATE,112) + isnull(c.EMPLOYEE_NR,d.EMPLOYEE_NR)  as EmployeeKey ---Why do we need this empoyee key?
  ,a.USER_ID    
  ,isnull(c.EMPLOYEE_NR,d.EMPLOYEE_NR) as Emplyee_Nr    
    ,f.[BusinessArea]   



  ,a.TRANSACTION_TYPE   
  ,a.FROM_STATUS    
  ,a.TO_STATUS  
  ,b.TO_STATUS as Next_Status   
  ,b.USER_ID as Next_Status_User    
  ,isnull(e.Name,'Other') as Tax_Admin_Name 
  ,case when b.TO_STATUS like '%FAIL%' THEN 1 ELSE 0 END AS Is_Fail 
    ,case when b.TO_STATUS like '%INFO%' THEN 1 ELSE 0 END AS Is_Info   
     ,case when b.TO_STATUS like '%TRAIN%' THEN 1 ELSE 0 END AS Is_Train

     ,
     case when b.TO_STATUS like '%FAIL%' THEN 1 ELSE 0 END
     + case when b.TO_STATUS like '%INFO%' THEN 1 ELSE 0 END
     + case when b.TO_STATUS like '%TRAIN%' THEN 1 ELSE 0 END as Returned

  ,a.TO_STATUS_DATE 
  ,a.TO_QUEUE   
  ,a.User_Movement_Nr   
  ,a.BILLING_CYCLE_ID   
  ,1 as Movement    

   from User_Movements a    
   inner join User_Movements b 
   on a.TRANSACTION_ID = b.TRANSACTION_ID and  a.User_Movement_Nr + 1 = b.User_Movement_Nr  
   left  JOIN [mmcenvsqlprd01].momdwl1.ibrststcnfEQ.IBRS_USER_INFO c 
   on a.USER_ID Collate SQL_Latin1_General_CP1_CI_AS = c.USER_ID Collate SQL_Latin1_General_CP1_CI_AS   
   left  JOIN [mmcenvsqlprd01].momdwl1.ibrststcnfWealth.IBRS_USER_INFO d 
   on a.USER_ID Collate SQL_Latin1_General_CP1_CI_AS     = d.USER_ID Collate SQL_Latin1_General_CP1_CI_AS       
   left join #Tax_Admin e on b.USER_ID Collate SQL_Latin1_General_CP1_CI_AS  = e.AWD Collate SQL_Latin1_General_CP1_CI_AS       
   inner join [mmcenvsqlbi01].[Master_Data].[Trans].[Employee_Snapshot] f 
   on convert(varchar,a.TO_STATUS_DATE,112) + isnull(c.EMPLOYEE_NR,d.EMPLOYEE_NR) Collate SQL_Latin1_General_CP1_CI_AS = f.[EmployeeKey]     Collate SQL_Latin1_General_CP1_CI_AS   
   where a.Queue_Taype = 'Tax Queue'

   drop table #Tax_Admin

我该如何将此代码移至 SSRS? 我已经尝试了 With 子句,但它不起作用。我尝试构建一个 Null table 然后更新信息,但没有用。 有人可以帮我吗?

在 SSRS 中创建临时 table 有什么问题?我 运行 进行了一个快速测试,以确保您可以在 SSRS 2012 中执行此操作,并且看起来运行良好,例如我的数据集定义为:

CREATE TABLE #temp(id INT, name VARCHAR(10));
INSERT INTO #temp SELECT 1, 'Harry';
INSERT INTO #temp SELECT 2, 'Megan';
SELECT * FROM #temp;

当您尝试在 SSRS 中执行此操作时遇到的实际错误是什么?

如果你不能让它工作,那么你可以创建一个存储过程(无参数)并简单地将你的脚本粘贴到其中,然后使用查询类型 = 存储过程并选择你刚刚在 SSRS 中创建的那个.

使用 WITH 最常见的问题是您之前的语句没有正确地以 ; 终止,但在您的脚本中似乎并非如此?人们通常 ;WITH 可以解决此问题。

您可以尝试使用 table 变量,即将 #Tax_Admin 更改为 @Tax_Admin 并将 DECLARE 更改为 TABLE.