0

下面是我的存储过程。

USE [CBRDev]
GO
/****** Object:  StoredProcedure [dbo].[usp_ExportData]    Script Date: 6/14/2017 12:48:44 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[usp_ExportData]
(
@StartDate Date,
@EndDate Date
)

AS
BEGIN
 Select REPLACE(U.EmployeeID, ',','') as EmployeeID,REPLACE(U.ClientID, ',','') as ClientID,REPLACE(U.ID, ',','') as ID,
 REPLACE(U.FirstName, ',','')as FirstName,REPLACE(U.MiddleName, ',','')as MiddleName,
 REPLACE(U.LastName, ',','')as LastName,REPLACE(U.Email, ',','')as Email,REPLACE(U.SSN, ',','')as SSN,
 REPLACE(U.DateOfBirth, ',','')as DateOfBirth,REPLACE(U.Gender, ',','')as Gender,REPLACE(U.CreatedDate, ',','')as CreatedDate,
REPLACE(U.ModifiedDate, ',','')as ModifiedDate, REPLACE(UPI.StreetAddress1, ',','')as StreetAddress1,
REPLACE(UPI.StreetAddress2, ',','')as StreetAddress2,REPLACE(UPI.City, ',','')as City,
REPLACE(UPI.State, ',','')as State,
REPLACE(UPI.ZipCode, ',','')as ZipCode,
REPLACE(UPI.CellPhoneNumber, ',','')as CellPhoneNumber, '' as Department, '' as JobTitle, '' as StreetAddress3 from Users U INNER JOIN PersonalContacts UPI ON U.ID= UPI.UserID
 where U.ModifiedDate >=@StartDate and U.ModifiedDate<= @EndDate
END

其中如果@StartDate 和@EndDate 参数为空,那么我想删除where 条件。

如果存在 @StartDate 和 @EndDate 参数,则应用 where 条件,那么我该怎么做?

4

5 回答 5

1

您可以只修改您的 WHERE 条件:

where @StartDate IS NULL OR @EndDate IS NULL OR 
      U.ModifiedDate BETWEEN @StartDate AND @EndDate

为了防止参数嗅探(用于不同参数值的一个缓存执行计划),我建议您在过程(WITH RECOMPILE)或查询级别使用重新编译提示。OPTION (RECOMPILE);.

于 2017-06-14T07:35:50.080 回答
0
USE [CBRDev]    
GO  
/****** Object:  StoredProcedure [dbo].[usp_ExportData] Script Date: 
6/14/2017 12:48:44 PM ******/
SET ANSI_NULLS ON  
GO  
SET QUOTED_IDENTIFIER ON  
GO  
ALTER procedure [dbo].[usp_ExportData]  
(
   @StartDate Date,
   @EndDate Date  
)

AS
BEGIN

Select REPLACE(U.EmployeeID, ',','') as EmployeeID,
REPLACE(U.ClientID, ',','') as ClientID, 
REPLACE(U.ID, ',','') as ID, REPLACE(U.FirstName, ',','')as FirstName, 
REPLACE(U.MiddleName, ',','')as MiddleName,
REPLACE(U.LastName, ',','')as LastName,
REPLACE(U.Email, ',','')as Email,REPLACE(U.SSN, ',','')as SSN,
REPLACE(U.DateOfBirth, ',','')as DateOfBirth,
REPLACE(U.Gender, ',','')as Gender,
REPLACE(U.CreatedDate, ',','')as CreatedDate,
REPLACE(U.ModifiedDate, ',','')as ModifiedDate, 
REPLACE(UPI.StreetAddress1, ',','')as StreetAddress1,
REPLACE(UPI.StreetAddress2, ',','')as StreetAddress2,
REPLACE(UPI.City, ',','')as City,
REPLACE(UPI.State, ',','')as State,
REPLACE(UPI.ZipCode, ',','')as ZipCode,
REPLACE(UPI.CellPhoneNumber, ',','')as CellPhoneNumber, '' as Department, '' 
as JobTitle, '' as StreetAddress3   
INTO #NEW_TABLE   
FROM Users U INNER JOIN PersonalContacts UPI ON U.ID= UPI.UserID  

IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL)  
  SELECT * FROM #NEW_TABLE  
  where ModifiedDate >=@StartDate and ModifiedDate<= @EndDate   
ELSE  
  SELECT * FROM #NEW_TABLE   
END
于 2017-06-14T07:48:18.810 回答
0

尝试这个....

ALTER procedure [dbo].[usp_ExportData]
(
@StartDate Date,
@EndDate Date
)
AS
BEGIN
declare @sql varchar(max)
set @sql = ''
set @sql += ' Select REPLACE(U.EmployeeID, '','','') as 
EmployeeID,REPLACE(U.ClientID, '','','') as ClientID,REPLACE(U.ID, '','','') as ID,
REPLACE(U.FirstName, '','','')as FirstName,REPLACE(U.MiddleName, '','','')as MiddleName,
REPLACE(U.LastName, '','','')as LastName,REPLACE(U.Email, ',','')as 
Email,REPLACE(U.SSN, '','','')as SSN,
REPLACE(U.DateOfBirth, '','','')as DateOfBirth,REPLACE(U.Gender, '','','')as
Gender,REPLACE(U.CreatedDate, '','','')as CreatedDate,
REPLACE(U.ModifiedDate, '','','')as ModifiedDate, REPLACE(UPI.StreetAddress1,
 '','','')as StreetAddress1,
