-
Notifications
You must be signed in to change notification settings - Fork 0
/
6) VARIABLES-LOOPS.sql
121 lines (85 loc) · 1.65 KB
/
6) VARIABLES-LOOPS.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
-- VARIABLES
--1) SET
DECLARE @isim nvarchar(20)
SET @isim = 'Emre'
PRINT @isim
GO
DECLARE @dogumTarihi datetime
--SET @dogumTarihi = BirthDate FROM Employees WHERE EmployeeID = 1 --HATA VERIR.
SET @dogumTarihi = (SELECT BirthDate FROM Employees WHERE EmployeeID = 1)
PRINT @dogumTarihi
GO
--2) SELECT
DECLARE @sayi int
SELECT @sayi = 15
PRINT @sayi
GO
DECLARE @dogumTarihi datetime
SELECT @dogumTarihi = BirthDate FROM Employees WHERE EmployeeID = 1
PRINT @dogumTarihi
GO
--LOOPS
-- IF
IF((SELECT AVG(UnitPrice) FROM Products) < 50)
BEGIN
UPDATE Products
SET UnitPrice = UnitPrice + (UnitPrice * 0.05)
END
ELSE
BEGIN
UPDATE Products
SET UnitPrice = UnitPrice - (UnitPrice * 0.1)
END
GO
DECLARE @ortalama money
DECLARE @oran float
SELECT @ortalama = AVG(UnitPrice) FROM Products
IF(@ortalama < 50)
BEGIN
SET @oran = 1.05
END
ELSE
BEGIN
SET @oran = 0.9
END
UPDATE Products
SET UnitPrice = UnitPrice * @oran
GO
--> EXISTS IF ILE BIRLIKTE KULLANILIR TRUE YADA FALSE DONER.
IF(NOT EXISTS(SELECT * FROM Employees WHERE FirstName = 'Emre' AND LastName = 'KISABOYUN'))
BEGIN
INSERT INTO Employees
(
FirstName,
LastName
)
VALUES
(
'Emre',
'KISABOYUN'
)
END
ELSE
BEGIN
SELECT * FROM Employees WHERE FirstName = 'Emre' AND LastName = 'KISABOYUN'
END
GO
-- CASE - WHEN
SELECT ProductName , UnitsInStock ,
CASE
WHEN UnitsInStock < 10
THEN 'Kritik Stok'
WHEN UnitsInStock >= 10
THEN 'Problem Yok'
END AS [ Stok Durumu]
FROM Products ;
GO
-- WHILE
DECLARE @sayi int = 1 -- BU SEKILDE DOGRUDAN DEGISKENE DEGER ATANABILIR.
--SET @sayi = 1
WHILE(@sayi <= 10)
BEGIN
PRINT @sayi
SET @sayi +=1
END
GO