Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Chapter 13: User-Defined Functions Summary What we added in this chapter was, in many ways, not new at all. Indeed, much of what goes into userdefined functions is the same set of statements, variables, and general coding practices that we have already seen in scripting and stored procedures. However, UDFs still provide us a wonderful new area of functionality that was not previously available in SQL Server. We can now encapsulate a wider range of code, and even use this encapsulated functionality inline with our queries. What’s more, we can now also provide parameterized views and dynamically created tables. User-defined functions are, in many ways, the most exciting of all the new functionality added to SQL Server. In pondering their uses, I have already come to realize that I’m only scratching the surface of their potential. Over the life of this next release, I suspect that developers will implement UDFs in ways I have yet to dream of — let’s hope you’ll be one of those developers! Exercise 1. Reimplement the spTriangular function from Chapter 12 as a function instead of a stored procedure. 426
14 Transactions and Locks This is one of those chapters that, when you go back to work, makes you sound like you’ve had your Wheaties today. Nothing we’re going to cover in this chapter is wildly difficult, yet transactions and locks tend to be two of the most misunderstood areas in the database world. As such, this beginning (or at least I think it’s a basic) concept is going to make you start to look like a real pro. In this chapter, we’re going to: ❑ Demystify transactions ❑ Examine how the SQL Server log and checkpoints work ❑ Unlock your understanding of locks We’ll learn why these topics are so closely tied to each other and how to minimize problems with each. Transactions Transactions are all about atomicity. Atomicity is the concept that something should act as a unit. From our database standpoint, it’s about the smallest grouping of one or more statements that should be considered to be all or nothing. Often, when dealing with data, we want to make sure that if one thing happens, another thing happens, or that neither of them does. Indeed, this can be carried out to the degree where 20 things (or more) all have to happen together or nothing happens. Let’s look at a classic example. Imagine that you are a banker. Sally comes in and wants to transfer $1,000 from checking to savings. You are, of course, happy to oblige, so you process her request.
- Page 414 and 415: Chapter 12: Stored Procedures Confi
- Page 416 and 417: Chapter 12: Stored Procedures Now,
- Page 418 and 419: Chapter 12: Stored Procedures SQL S
- Page 420 and 421: Chapter 12: Stored Procedures 382 c
- Page 422 and 423: Chapter 12: Stored Procedures It wo
- Page 424 and 425: Chapter 12: Stored Procedures 386 r
- Page 426 and 427: Chapter 12: Stored Procedures 388 -
- Page 428 and 429: Chapter 12: Stored Procedures Note
- Page 430 and 431: Chapter 12: Stored Procedures 392 n
- Page 432 and 433: Chapter 12: Stored Procedures All t
- Page 434 and 435: Chapter 12: Stored Procedures Sproc
- Page 436 and 437: Chapter 12: Stored Procedures When
- Page 438 and 439: Chapter 12: Stored Procedures 400 @
- Page 440 and 441: Chapter 12: Stored Procedures I’d
- Page 442 and 443: Chapter 12: Stored Procedures match
- Page 444 and 445: Chapter 12: Stored Procedures There
- Page 446 and 447: Chapter 12: Stored Procedures 408 f
- Page 449 and 450: 13 User-Defined Functions Well, her
- Page 451 and 452: types!), except for BLOBs, cursors,
- Page 453 and 454: We get back the same set as with th
- Page 455 and 456: AS RETURN (SELECT BusinessEntityID,
- Page 457 and 458: in your relational database. These
- Page 459 and 460: AS BEGIN ( EmployeeID int NOT NULL,
- Page 461 and 462: So, as you can see, we can actually
- Page 463: Despite being schema-bound, this on
- Page 467 and 468: we are unable or do not want to com
- Page 469 and 470: Figure 14-1 Data needed Data in cac
- Page 471 and 472: Transaction 4 This transaction wasn
- Page 473 and 474: Oops — problem!!! Transaction 2 h
- Page 475 and 476: The only cure for this is setting y
- Page 477 and 478: Exclusive Locks Exclusive locks are
- Page 479 and 480: Also: ❑ The Sch-S is compatible w
- Page 481 and 482: The syntax for switching between th
- Page 483 and 484: As with most things in life, howeve
- Page 485 and 486: purchased. Process 2 records sales;
- Page 487: Chapter 14: Transactions and Locks
- Page 490 and 491: Chapter 15: Triggers the world’s
- Page 492 and 493: Chapter 15: Triggers WITH ENCRYPTIO
- Page 494 and 495: Chapter 15: Triggers FOR|AFTER The
- Page 496 and 497: Chapter 15: Triggers 458 To illustr
- Page 498 and 499: Chapter 15: Triggers 460 IF EXISTS
- Page 500 and 501: Chapter 15: Triggers ❑ Feeding de
- Page 502 and 503: Chapter 15: Triggers Trigger Firing
- Page 504 and 505: Chapter 15: Triggers Like regular t
- Page 506 and 507: Chapter 15: Triggers The COLUMNS_UP
- Page 508 and 509: Chapter 15: Triggers This is the sa
- Page 510 and 511: Chapter 15: Triggers 472 we have th
- Page 512 and 513: Chapter 16: A Brief XML Primer So,
14<br />
Transactions and Locks<br />
This is one of those chapters that, when you go back to work, makes you sound like you’ve had<br />
your Wheaties today. Nothing we’re going to cover in this chapter is wildly difficult, yet transactions<br />
and locks tend to be two of the most misunderstood areas in the database world. As such,<br />
this beginning (or at least I think it’s a basic) concept is going to make you start to look like a<br />
real pro.<br />
In this chapter, we’re going to:<br />
❑ Demystify transactions<br />
❑ Examine how the <strong>SQL</strong> <strong>Server</strong> log and checkpoints work<br />
❑ Unlock your understanding of locks<br />
We’ll learn why these topics are so closely tied to each other and how to minimize problems<br />
with each.<br />
Transactions<br />
Transactions are all about atomicity. Atomicity is the concept that something should act as a unit.<br />
From our database standpoint, it’s about the smallest grouping of one or more statements that<br />
should be considered to be all or nothing.<br />
Often, when dealing with data, we want to make sure that if one thing happens, another thing<br />
happens, or that neither of them does. Indeed, this can be carried out to the degree where 20 things<br />
(or more) all have to happen together or nothing happens. Let’s look at a classic example.<br />
Imagine that you are a banker. Sally comes in and wants to transfer $1,000 from checking to savings.<br />
You are, of course, happy to oblige, so you process her request.