When you run SQL Query Analyzer, you will get a connection form, as you can see in Figure B.3, where you can select which server or server instance to connect to, and which authentication mode to use. If you selected SQL Server Authentication mode, you must provide a valid login and password to connect.
Caution
If you try to connect to SQL Server using the SQL Server authentication mode and you fail to supply a valid login name, SQL Query Analyzer will not try to connect using your Windows NT 4.0 or Windows 2000 credentials, as it did in previous versions, so the connection will be rejected. It is recommended to use integrated security if possible.
Initially, the Server drop-down list will not contain any entries. The server name will be added to the list only after successful connection to a server.
Tip
SQL Query Analyzer will try to connect to the local default instance if you leave the server name blank or write a dot '.'or '(local)'as the server name.
Caution
If you installed SQL Server 2000 with SQL Server 7.0 or 6.5 on the same computer, the default instance will be either SQL Server 7.0 or SQL Server 6.5, which means that many of the examples in this book will not work because of the different functionality between SQL Server 2000 and earlier versions. So, make sure you connect to the right instance of SQL Server 2000 before trying any of the examples.
When connected, SQL Query Analyzer will show a workplace as shown in Figure B.4.
As in other Windows applications, you can see some common elements such as the menu bar, the toolbar, and the status bar.
SQL Query Analyzer is a Multi Document Interface (MDI) application where every connection to SQL Server uses its own child window, inside the parent window. You can select the connection window by using the application window menu as usual, and you can arrange the windows in the common ways: Cascade, Tile Horizontally, and Tile Vertically.
Tip
If you have established more than one connection to SQL Server from SQL Query Analyzer, it is usually better to maximize the connection window to have extra visible space to work with the Editor and Results panes. As soon as you maximize one of the connection windows, all the other connection windows will be maximized as well.
The Editor pane of a connection window is a text editor you can use to edit your queries and SQL statements. These statements can come from different sources as follows:
Scripts written directly in the Editor pane
Saved SQL script files you can retrieve in the Editor pane for further editing
Scripts produced from the Object Browser and Object Search, as you will see later
Predesigned templates you can reuse to speed up and facilitate the process of writing code
This is a code-oriented text editor with many features to help you write SQL language code. You can find information about these features in Books Online, but it is interesting to pay attention to the following points:
The text is color coded to differentiate between keywords, system objects and system stored procedures, operators, comments, and so on.
You can use shortcuts to speed the editing process. You can find the full shortcut list in the topic "SQL Query Analyzer Keyboard Shortcuts" in Books Online, but the more frequently used are in Table B.1.
It is possible to select the font, tab length, and other options, as you can see in Figures B.13 and B.15 later in this appendix.
You can select which part of the code to execute by selecting the block either with the mouse or with the keyboard combining the Shift key with the navigation keys.
Tip
As you can select which block of your code to execute, it is not advisable to clear the window to write new code, unless, of course, you wanted to start completely from scratch. Clearing the Editor Pane window prevents you from being able to copy previous statements to save coding time.
Writing queries requires precise knowledge of database objects definition and complete syntax of statements, functions, and stored procedures. Developers can use the SQL Query Analyzer Object Browser to get information about database objects. Moreover, Object Browser is a dynamic tool with embedded scripting capabilities that can greatly improve your coding productivity.
As you can see in Figure B.5, Object Browser has two main sections: the server structure, with information about databases and database object of the server that the user is connected to, and the common objects, with information about system functions and data types.
Caution
It is a common mistake to consider the system stored procedures as system functions, because system stored procedures, as described in Chapter 8, are no more than standard stored procedures created during the installation process in the Master database. That's why Object Browser shows them in their original place, which is under the Server section, Master database, Stored Procedures.
As you can see in Figure B.6, you can right-click any object to see a context menu from where you can create a script of that object either to the Clipboard, a new window, or a file. Depending on the object type, it is possible to script different statements. In the example shown in Figure B.6, a user table, the available statements are CREATE, DROP, SELECT, INSERT, UPDATE, and DELETE. For stored procedures, you can choose to script the CREATE, ALTER, DROP, and EXECUTE statements.
Tip
During the coding process, scripting to the Clipboard is usually more flexible than scripting to a new window because after it is in the Clipboard, you can paste the script anywhere in your code, even several times.
In Figure B.7, you can see how the Object Browser shows object dependencies, as well as definitions of columns and parameters. The Object Browser enables the user to drag and drop any object's name to the Editor Pane, reducing the likelihood of misspelling an object name.
Caution
Always check dependencies before altering or dropping any object. Failing to do so could produce errors when other objects must reference the modified or missing object.
An interesting feature in SQL Server 2000 is the possibility of defining extended properties for databases and database objects. From the Object Browser it is possible to edit them using the context menu, as you can see in Figure B.8.
Working with several databases, and hundreds or thousands of objects, makes it difficult to find specific objects in Object Browser. Using the Object Search you can efficiently search for any object on any database.
Figure B.9 shows the Object Search window. You can use wildcards in the Object name field, as seen in Figure B.9. Searching for ___help* means any three characters followed by the word help followed by any string. Other combinations, such as *sys* sys* or authors, are valid as well.
You can search for objects with specific extended properties values.
Tip
Object Search is especially useful when you want to know which table contains the information you need to select in your query. In this case, provide an approximate column name, using wildcards, and specify Column as object type.
The lower section of every connection's window, below the Editor pane, is the Results pane, in which SQL Query Analyzer shows the results of the queries and any output message from SQL Server.
SQL Query Analyzer can show results in Text or Grid, in a spreadsheetlike format. It is possible to send results directly to a file, in which case the Results pane shows only SQL Server messages and confirmation of the file write operation as shown in Listing B.1.
To send results to a file, select the menu Query—Results to File. With this setting, whenever you execute a query, SQL Query Analyzer will prompt you to provide a filename, and the results will be sent to the selected file. To test how SQL Query Analyzer sends results to a file, make sure the menu Query—Results to File is checked and execute the example of Listing B.1.
USE Northwind GO SELECT * FROM Products (77 row(s) affected) The following file has been saved successfully: C:WINNTProfilesAdministratorMy Documentsfn.rpt 14242 bytes |
In Figure B.10, you can see the same results in text, and in Figure B.11 in grid.
Note
Results in grid are read-only. If you want to edit table values directly in a grid from SQL Query Analyzer, go to Object Browser, right-click in the object, and select Open. This way, SQL Query Analyzer will open a new window with a fully editable grid.
Caution
Executing more than a single query in the same batch when Grid mode is selected produces several grids in the Result pane. This can be a bit confusing because of the number of different slide bars in the Result pane. The main slider belongs to the Result pane, and every individual grid has its own slider, too.
3.22.27.45