Table of Contents

Cover

Table of Contents

Title Page

Copyright

Dedication

About the Author

Credits

Acknowledgments

Introduction

Who This Book Is For

What This Book Covers

How This Book Is Structured

What You Need to Use This Book

Conventions

Source Code

Errata

p2p.wrox.com

www.professionalsql.com

1: RDBMS Basics: What Makes Up a SQL Server Database?

An Overview of Database Objects

SQL Server Data Types

SQL Server Identifiers for Objects

Summary

2: Tools of the Trade

Books Online

SQL Server Configuration Manager

SQL Server Management Studio

SQL Server Integration Services (SSIS)

Bulk Copy Program (bcp)

SQL Server Profiler

sqlcmd

PowerShell

Summary

3: The Foundation Statements of T-SQL

Getting Started with a Basic SELECT Statement

Adding Data with the INSERT Statement

Changing What You’ve Got with the UPDATE Statement

The DELETE Statement

Summary

Exercises

4: JOINs

JOINs

INNER JOINs

OUTER JOINs

Seeing Both Sides with FULL JOINs

CROSS JOINs

Exploring Alternative Syntax for Joins

The UNION

Summary

Exercises

5: Creating and Altering Tables

Object Names in SQL Server

The CREATE Statement

The ALTER Statement

The DROP Statement

Using the GUI Tool

Summary

Exercises

6: Constraints

Types of Constraints

Constraint Naming

Key Constraints

CHECK Constraints

DEFAULT Constraints

Disabling Constraints

Rules and Defaults — Cousins of Constraints

Triggers for Data Integrity

Choosing What to Use

Summary

7: Adding More to Our Queries

What Is a Subquery?

Correlated Subqueries

Derived Tables

The EXISTS Operator

Mixing Data Types: CAST and CONVERT

The MERGE Command

Performance Considerations

Summary

Exercises

8: Being Normal: Normalization and Other Basic Design Issues

Tables

Keeping Your Data “Normal”

Relationships

Diagramming

De-Normalization

Beyond Normalization

Drawing Up a Quick Example

Summary

Exercises

9: SQL Server Storage and Index Structures

SQL Server Storage

Understanding Indexes

Creating, Altering, and Dropping Indexes

Choosing Wisely: Deciding What Index Goes Where and When

Maintaining Your Indexes

Summary

Exercises

10: Views

Simple Views

More Complex Views

Editing Views with T-SQL

Dropping Views

Creating and Editing Views in the Management Studio

Auditing: Displaying Existing Code

Protecting Code: Encrypting Views

About Schema Binding

Making Your View Look Like a Table with VIEW_METADATA

Indexed (Materialized) Views

Summary

Exercises

11: Writing Scripts and Batches

Script Basics

Batches

sqlcmd

Dynamic SQL: Generating Your Code On the Fly with the EXEC Command

Control-of-Flow Statements

Summary

Exercises

12: Stored Procedures

Creating the Sproc: Basic Syntax

Changing Stored Procedures with ALTER

Dropping Sprocs

Parameterization

Confirming Success or Failure with Return Values

More On Dealing with Errors

What a Sproc Offers

Extended Stored Procedures (XPs)

A Brief Look at Recursion

Debugging

.NET Assemblies

Summary

13: User-Defined Functions

What a UDF Is

UDFs Returning a Scalar Value

UDFs That Return a Table

Debugging User-Defined Functions

.NET in a Database World

Summary

Exercise

14: Transactions and Locks

Transactions

How the SQL Server Log Works

Locks and Concurrency

Setting the Isolation Level

Dealing with Deadlocks (aka “a 1205”)

Summary

15: Triggers

What Is a Trigger?

Using Triggers for Data Integrity Rules

Other Common Uses for Triggers

Other Trigger Issues

INSTEAD OF Triggers

Performance Considerations

Dropping Triggers

Debugging Triggers

Summary

16: A Brief XML Primer

XML Basics

What SQL Server Brings to the Party

A Brief Word on XSLT

Summary

17: Reporting for Duty, Sir! A Look At Reporting Services

Reporting Services 101

Building Simple Report Models

Report Server Projects

Summary

18: Getting Integrated with Integration Services

Understanding the Problem

Using the Import/Export Wizard to Generate Basic Packages

Examining Package Basics

Executing Packages

A Final Word on Packages

Summary

19: Playing Administrator

Scheduling Jobs

Backup and Recovery

Index Maintenance

Archiving Data

Policy Based Management

Summary

Exercises

A: System Functions

Legacy System Functions (a.k.a. Global Variables)

Aggregate Functions

Configuration Functions

Cryptographic Functions

Cursor Functions

Date and Time Functions

Hierarchy Functions

Mathematical Functions

Basic Metadata Functions

Rowset Functions

Security Functions

String Functions

System Functions

Text and Image Functions

B: Very Simple Connectivity Examples

Some General Concepts

Connectivity Examples

A Brief Word on LINQ

Index

Advertisement

End User License Agreement

List of Illustrations

Chapter 1: RDBMS Basics: What Makes Up a SQL Server Database?

Figure 1-1

Figure 1-2

Figure 1-3

Chapter 2: Tools of the Trade

Figure 2-1

Figure 2-2

Figure 2-3

Figure 2-4

Figure 2-5

Figure 2-6

Figure 2-7

