/
Chapter11.txt
398 lines (329 loc) · 10.6 KB
/
Chapter11.txt
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* Chapter 11 */
/* Try it out - Joining Two Tables - Step 1 */
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
/* Try it out - Joining Two Tables - Step 3 */
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
AND sp.Price = s.CurrentPrice
/* Try it out - Joining Two Tables - Step 5 */
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
LEFT OUTER JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
/* Try it out - Joining Two Tables - Step 7 */
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
LEFT OUTER JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
WHERE sp.Price IS NULL
/* Try it out - Joining Two Tables - Step 9 */
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.SharePrices sp
RIGHT OUTER JOIN ShareDetails.Shares s ON sp.ShareId = s.ShareId
/* Try it out - Joining Two Tables - Step 11 */
INSERT INTO ShareDetails.SharePrices
(ShareId, Price, PriceDate)
VALUES (99999,12.34,'1 Aug 2008 10:10AM')
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.SharePrices sp
FULL OUTER JOIN ShareDetails.Shares s ON sp.ShareId = s.ShareId
/* Try it out - Joining Two Tables - Step 13 */
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.SharePrices sp
CROSS JOIN ShareDetails.Shares s
/* Try it out - Declaring and Working with Variables - Step 1 */
DECLARE @MyDate datetime=GETDATE(), @CurrPriceInCents money
SELECT @CurrPriceInCents = CurrentPrice * 100
FROM ShareDetails.Shares
WHERE ShareId = 2
SELECT @MyDate,@CurrPriceInCents
/* Try it out - Declaring and Working with Variables - Step 3 */
DECLARE @MyDate datetime= GETDATE(), @CurrPriceInCents money
SELECT @CurrPriceInCents = CurrentPrice * 100
FROM ShareDetails.Shares
WHERE ShareId = 2
GO
SELECT @MyDate,@CurrPriceInCents
/* Try it out - Declaring and Working with Variables - Step 5 */
DECLARE @MyDate datetime= GETDATE(), @CurrPriceInCents money
SELECT @CurrPriceInCents = CurrentPrice * 100
FROM ShareDetails.Shares
-- WHERE ShareId = 2
--GO
SELECT @MyDate,@CurrPriceInCents
/* Try it out - Temporary Tables - Step 1 */
CREATE TABLE #SharesTmp
(ShareDesc varchar(50),
Price numeric(18,5),
PriceDate datetime)
/* Try it out - Temporary Tables - Step 2 */
INSERT INTO #SharesTmp
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
/* Try it out - Temporary Tables - Step 3 and 5*/
SELECT * FROM #SharesTmp
/* Try it out - Temporary Tables - Step 7 */
DROP TABLE #SharesTmp
/* Try it out - Temporary Tables - Step 8 */
CREATE TABLE ##SharesTmp
(ShareDesc varchar(50),
Price numeric(18,5),
PriceDate datetime)
INSERT INTO ##SharesTmp
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
SELECT * FROM ##SharesTmp
/* Try it out - Temporary Tables - Step 10 */
SELECT * FROM ##SharesTmp
/* Try it out - Counting Rows - Step 1 */
SELECT COUNT(*) AS 'Number of Rows'
FROM ShareDetails.Shares
/* Try it out - Counting Rows - Step 3 */
SELECT COUNT(*) AS 'Number of Rows'
FROM ShareDetails.Shares
WHERE CurrentPrice > 10
/* Try it out - Summing Values - Step 1 */
SELECT SUM(Amount) AS 'Amount Deposited'
FROM TransactionDetails.Transactions
WHERE CustomerId = 1
AND TransactionType = 1
/* Try it out - Max and Min - Step 1 */
SELECT MAX(Price) MaxPrice,MIN(Price) MinPrice
FROM ShareDetails.SharePrices
/* Try it out - AVG - Step 1 */
SELECT AVG(Price) AvgPrice
FROM ShareDetails.SharePrices
WHERE ShareId = 1
/* Try it out - GROUP BY - Step 1 */
SELECT ShareId, MIN(Price) MinPrice, Max(Price) MaxPrice
FROM ShareDetails.SharePrices
WHERE ShareId < 9999
GROUP BY ShareId
/* Try it out - GROUP BY - Step 3 */
SELECT sp.ShareId, s.ShareDesc,MIN(Price) MinPrice, Max(Price) MaxPrice
FROM ShareDetails.SharePrices sp
LEFT JOIN ShareDetails.Shares s ON s.ShareId = sp.ShareId
WHERE sp.ShareId < 9999
GROUP BY ALL sp.ShareId, s.ShareDesc
/* Try it out - HAVING - Step 1 */
SELECT sp.ShareId, s.ShareDesc,MIN(Price) MinPrice, Max(Price) MaxPrice
FROM ShareDetails.SharePrices sp
LEFT JOIN ShareDetails.Shares s ON s.ShareId = sp.ShareId
WHERE sp.ShareId < 9999
GROUP BY ALL sp.ShareId, s.ShareDesc
HAVING MIN(Price) > 10
/* Try it out - DISTINCT VALUES - Step 1 */
SELECT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
/* Try it out - DISTINCT VALUES - Step 2 */
SELECT DISTINCT s.ShareDesc,sp.Price,sp.PriceDate
FROM ShareDetails.Shares s
JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
/* Try it out - DISTINCT VALUES - Step 3 */
SELECT DISTINCT s.ShareDesc
FROM ShareDetails.Shares s
JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId
/* Try it out - DATEADD - Step 1 */
DECLARE @OldTime datetime
SET @OldTime = '24 March 2008 3:00 PM'
SELECT DATEADD(hh,4,@OldTime)
/* Try it out - DATEADD - Step 2 */
DECLARE @OldTime datetime
SET @OldTime = '24 March 2008 3:00 PM'
SELECT DATEADD(hh,-6,@OldTime)
/* Try it out - DATEDIFF - Step 1 */
DECLARE @FirstTime datetime, @SecondTime datetime
SET @FirstTime = '24 March 2008 3:00 PM'
SET @SecondTime = '24 March 2008 3:33PM'
SELECT DATEDIFF(ms,@FirstTime,@SecondTime)
/* Try it out - DATENAME - Step 1 */
DECLARE @StatementDate datetime
SET @StatementDate = '24 March 2008 3:00 PM'
SELECT DATENAME(dw,@StatementDate)
/* Try it out - DATEPART - Step 1 */
DECLARE @WhatsTheDay datetime
SET @WhatsTheDay = '24 March 2008 3:00 PM'
SELECT DATEPART(dd, @WhatsTheDay)
/* Try it out - DATEPART - Step 2 */
DECLARE @WhatsTheDay datetime
SET @WhatsTheDay = '24 March 2008 3:00 PM'
SELECT DATENAME(dw, @WhatsTheDay) + ', ' +
CAST(DATEPART(dd,@WhatsTheDay) AS varchar(2)) + ' ' +
DATENAME(mm,@WhatsTheDay) + ' ' +
CAST(DATEPART(yyyy,@WhatsTheDay) AS char(4))
/* Try it out - ASCII - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = ASCII('Robin ')
/* Try it out - CHAR - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = ASCII('Robin ')
SELECT CHAR(@StringTest)
SELECT @StringTest
/* Try it out - CHAR - Step 3 */
DECLARE @StringTest int
SET @StringTest = ASCII('Robin ')
SELECT CHAR(@StringTest)
/* Try it out - LEFT - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = 'Robin '
SELECT LEFT(@StringTest,3)
/* Try it out - LOWER - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = 'Robin '
SELECT LOWER(LEFT(@StringTest,3))
/* Try it out - LTRIM - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = ' Robin'
SELECT 'Start-'+LTRIM(@StringTest),'Start-'+@StringTest
/* Try it out - RIGHT - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = ' Robin'
SELECT RIGHT(@StringTest,3)
/* Try it out - RTRIM - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = 'Robin'
SELECT @StringTest+'-End',RTRIM(@StringTest)+'-End'
/* Try it out - STR - Step 1 */
SELECT 'A'+82
/* Try it out - STR - Step 3 */
SELECT 'A'+STR(82)
/* Try it out - STR - Step 5 */
SELECT 'A'+LTRIM(STR(82))
/* Try it out - SUBSTR - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = 'Robin '
SELECT SUBSTRING(@StringTest,3,LEN(@StringTest))
/* Try it out - UPPER - Step 1 */
DECLARE @StringTest char(10)
SET @StringTest = 'Robin '
SELECT UPPER(@StringTest)
/* Try it out - CASE - Step 1 */
SET QUOTED_IDENTIFIER OFF
SELECT CustomerId,
CASE WHEN CreditType = 0 THEN "Debits" ELSE "Credits" END
AS TranType,SUM(Amount)
FROM TransactionDetails.Transactions t
JOIN TransactionDetails.TransactionTypes tt ON
tt.TransActionTypeId = t.TransactionType
WHERE t.DateEntered BETWEEN '1 Aug 2008' AND '31 Aug 2008'
GROUP BY CustomerId,CreditType
/* Try it out - CAST/CONVERT - Step 1 */
DECLARE @Cast int
SET @Cast = 1234
SELECT CAST(@Cast as char(10)) + '-End'
/* Try it out - CAST/CONVERT - Step 3 */
DECLARE @Convert int
SET @Convert = 5678
SELECT CONVERT(char(10),@Convert) + '-End'
/* Try it out - ISDATE - Step 1 */
DECLARE @IsDate char(15)
SET @IsDate = '31 Sep 2008'
SELECT ISDATE(@IsDate)
/* Try it out - ISDATE - Step 3 */
DECLARE @IsDate char(15)
SET @IsDate = '30 Sep 2008'
SELECT ISDATE(@IsDate)
/* Try it out - ISNULL - Step 1 */
DECLARE @IsNull char(10)
SET @IsNull = NULL
SELECT ISNULL(@IsNull,GETDATE())
/* Try it out - ISNUMERIC - Step 1 */
DECLARE @IsNum char(10)
SET @IsNum = 'Robin '
SELECT ISNUMERIC(@IsNum)
/* Try it out - ISNUMERIC - Step 2 */
DECLARE @IsNum char(10)
SET @IsNum = '1234 '
SELECT ISNUMERIC(@IsNum)
/* Try it out - RAISERROR - Step 1 */
sp_addmessage @msgnum=50001,@severity=1,
@msgtext='Customer is overdrawn'
/* Try it out - RAISERROR - Step 2 */
RAISERROR (50001,1,1)
/* Try it out - RAISERROR - Step 4 */
sp_addmessage @msgnum =50001,@severity=1,
@msgtext='Customer is overdrawn. CustomerId= %010u',@replace='replace'
/* Try it out - RAISERROR - Step 5 */
RAISERROR (50001,1,1,243)
/* Try it out - @@ERROR - Step 1 */
SELECT 100/0
SELECT @@ERROR
SELECT @@ERROR
/* Try it out - @@ERROR - Step 4 */
RAISERROR (50001,1,1,243)
SELECT @@ERROR
/* Try it out - @@ERROR - Step 6 */
RAISERROR (50001,11,1,243)
SELECT @@ERROR
/* Try it out - TRY. . .CATCH - Step 1 */
DECLARE @Probs int
BEGIN TRY
SELECT 'This will work'
SELECT @Probs='Not Right'
SELECT 10+5,
'This will also work, however the error means it will not run'
END TRY
BEGIN CATCH
SELECT 'An error has occurred at line ' +
LTRIM(STR(ERROR_LINE())) +
' with error ' + LTRIM(STR(ERROR_NUMBER())) + ' ' + ERROR_MESSAGE()
END CATCH
/* Try it out - TRY. . .CATCH - Step 3 */
DECLARE @Probs int
BEGIN TRY
SELECT 'This will work'
BEGIN TRY
SELECT @Probs='Not Right'
SELECT 10+5,
'This will also work, however the error means it will not run'
END TRY
BEGIN CATCH
SELECT 'The second catch block'
END CATCH
SELECT 'And then this will now work'
END TRY
BEGIN CATCH
SELECT 'An error has occurred at line ' +
LTRIM(STR(ERROR_LINE())) +
' with error ' + LTRIM(STR(ERROR_NUMBER())) + ' ' + ERROR_MESSAGE()
END CATCH
/* Try it out - TRY. . .CATCH - Step 5 */
DECLARE @Probs int
BEGIN TRY
SELECT 'This will work'
BEGIN TRY
SELECT * FROM #Temp
END TRY
BEGIN CATCH
SELECT 'The second catch block'
END CATCH
SELECT 'And then this will now work'
END TRY
BEGIN CATCH
SELECT 'An error has occurred at line ' +
LTRIM(STR(ERROR_LINE())) +
' with error ' + LTRIM(STR(ERROR_NUMBER())) + ' ' + ERROR_MESSAGE()
END CATCH
/* Try it out - TRY. . .CATCH - Step 7 */
DECLARE @Probs int
SELECT 'This will work'
BEGIN TRY
SELECT @Probs='Not Right'
SELECT 10+5,
'This will also work, however the error means it will not run'
END TRY
BEGIN CATCH
DECLARE @ErrMsg NVARCHAR(4000)
DECLARE @ErrSeverity INT
DECLARE @ErrState INT
SELECT 'Blimey! An error'
SELECT
@ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY(),
@ErrState = ERROR_STATE();
RAISERROR (@ErrMsg,@ErrSeverity,@ErrState)
END CATCH