SQL Server 2008 R2 SQL Statements and SQL Scripts

SQL Server 2008 R2 SQL Statements and SQL Scripts

Because we have already argued that you need to know how to write and use SQL statements instead of relying on GUI tools, we come back to simply using SQL as the basis of our work. But we do not want to use a command-line utility, and we are not going to use the GUI tool in GUI mode, so what’s left?

The answer is that the Microsoft SQL Server Management Studio provides us with an excellent SQL editing environment. This lets us take advantage of GUI capabilities while still working with text-based SQL statements. We do this by opening an SQL Query window, but using it more generally as an “SQL statement editor” window.

Opening an SQL Server 2008 R2 SQL Statement Editor Window

1. Click the VRG database object in the Object Browser to select it.

2. Click the New Query button. A new tabbed SQL Query window is displayed, and the

SQL Editor toolbar appears, as shown in Figure 10-9.

3. Click the Intellisense Enabled button to disable Intellisense. Intellisense is an object-search feature that, although useful for experienced developers, can be more confusing than helpful if you do not know how to use it. In this book, we will not use Intellisense, but if you want to learn more about it, you can read about it in the SQL Server 2008 R2 Books Online.

If the tabbed document window looks familiar, it is because it is the same window we used for SQL Server 2008 R2 SQL queries in Chapter 2. We are simply using it for a slightly different purpose. This SQL editing environment will be our tool of choice.

One advantage of using this SQL editor is the ability to save and reuse SQL scripts. For SQL Server, SQL scripts are plaintext files labeled with the *.sql file extension. We can save, open, and run (and rerun) SQL scripts. As shown in Figure 10-10, when the SQL Server Management Studio is installed, the installation process creates a set of folders in the user’s Documents folder. By default, scripts are stored in the Projects folder. However, in order to organize our work we can create subfolders for various database projects. This is illustrated by

Figure 10-9

the DBP-e12-Cape-Codd-Database folder that was created to store the scripts (including scripts containing SQL queries) that we used with the Cape Codd Outdoor Sports database in Chapter 2.

The SQL Server Management Studio SQL Editor

The New Query button The SQL Editor

toolbar

Available Databases

dropdown list—select the database here

The Execute button The Parse button The Intellisense

Enabled button The SQL statement

tabbed window

Part 4 Multiuser Database Processing

The Libraries folder The Documents Library folder

The My Documents folder

The Public Documents folder

The SQL Server

Management Studio

folder The Projects folder The DBP-e12-Cape-

Codd-Database

folder—this is not a default folder, but was created to store scripts for the Cape Codd database used in Chapter 2

Figure 10-10

An SQL script is composed of one or more SQL statements, which can include SQL script

The SQL Server Management

comments. SQL script comments are lines of text that do not run when the script is

Studio Folder Structure

executed, but are used to document the purpose and contents of the script. Each comment line begins with the characters /* and ends with the characters */ .

Creating and Saving an SQL Script

1. In the open tabbed SQL Query window, type the SQL comments shown in Figure 10-11.

2. Click the Save button. The Save File As dialog box is displayed, as shown in Figure 10-12. The Open File

button The Save button The SQL text—the

yellow line on the right margin shows that this text has not been saved

The Close button

Figure 10-11

Entering SQL Statements in the SQL Editor

Chapter 10 Managing Databases with SQL Server 2008 R2

The Save File As dialog box

The New Folder button

The New Folder dialog box—type the new folder name here

The OK button The File Name

text box—type the new file name here

The Save button

Figure 10-12

3. Click the New Folder button in the Save File As dialog box. The New Folder dialog

box is displayed, as shown in Figure 10-12.

Saving the SQL Script in a New Folder

4. Type the folder name DBP-e12-View-Ridge-Gallery-Database into the Name text

box of the New Folder dialog box.

5. Click the OK Button on the New Folder dialog box.

6. Type the file name DBP-e12-VRG-Create-Tables in the File Name text box of the

Save File As dialog box.

7. Click the OK Button on the Save File As dialog box. The script is saved, the colored line on the right-hand margin of the text is changed to green, and the tab is renamed with the new file name.

8. Click the document Close button shown in Figure 10-11 to close the script

window.

Creating and Populating the View Ridge Database Tables

As we have seen, tables and other SQL Server structures can be created and modified in two ways. The first is to write SQL code using either the CREATE or ALTER SQL statements we discussed in Chapter 7. The second is to use the SQL Server 2008 R2 GUI display tools discussed earlier in this chapter. Although either method will work, CREATE statements are preferred for the reasons described in Chapter 7. Some professionals choose to create structures via SQL but then modify them with the GUI tools.

As discussed in Chapter 7, each DBMS product has its own variant or extension of SQL, with procedural language extensions based on SQL/Persistent Stored Modules (SQL/PSM) standard, which are additions that allow SQL to function similarly to a procedural programming language (e.g., IF . . . THEN . . . ELSE structures). Microsoft’s SQL Server version is called Transact-SQL (T-SQL). We will point out specific Transact-SQL syntax as we encounter it in our discussion. For more on Transact-SQL, see the SQL Server 2008 R2 Books Online article “Transact-SQL Reference” at http://msdn.microsoft.com/en-us/library/bb510741.aspx.