将参数传递给动态查询

时间:2014-10-02 19:51:00

标签: sql tsql

我有一个存储过程如下,我需要将参数传递给动态数据透视表。我的查询运行,我只需要根据传递的参数进行一些过滤

  -- AND (@SelectedSystemIDs IS NULL OR  System.ID IN(select  * from dbo.SplitInts_RBAR_1(@SelectedSystemIDs, ',')))
  --AND ((@PlatformID IS NULL) OR  (System.PlatformID = @PlatformID) OR (@PlatformID = 12 AND System.PlatformID <= 2))  
  -- AND (ServiceEntry.ServiceDateTime between @StartDate and @EndDate)

所以我想添加上述标准,怎么能实现呢?

ALTER PROCEDURE  [dbo].[spExportStuff]                            
(@StartDate datetime,                            
@EndDate datetime,                                   
@SelectedSystemIDs nvarchar (2000) = NULL,    
@SelectedTsbIDs nvarchar (2000) = NULL,   
@UserRoleID int
)                            
AS                            

DECLARE @InstrumentType int = NULL    
DECLARE @PlatformID int = null    

IF (@SelectedSystemIDs = '')                        
  begin        
      SET @SelectedSystemIDs = NULL         
  END         

IF (@SelectedTsbIDs = '')                        
  begin        
      SET @SelectedTsbIDs = NULL         
 END         

IF(@UserRoleID = 1)    
 BEGIN     
  SET @PlatformID = 1     
  END  

IF(@UserRoleID = 2)  
BEGIN  
SET @PlatformID = 2  
END  

IF (@UserRoleID = 3)    
BEGIN    
SET @PlatformID = 12    
END   

IF(@UserRoleID = 4)
BEGIN 
SET @PlatformID = 3
END

IF(@UserRoleID = 5)
BEGIN 
SET @PlatformID = 4
END

IF(@UserRoleID = 6)
BEGIN 
SET @PlatformID = NULL
END


DECLARE @PivotColumnHeaders NVARCHAR(MAX)

SELECT @PivotColumnHeaders = 
   COALESCE(
     @PivotColumnHeaders + ',[' + cast(SystemFullName as Nvarchar) + ']',
     '[' + cast(SystemFullName as varchar)+ ']'
   )
FROM System
DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
   SELECT *
   FROM (
     SELECT
       TSBNumber [TSBNumber],
       SystemFullName,
       ClosedDate
     FROM ServiceEntry 
     INNER JOIN System 
       ON ServiceEntry.SystemID = System.ID
     Group By TSBNumber, SystemFullName, ClosedDate
   ) AS PivotData
   PIVOT (
     max(ClosedDate)
     FOR SystemFullName IN (
       ' + @PivotColumnHeaders + '
     )
   ) AS PivotTable
'
EXECUTE(@PivotTableSQL)

1 个答案:

答案 0 :(得分:0)

您可以构造所需的条件作为存储过程的另一个输入,并将其附加到动态查询

ALTER PROCEDURE  [dbo].[spExportStuff]                            
(@StartDate datetime,                            
@EndDate datetime,                                   
@SelectedSystemIDs nvarchar (2000) = NULL,    
@SelectedTsbIDs nvarchar (2000) = NULL,   
@UserRoleID int,
@WhereClause varchar(max)  -> this is the new parameter.
)      


DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
   SELECT *
   FROM (
     SELECT
       TSBNumber [TSBNumber],
       SystemFullName,
       ClosedDate
     FROM ServiceEntry 
     INNER JOIN System 
       ON ServiceEntry.SystemID = System.ID
     Group By TSBNumber, SystemFullName, ClosedDate
   ) AS PivotData
   PIVOT (
     max(ClosedDate)
     FOR SystemFullName IN (
       ' + @PivotColumnHeaders + '
     )
   ) AS PivotTable
' + @WhereClause   -> you can append the where clause here.

EXECUTE(@PivotTableSQL)