-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31_Basics.sql
More file actions
228 lines (206 loc) · 6.15 KB
/
31_Basics.sql
File metadata and controls
228 lines (206 loc) · 6.15 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**************************************************************
* SQL Server 2022 Basic Concepts Tutorial
* Description: This script demonstrates basic Transact-SQL concepts:
* - Table creation and data insertion.
* - Date/time conversion using AT TIME ZONE.
* - Query hints with the OPTION clause.
* - OUTPUT clause for capturing DML results.
* - Text data manipulation with READTEXT, UPDATETEXT, and WRITETEXT.
* - Search conditions in WHERE clauses.
* - Table value constructors.
* - TOP clause.
* - Common Table Expressions (CTEs) and nested CTEs.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Ensure you are using the target database.
*/
USE TestDB;
GO
-------------------------------------------------
-- Region: 1. Creating and Populating Sample Table
-------------------------------------------------
/*
Create a sample table for demonstration.
*/
IF OBJECT_ID(N'dbo.SampleData', N'U') IS NOT NULL
DROP TABLE dbo.SampleData;
GO
CREATE TABLE dbo.SampleData
(
ID INT PRIMARY KEY,
Name NVARCHAR(100),
Age INT,
Salary DECIMAL(10, 2),
Department NVARCHAR(100),
Notes TEXT
);
GO
/*
Insert sample data.
*/
INSERT INTO dbo.SampleData (ID, Name, Age, Salary, Department, Notes)
VALUES
(1, 'Alice', 30, 60000.00, 'HR', 'Initial note for Alice'),
(2, 'Bob', 25, NULL, 'IT', 'Initial note for Bob'),
(3, 'Charlie', 35, 80000.00, 'Finance', 'Initial note for Charlie'),
(4, 'David', 40, 90000.00, 'IT', 'Initial note for David'),
(5, 'Eve', 28, NULL, 'HR', 'Initial note for Eve');
GO
-------------------------------------------------
-- Region: 2. AT TIME ZONE
-------------------------------------------------
/*
Convert the current date/time to different time zones.
*/
SELECT
Name,
GETDATE() AS CurrentTime,
GETDATE() AT TIME ZONE 'UTC' AS UTCTime,
GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time' AS PSTTime
FROM dbo.SampleData;
GO
-------------------------------------------------
-- Region: 3. OPTION Clause
-------------------------------------------------
/*
Use the OPTION clause to specify a query hint.
*/
SELECT
Name,
Salary
FROM dbo.SampleData
WHERE Department = 'IT'
OPTION (MAXDOP 1);
GO
-------------------------------------------------
-- Region: 4. OUTPUT Clause
-------------------------------------------------
/*
Capture the output of an INSERT statement using the OUTPUT clause.
*/
DECLARE @InsertedData TABLE (ID INT, Name NVARCHAR(100));
INSERT INTO dbo.SampleData (ID, Name, Age, Salary, Department, Notes)
OUTPUT INSERTED.ID, INSERTED.Name INTO @InsertedData
VALUES (6, 'Frank', 32, 70000.00, 'Marketing', 'Initial note for Frank');
SELECT * FROM @InsertedData;
GO
-------------------------------------------------
-- Region: 5. READTEXT
-------------------------------------------------
/*
Read a portion of the text in the Notes column for the record with ID = 1.
*/
DECLARE @TextPointer VARBINARY(16);
SELECT @TextPointer = TEXTPTR(Notes) FROM dbo.SampleData WHERE ID = 1;
READTEXT dbo.SampleData.Notes @TextPointer 0 10;
GO
-------------------------------------------------
-- Region: 6. Search Condition
-------------------------------------------------
/*
Use a search condition in the WHERE clause.
*/
SELECT
Name,
Salary
FROM dbo.SampleData
WHERE Salary IS NOT NULL AND Department = 'IT';
GO
-------------------------------------------------
-- Region: 7. Table Value Constructor
-------------------------------------------------
/*
Insert multiple rows using a table value constructor.
*/
INSERT INTO dbo.SampleData (ID, Name, Age, Salary, Department, Notes)
VALUES
(7, 'Grace', 29, 75000.00, 'Sales', 'Initial note for Grace'),
(8, 'Hank', 33, 85000.00, 'Sales', 'Initial note for Hank');
GO
-------------------------------------------------
-- Region: 8. TOP Clause
-------------------------------------------------
/*
Select the top 3 rows based on Salary in descending order.
*/
SELECT TOP 3
Name,
Salary
FROM dbo.SampleData
ORDER BY Salary DESC;
GO
-------------------------------------------------
-- Region: 9. UPDATETEXT
-------------------------------------------------
/*
Update a portion of the text in the Notes column for record with ID = 1.
Replace the first 10 characters with 'Updated note for Alice'.
*/
DECLARE @TextPointer2 VARBINARY(16);
SELECT @TextPointer2 = TEXTPTR(Notes) FROM dbo.SampleData WHERE ID = 1;
UPDATETEXT dbo.SampleData.Notes @TextPointer2 0 10 'Updated note for Alice';
GO
-------------------------------------------------
-- Region: 10. WITH Common Table Expression (CTE)
-------------------------------------------------
/*
Use a CTE to filter and return IT department employees.
*/
WITH EmployeeCTE AS
(
SELECT
Name,
Salary
FROM dbo.SampleData
WHERE Department = 'IT'
)
SELECT * FROM EmployeeCTE;
GO
-------------------------------------------------
-- Region: 11. Nested Common Table Expressions
-------------------------------------------------
/*
Use nested CTEs to further filter IT department employees with Salary > 80000.
*/
WITH CTE1 AS
(
SELECT
Name,
Salary
FROM dbo.SampleData
WHERE Department = 'IT'
),
CTE2 AS
(
SELECT
Name,
Salary
FROM CTE1
WHERE Salary > 80000.00
)
SELECT * FROM CTE2;
GO
-------------------------------------------------
-- Region: 12. WRITETEXT
-------------------------------------------------
/*
Write a new value to the text column for record with ID = 2.
*/
DECLARE @TextPointer3 VARBINARY(16);
SELECT @TextPointer3 = TEXTPTR(Notes) FROM dbo.SampleData WHERE ID = 2;
WRITETEXT dbo.SampleData.Notes @TextPointer3 'Completely new note for Bob';
GO
-------------------------------------------------
-- Region: 13. Cleanup
-------------------------------------------------
/*
Clean up the sample table.
*/
DROP TABLE dbo.SampleData;
GO
-------------------------------------------------
-- End of Script
-------------------------------------------------