-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun.sql
69 lines (57 loc) · 1.63 KB
/
fun.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
-- CASE 1
-- scalar-valued function => in which case the result of the defined type returns
-- Praduct table ListPrice and ListPrice find the difference
CREATE FUNCTION SCOST_LPRICE_DIFF(
@PraductId INT
)
RETURNS Numeric(10,2) -- INT
AS
BEGIN
DECLARE @ans INT =0
SET @ans = (SELECT ListPrice-StandardCost FROM Product WHERE ProductID = @PraductId)
RETURN @ans
END
-- run fun
SELECT dbo.SCOST_LPRICE_DIFF(750)
-- CASE 2
-- inline table-valued function -> as a result the table returns
-- The view difference parameter is present in the function
-- search like praduct name
CREATE FUNCTION SEARCH_PRADUCT(
@SEARCH_TX VARCHAR(200)
)
RETURNS TABLE AS
RETURN (
SELECT *FROM Product WHERE ProductName LIKE '%'+@SEARCH_TX+'%'
)
-- run fun
-- search like praduct name
SELECT *FROM dbo.SEARCH_PRADUCT('Road');
-- CASE 3
-- multi-statement table-valued function -> The result returns to the table with only the variable
-- Order once @start_year Date of first and last order of products
CREATE FUNCTION PRADUCT_FIRST_ODATE(
@start_year VARCHAR(200)
)
RETURNS @ans TABLE(
ProductID int ,
FirstOrder datetime ,
LastOrder datetime
)
AS
begin
with cte AS (
SELECT ProductID ,
MIN(OrderDate) AS FirstOrder,
MAX(OrderDate) AS LastOrder
FROM SalesOrderDetail SD INNER JOIN SalesOrderHeader SOH on SD.SalesOrderID = SOH.SalesOrderID
GROUP BY ProductID
)
INSERT @ans SELECT *FROM cte WHERE YEAR(FirstOrder)=@start_year
return
end
-- run fun
-- search like praduct name
SELECT *FROM dbo.PRADUCT_FIRST_ODATE('2011');
-- ALTER FUNCTION - update function
-- DROP FUNCTION - delete function