-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39_dynamic_sql_inside_stored_procedure.sql
89 lines (73 loc) · 2.05 KB
/
39_dynamic_sql_inside_stored_procedure.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
--select top 10 * from Sample_Superstore
--question
--search for records based on different columns?
--segment,customer_id,state,category
/*
create procedure search
@segment nvarchar(200),
@customer_id nvarchar(200),
@state nvarchar(200),
@category nvarchar(200)
as
declare @seg nvarchar(200) = @segment
declare @cust_id nvarchar(200) = @customer_id
declare @stt nvarchar(200) = @state
declare @cat nvarchar(200) = @category
declare @sql nvarchar(500) = 'SELECT * FROM
Sample_Superstore
WHERE Segment = @seg
AND Customer_ID = @cust_id
AND State = @stt
AND Category = @cat'
print(@sql)
print(@seg)
print(@segment)
print(@cust_id)
print(@customer_id)
print(@stt)
print(@state)
execute sp_executesql @sql,
N'@seg nvarchar(200),@cust_id nvarchar(200),@stt nvarchar(200),@cat nvarchar(200)',
@seg = @segment,
@cust_id = @customer_id,
@stt = @state,
@cat = @category
*/
--drop procedure search
/*
exec search
@segment = 'Consumer',
@customer_id = 'CG-12520',
@state = 'Kentucky',
@category = 'Furniture'
*/
--method 2
--query is dynamic
--not necessary to give all filter values
--declare @sql nvarchar(200) = '''hello text''%'
--print(@sql)
--select top 2 * from Sample_Superstore
create procedure test
@customer nvarchar(200) = null
,@segment nvarchar(200) = null
as
declare @customerFilter nvarchar(200)
declare @segmentFilter nvarchar(200)
set @customerFilter = case when @customer is null --or @customer = 0
then '1 = 1'
else 'Customer_ID ='''+ @customer+''''
end
set @segmentFilter = case when @segment is null --or @segment = 0
then '1 = 1'
else 'Segment ='''+@segment+''''
end
declare @sql nvarchar(500)
set @sql = 'SELECT * FROM Sample_Superstore
WHERE 1 = 1 AND ' + @customerFilter + 'AND ' + @segmentFilter + ''
print(@sql)
exec(@sql)
--executing the procedure
exec test
--@customer = null ,
@segment = 'Consumer'
--select top 2 * from Sample_Superstore