2017-06-14 12 views
0

以下は私のストアドプロシージャです。使用方法SQLのwhere句のelse節

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 >[email protected] and U.ModifiedDate<= @EndDate 
END 

@StartDateパラメータと@EndDateパラメータがnullの場合、where条件を削除します。

@StartDateと@EndDateパラメータが存在する場合は、どこに条件を適用しますか?どのようにすればいいですか?

+0

開始日がある場合は終了日を、そうでなければ日付はないはずですか? – maSTAShuFu

答えて

1

あなたは自分のWHERE条件を変更することができます。

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

parameter sniffing(異なるパラメータ値のために使用されるものにキャッシュされた実行計画を)防ぐために、私はあなたが手順(WITH RECOMPILE)またはクエリ・レベルで再コンパイルのヒントを使用することを示唆しています。 OPTION (RECOMPILE);

+1

..この種の 'optional where'の場合、パラメータスニッフィングに注意してください。 –

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 
>=''[email protected]+'' and U.ModifiedDate<= ''[email protected]''' 
exec(@sql) 
END 
+0

2つのエラーが発生します。 行17: '、'の近くの構文が正しくありません。 および 行49:条件が期待されるコンテキストで、 'and'の近くに指定された非ブール型の-An式。 – sbm6070

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 >[email protected] and ModifiedDate<= @EndDate 
ELSE 
    SELECT * FROM #NEW_TABLE 
END 
+0

それは という名前です。データベースにはすでに 'TEMP_TABLE'という名前のオブジェクトがあります。 – sbm6070

+0

あなたの好みに基づいてテーブルの名前を変更することができます。 – user8158485

+0

うまく動作しました – sbm6070

0

私はちょうどそれは、そのテーブルをドロップしnullでない場合は、 'TEMP_DATA_TABLEがNULLであるかどうかを、ストアドプロシージャを呼び出す前に確認してください

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 >='''[email protected]+''' and U.ModifiedDate<= '''[email protected]+'''' 

exec(@sql) 
END 
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 

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 >[email protected] and ModifiedDate<= @EndDate 
ELSE 
    SELECT * FROM TEMP_DATA_TABLE 
END