以下是我的存储过程。
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参数为null,那么我想删除where条件。
如果@StartDate和@EndDate参数存在则应用于条件然后我该怎么做?
答案 0 :(得分:1)
您只需修改WHERE条件:
where @StartDate IS NULL OR @EndDate IS NULL OR
U.ModifiedDate BETWEEN @StartDate AND @EndDate
为防止parameter sniffing(一个用于不同参数值的缓存执行计划),我建议您在过程(WITH RECOMPILE
)或查询级别上使用重新编译提示。 OPTION (RECOMPILE);
。
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分:0)
试试这个......
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
let finalNumber = numberFormatter.number(from: "\(1e+07)")
print(finalNumber!)
答案 4 :(得分:0)
我只是在调用存储过程之前检查'TEMP_DATA_TABLE'是否为null,如果它不为null则删除该表。它有效。
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