-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_90 Kitchen sink Dynamic.sql
62 lines (58 loc) · 2.38 KB
/
12_90 Kitchen sink Dynamic.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
ALTER PROCEDURE [dbo].[CustomerSearch_DynamicSQL]
@CustomerID int = NULL,
@CustomerName nvarchar(100) = NULL,
@BillToCustomerID int = NULL,
@CustomerCategoryID int = NULL,
@BuyingGroupID int = NULL,
@MinAccountOpenedDate date = NULL,
@MaxAccountOpenedDate date = NULL,
@DeliveryCityID int = NULL,
@IsOnCreditHold bit = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql nvarchar(max) = N'
SELECT CustomerID, CustomerName, IsOnCreditHold
FROM Sales.Customers WHERE 1 = 1'
+ CASE WHEN @CustomerID IS NOT NULL THEN
N' AND CustomerID = @CustomerID' ELSE N'' END
+ CASE WHEN @CustomerName IS NOT NULL THEN
N' AND CustomerName LIKE @CustomerName' ELSE N'' END
+ CASE WHEN @CustomerName IS NOT NULL THEN
N' AND CustomerName LIKE @CustomerName' ELSE N'' END
+ CASE WHEN @BillToCustomerID IS NOT NULL THEN
N' AND BillToCustomerID = @BillToCustomerID' ELSE N'' END
+ CASE WHEN @CustomerCategoryID IS NOT NULL THEN
N' AND CustomerCategoryID = @CustomerCategoryID' ELSE N'' END
+ CASE WHEN @BuyingGroupID IS NOT NULL THEN
N' AND BuyingGroupID = @BuyingGroupID' ELSE N'' END
+ CASE WHEN @MinAccountOpenedDate IS NOT NULL THEN
N' AND AccountOpenedDate >= @MinAccountOpenedDate' ELSE N'' END
+ CASE WHEN @MaxAccountOpenedDate IS NOT NULL THEN
N' AND AccountOpenedDate <= @MaxAccountOpenedDate' ELSE N'' END
+ CASE WHEN @DeliveryCityID IS NOT NULL THEN
N' AND DeliveryCityID = @DeliveryCityID' ELSE N'' END
+ CASE WHEN @IsOnCreditHold IS NOT NULL THEN
N' AND IsOnCreditHold = @IsOnCreditHold' ELSE N'' END;
DECLARE @params nvarchar(max) = N'
@CustomerID int,
@CustomerName nvarchar(100),
@BillToCustomerID int,
@CustomerCategoryID int,
@BuyingGroupID int,
@MinAccountOpenedDate date,
@MaxAccountOpenedDate date,
@DeliveryCityID int,
@IsOnCreditHold bit';
PRINT @sql;
EXEC sys.sp_executesql @sql, @params,
@CustomerID,
@CustomerName,
@BillToCustomerID,
@CustomerCategoryID,
@BuyingGroupID,
@MinAccountOpenedDate,
@MaxAccountOpenedDate,
@DeliveryCityID,
@IsOnCreditHold;
END