Figure 2-8

Figure 2-9

Figure 2-10

Figure 2-11

Figure 2-12

Figure 2-13

Figure 2-14

Chapter 3: The Foundation Statements of T-SQL

Figure 3-1

Figure 3-2

Figure 3-3

Chapter 4: JOINs

Figure 4-1

Chapter 5: Creating and Altering Tables

Figure 5-1

Figure 5-2

Figure 5-3

Figure 5-4

Figure 5-5

Figure 5-6

Figure 5-7

Figure 5-8

Figure 5-9

Figure 5-10

Chapter 6: Constraints

Figure 6-1

Chapter 8: Being Normal: Normalization and Other Basic Design Issues

Figure 8-1

Figure 8-2

Figure 8-3

Figure 8-4

Figure 8-5

Figure 8-6

Figure 8-7

Figure 8-8

Figure 8-9

Figure 8-10

Figure 8-11

Figure 8-12

Figure 8-13

Figure 8-14

Figure 8-15

Figure 8-16

Figure 8-17

Figure 8-18

Figure 8-19

Figure 8-20

Figure 8-21

Figure 8-22

Figure 8-23

Figure 8-24

Figure 8-25

Figure 8-26

Figure 8-27

Figure 8-28

Figure 8-29

Figure 8-30

Figure 8-31

Figure 8-32

Figure 8-33

Chapter 9: SQL Server Storage and Index Structures

Figure 9-1

Figure 9-2

Figure 9-3

Figure 9-4

Figure 9-5

Figure 9-6

Figure 9-7

Figure 9-8

Chapter 10: Views

Figure 10-1

Figure 10-2

Figure 10-3

Figure 10-4

Figure 10-5

Figure 10-6

Chapter 12: Stored Procedures

Figure 12-1

Figure 12-2

Figure 12-3

Figure 12-4

Figure 12-5

Figure 12-6

Figure 12-7

Chapter 13: User-Defined Functions

Figure 13-1

Chapter 14: Transactions and Locks

Figure 14-1

Figure 14-2

Figure 14-3

Chapter 15: Triggers

Figure 15-1

Figure 15-2

Figure 15-3

Figure 15-4

Figure 15-5

Figure 15-6

Chapter 16: A Brief XML Primer

Figure 16-1

Figure 16-2

Chapter 17: Reporting for Duty, Sir! A Look At Reporting Services

Figure 17-1

Figure 17-2

Figure 17-3

Figure 17-4

Figure 17-5

Figure 17-6

Figure 17-7

Figure 17-8

Figure 17-9

Figure 17-10

Figure 17-11

Figure 17-12

Figure 17-13

Figure 17-14

Figure 17-15

Figure 17-16

Figure 17-17

Figure 17-18

Figure 17-19

Figure 17-20

Figure 17-21

Figure 17-22

Figure 17-23

Figure 17-24

Figure 17-25

Figure 17-26

Figure 17-27

Figure 17-28

Figure 17-29

Figure 17-30

Chapter 18: Getting Integrated with Integration Services

Figure 18-1

Figure 18-2

Figure 18-3

Figure 18-4

Figure 18-5

Figure 18-6

Figure 18-7

Figure 18-8

Figure 18-9

Figure 18-10

Figure 18-11

Figure 18-12

Figure 18-13

Figure 18-14

Figure 18-15

Figure 18-16

Figure 18-17

Figure 18-18

Figure 18-19

Chapter 19: Playing Administrator

Figure 19-1

Figure 19-2

Figure 19-3

Figure 19-4

Figure 19-5

Figure 19-6

Figure 19-7

Figure 19-8

Figure 19-9

Figure 19-10

Figure 19-11

Figure 19-12

Figure 19-13

Figure 19-14

Figure 19-15

Figure 19-16

Guide

Cover

Table of Contents

Start Reading

Pages

v

vi

vii

ix

xi

xiii

xxxi

xxxii

xxxiii

xxxiv

xxxv

xxxvi

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

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

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

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

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

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

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

399

400

401

402

403

404

405

406

407

408

409

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

Beginning Microsoft SQL Server® 2008 Programming

Robert Vieira

Beginning Microsoft SQL Server® 2008 Programming

Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com

Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN-13: 978-0-470-25701-2

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

Library of Congress Cataloging-in-Publication Data:

Vieira, Robert.
  Beginning SQL server 2008 programming / Robert Vieira.
     p. cm.
  Includes index.
  ISBN 978-0-470-25701-2 (paper/website) 1. SQL server. 2. Database management. 3. Relational databases. I. Title.
  QA76.9.D3V5254 2008
  005.75’85--dc22
                                           2008033212

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. SQL Server is a registered trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

For over 10 years now, and through what is now 5 books (6 when I finish the Pro title this time around), I’ve dedicated my books to my daughters (well, it was a single daughter when I wrote my first one). I don’t see any reason to change that now.

Things in life have changed a great deal for me over the years, and the oldest daughter didn’t have to sacrifice as much time with me this time around (college life means she doesn’t want to see that much of me anyway — I hate kids growing up!). The younger one regularly asks me when I’m going to be done, so I guess she’s noticed time lost, and, frankly, so have I.

And so it is, once again, that I dedicate this book to my two daughters, and promise that at least some portion of the proceeds of this book will help pay for a nice trip to Disney World so we can make up a little time!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!