REPLACE(UPI.StreetAddress2, '','','')as StreetAddress2,REPLACE(UPI.City, '','','')as
 City,
REPLACE(UPI.State, '','','')as State,
REPLACE(UPI.ZipCode, '','','')as ZipCode,
REPLACE(UPI.CellPhoneNumber, '','','')as CellPhoneNumber, '' as Department, '' as 
JobTitle, '' as StreetAddress3 from Users U INNER JOIN PersonalContacts UPI ON
 U.ID= UPI.UserID'

if(@StartDate <> '' or @EndDate <> '')set @sql += ' where U.ModifiedDate >='''+@StartDate+''' and U.ModifiedDate<= '''+@EndDate+''''

exec(@sql)
END
于 2017-06-14T08:05:45.077 回答
0

我只是在调用存储过程之前检查“TEMP_DATA_TABLE”是否为空,如果它不为空,则删除该表。它有效。

USE [CBRDev]    
GO  
/****** Object:  StoredProcedure [dbo].[usp_ExportData] Script Date: 
6/14/2017 12:48:44 PM ******/
SET ANSI_NULLS ON  
GO  
SET QUOTED_IDENTIFIER ON  
GO  
ALTER procedure [dbo].[usp_ExportData]  
(
   @StartDate Date,
   @EndDate Date  
)

AS
BEGIN

if (select OBJECT_ID('TEMP_DATA_TABLE')) Is Not Null
Begin 
DROP Table TEMP_DATA_TABLE
end
Select REPLACE(U.EmployeeID, ',','') as EmployeeID,
REPLACE(U.ClientID, ',','') as ClientID, 
REPLACE(U.ID, ',','') as ID, REPLACE(U.FirstName, ',','')as FirstName, 
REPLACE(U.MiddleName, ',','')as MiddleName,
REPLACE(U.LastName, ',','')as LastName,
REPLACE(U.Email, ',','')as Email,REPLACE(U.SSN, ',','')as SSN,
REPLACE(U.DateOfBirth, ',','')as DateOfBirth,
REPLACE(U.Gender, ',','')as Gender,
REPLACE(U.CreatedDate, ',','')as CreatedDate,
REPLACE(U.ModifiedDate, ',','')as ModifiedDate, 
REPLACE(UPI.StreetAddress1, ',','')as StreetAddress1,
REPLACE(UPI.StreetAddress2, ',','')as StreetAddress2,
REPLACE(UPI.City, ',','')as City,
REPLACE(UPI.State, ',','')as State,
REPLACE(UPI.ZipCode, ',','')as ZipCode,
REPLACE(UPI.CellPhoneNumber, ',','')as CellPhoneNumber, '' as Department, '' 
as JobTitle, '' as StreetAddress3   
INTO TEMP_DATA_TABLE   
FROM Users U INNER JOIN PersonalContacts UPI ON U.ID= UPI.UserID  

IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL)  
  SELECT * FROM TEMP_DATA_TABLE 
  where ModifiedDate >=@StartDate and ModifiedDate<= @EndDate   
ELSE  
  SELECT * FROM TEMP_DATA_TABLE  
END
于 2017-06-14T08:31:02.357 回答
0
ALTER procedure [dbo].[usp_ExportData]
(
@StartDate Date,
@EndDate Date
)
AS
BEGIN
declare @sql varchar(max)
set @sql = ''
set @sql += 'Select REPLACE(U.EmployeeID, ',','') as 
EmployeeID,REPLACE(U.ClientID, ',','') as ClientID,REPLACE(U.ID, ',','') as ID,
REPLACE(U.FirstName, ',','')as FirstName,REPLACE(U.MiddleName, ',','')as MiddleName,
REPLACE(U.LastName, ',','')as LastName,REPLACE(U.Email, ',','')as 
Email,REPLACE(U.SSN, ',','')as SSN,
REPLACE(U.DateOfBirth, ',','')as DateOfBirth,REPLACE(U.Gender, ',','')as
Gender,REPLACE(U.CreatedDate, ',','')as CreatedDate,
REPLACE(U.ModifiedDate, ',','')as ModifiedDate, REPLACE(UPI.StreetAddress1,
 ',','')as StreetAddress1,
REPLACE(UPI.StreetAddress2, ',','')as StreetAddress2,REPLACE(UPI.City, ',','')as
 City,
REPLACE(UPI.State, ',','')as State,
REPLACE(UPI.ZipCode, ',','')as ZipCode,
REPLACE(UPI.CellPhoneNumber, ',','')as CellPhoneNumber, '' as Department, '' as 
JobTitle, '' as StreetAddress3 from Users U INNER JOIN PersonalContacts UPI ON
 U.ID= UPI.UserID'
 if(@StartDate and @EndDate <> '')set @sql += 'where U.ModifiedDate
 >=''+@StartDate+'' and U.ModifiedDate<= ''+@EndDate'''
exec(@sql)
END
于 2017-06-14T07:45:14.953 回答