Microsoft SQL Server 2000 Weekend Crash Course phần 8 doc

41 271 0
Microsoft SQL Server 2000 Weekend Crash Course phần 8 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Use INFORMATION_SCHEMA as a prefix for the name of the object you would like to find information about. The basic syntax for using INFORMATION_SCHEMA is as follows: SELECT * FROM INFORMATION_SCHEMA.TABLES When run from the Query Analyzer, this query will produce the output shown in Figure 23-1. Figure 23-1 Querying INFORMATION_SCHEMA tables’ information Here are some samples of the more frequently used INFORMATION_SCHEMA objects — the names are very descriptive, though you should consult documenta- tion (such as Books Online) to verify the view’s columns you wish to query. ¼ Table information: VIEW_TABLE_USAGE TABLE_CONSTRAINTS TABLE_PRIVILEGES TABLES VIEWS Session 23—Accessing SQL Server System Information 265 Part V—Sunday Morning Session 23 364840-9 ch23.F 8/28/01 12:55 PM Page 265 ¼ Column information: COLUMNS VIEW_COLUMN_USAGE KEY_COLUMN_USAGE ¼ Constraint information: CHECK_CONSTRAINTS CONSTRAINT_COLUMN_USAGE CONSTRAINT_TABLE_USAGE REFERENTIAL_CONSTRAINTS Using System Stored Procedures Using system stored procedures is a black art that can never be mastered in full. Yes, you can know them all by heart (all 930 of them plus 174 extended stored procedures) — only to find that the number has increased, and that names and parameter types have been changed in the newest release. Until recently, no alternative to using system stored procedures was available. Even since the advent of INFORMATION_SCHEMA they remain the most comprehen- sive set of tools for your SQL Server system. You can use system stored procedures to administer SQL Server, to query it for information, and to create and drop data- base objects. In fact, SQL Server uses them itself, behind the scenes, in performing various administrative tasks. Unlike INFORMATION_SCHEMA, system stored proce- dures not only display information, but also modify it. Proceed with caution. You can access most of the functionality offered by system stored procedures through the Enterprise Manager interface, but it’s worth your while to familiarize yourself with the most common of them. To execute them directly you can use any of the access interfaces available: Query Analyzer, ISQL, OSQL, and so on. The system stored procedures can be grouped into categories: Microsoft Books Online lists 10 of them. Here’s a list of the ones you are most likely to use: ¼ General stored procedures ¼ Catalog stored procedures ¼ Security stored procedures ¼ SQL Server Agent stored procedures ¼ Extended stored procedures I discuss these stored procedures in the following sections. Sunday Morning266 364840-9 ch23.F 8/28/01 12:55 PM Page 266 General stored procedures These procedures help you with basic system administration. General stored proce- dures, as the name implies, support general SQL Server activity. Table 23-2 lists some selected stored procedures. Table 23-2 Selected General Stored Procedures Stored Procedure Description sp_help Provides information about any object listed in the sysobjects table. sp_helptext Provides access to the text of a rule, default, unencrypted stored procedure, or user-defined function, trigger, or view. sp_helpindex Provides information about all indexes defined for a specific table or view. sp_helpuser Provides information about SQL server users, roles, and so on. sp_who Provides information about current users and processes. sp_lock Provides information about locks. Catalog stored procedures Catalog stored procedures return information about tables, columns, data types, privileges, and such. These procedures have been largely supplanted by INFORMA- TION_SCHEMA views. See Table 23-3 for a list of selected catalog stored procedures. Table 23-3 Selected Catalog Stored Procedures Stored Procedure Description sp_databases Provides a list of all accessible databases in a given instance of SQL Server. sp_tables Provides a list of all tables in a database. Continued Session 23—Accessing SQL Server System Information 267 Part V—Sunday Morning Session 23 364840-9 ch23.F 8/28/01 12:55 PM Page 267 Table 23-3 Continued Stored Procedure Description sp_columns Provides column information for a table. sp_server_info Provides information about the SQL Server. sp_stored_procedures Provides a list of all stored procedures in a database. Unless the documentation states otherwise, all system stored procedures are functions whose return value is 0 on success and an error-number value on failure. Security stored procedures These procedures are among those that modify system tables. Proceed with cau- tion, and use the Enterprise Manager interface instead of these stored procedures whenever possible. Table 23-4 lists several security stored procedures. Table 23-4 Selected Security Stored Procedures Stored Procedure Description sp_addrole Creates a new role for a database. sp_adduser Adds a new security account. sp_helpuser Provides information about a particular user or database role. sp_helplogins Provides information about logins defined on the system. sp_password Adds or resets a user’s password in SQL Server. In examining a sysobjects system table (especially that of the Master database) you mzight find undocumented procedures. Refrain from using any of these, as it might lead to unpre- dictable results and non-portable systems. Note Note Sunday Morning268 364840-9 ch23.F 8/28/01 12:55 PM Page 268 SQL Server Agent stored procedures These stored procedures are called behind the scenes to perform the tasks associ- ated with SQL Server Agent — scheduling jobs, creating alerts, and so on. Unless you wish to create these objects programmatically, the SQL Server Agent interface should be your tool of choice. Table 23-5 lists some of the SQL Server Agent stored procedures. Table 23-5 Selected SQL Server Agent Stored Procedures Stored Procedure Description sp_add_alert Adds an alert to SQL Server Agent. sp_add_job Adds a job to SQL Server Agent. sp_add_jobschedule Creates a schedule for a given job. sp_addtask Creates a scheduled task. sp_help_alert Provides information about all alerts defined for the system. Most “add” stored procedures have a corresponding “drop” procedure. Extended stored procedures Extended stored procedures are your window onto the operating system. They enable you to do things that would be difficult or impossible in T-SQL; at the same time they can open up potential security loopholes that provide access to your network through SQL Server. Extended stored procedures are usually, but not always, prefixed with xp_. You create an extended stored procedure as an external Dynamic Linked Library (DLL), usually in C++, and link it into SQL Server. When an extended stored proce- dure is called, SQL Server actually loads a program (DLL), passes parameters to it, and retrieves the return values. Tip Session 23—Accessing SQL Server System Information 269 Part V—Sunday Morning Session 23 364840-9 ch23.F 8/28/01 12:55 PM Page 269 To execute an extended stored procedure you must have sufficient privileges. See Table 23-6 for a list of selected extended stored procedures. Table 23-6 Selected Extended Stored Procedures Stored Procedure Description xp_cmdshell Executes a command as you would from the command line, and returns text rows. xp_logininfo Provides details of the account and privileges for the login. xp_sprintf Formats values into an output string. xp_sscanf Reads formatted information from a string into the arguments’ location. xp_logevent Logs event information into the Windows Event log as well as into the SQL Server log. API system stored procedures are reserved exclusively for OLE DB providers, ODBC drivers, and DB-LIB functions. You should not call these procedures directly. REVIEW ¼ SQL Server gives you several ways to access its system information. ¼ The recommended way to obtain information about SQL Server objects and their properties is to use INFORMATION_SCHEMA views. These hidden views are created for each database in SQL Server 2000. ¼ You can use system stored procedures to retrieve most of the information available through INFORMATION_SCHEMA; in addition, system stored proce- dures enable you to perform many administrative tasks. ¼ You should use the Enterprise Manager interface in place of system-stored procedures whenever possible, to ensure that the system stored procedures are used properly. Note Sunday Morning270 364840-9 ch23.F 8/28/01 12:55 PM Page 270 QUIZ YOURSELF 1. What are the two primary methods of retrieving system information from SQL Server 2000? 2. What is an INFORMATION_SCHEMA? 3. Why is it not usually a good idea to query SQL server system tables directly? 4. What is the purpose of system stored procedures? 5. How does an extended stored procedure differ from a standard stored procedure? Session 23—Accessing SQL Server System Information 271 Part V—Sunday Morning Session 23 364840-9 ch23.F 8/28/01 12:55 PM Page 271 364840-9 ch23.F 8/28/01 12:55 PM Page 272 Session Checklist ✔ Configuring and using SQL Server Agent ✔ Scheduling jobs ✔ Creating alerts ✔ Managing operators ✔ Administering multiple servers A utomating some of the SQL Server administrative tasks will make your life easier. With SQL Server Agent, you can create and schedule various tasks, from database maintenance to data manipulations; you also can use alerts to keep in touch with your SQL Server via e-mail, Net Send pop-up messages, or even a pager. Configuring and Using SQL Server Agent Behind every automated task executed in SQL Server 2000 stands SQL Server Agent (which I mentioned briefly in Session 4). Using it you can define, execute, and SESSION Automating Administration Tasks with SQL Server Agent 24 374840-9 ch24.F 8/28/01 12:55 PM Page 273 schedule for execution various database tasks. The system database MSDB drives it: When you modify its properties, schedule a new job, or add an alert, all the data are written into MSDB to be read by SQL Server Agent; there is no direct communi- cation with the Agent. You should never attempt to modify MSDB tables directly: doing so could result in a corrupted system. Always use the interfaces provided by the SQL Server wizards. In order to run, SQL Server Agent requires that the SQL Server Agent Service be running. Usually you’ll want the service to start when Windows starts: You can set it to do this in the SQL Server Service Manager. Running the service will ensure that any scheduled tasks are executed on time. You need to configure SQL Server Agent to run properly on your system. Configuration screen You can access the configuration screen, shown in Figure 24-1, from the Enterprise Manager console ➪ Management node ➪ SQL Server Agent node ➪ right-click menu Properties option. You will specify an account to use on startup; this account must have all the necessary permissions to execute tasks in SQL Server. If you wish to receive notifications from your SQL Server via e-mail or pager you will need to set a mail profile (I describe this process in detail in Session 25) corresponding to one on your e-mail server (which must be a MAPI-compliant application such as Microsoft Exchange). The Agent writes an error log for all its activities into a specific file, and if your system is on the network you can send the log to a net recipient by specifying the other computer’s name. Properties As you go through the tabs on the SQL Server Agent properties dialog you can instruct the Agent (on the Advanced tab) to restart SQL Server if it shuts down unexpectedly and even to restart itself. In a multi-server environment you may want to forward alerts and messages to some other server(s). Note Sunday Morning274 374840-9 ch24.F 8/28/01 12:55 PM Page 274 [...]... account for SQL Server Agent and SQL Mail Setting up Your Mail Profile SQL Server 2000 enables you to communicate with it via e-mail To help you do this, SQL Server provides two services: SQL Mail and SQL Server Agent Mail Collectively, these two services are referred to as SQL Server mail services You use 384 840-9 ch25.F 8/ 28/ 01 12:55 PM 284 Page 284 Sunday Morning the first service, SQL Mail, to... between SQL Mail and SQL Server Agent Mail? 2 What is MAPI? What is a MAPI profile? 3 Does SQL Server have mail -server capabilities, or does it need an external mail server to receive and send e-mail messages? 4 On what domain account should you start SQL Server if you want to communicate with a MAPI-compliant mail server? Part V—Sunday Morning Session 25 384 840-9 ch25.F 8/ 28/ 01 12:55 PM Page 292 39 484 0-9... which SQL Server is installed, as shown in Figure 25-1 3 Click Show Profiles This will display all the existing profiles and enable you to add a new one, as shown in Figure 25-2 384 840-9 ch25.F 8/ 28/ 01 12:55 PM Page 285 Session 25—Configuring SQL Server Mail 285 Figure 25-1 Examining mail-profile properties Part V—Sunday Morning Session 25 Figure 25-2 Creating a new profile 384 840-9 ch25.F 8/ 28/ 01... Configuring SQL Mail and SQL Server Agent Mail You configure SQL Mail from the Enterprise Manager console To do so, select Support Services node ➪ SQL Mail sub-node ➪ choose Properties from the rightclick menu From the drop-down box shown in Figure 25-3, select the profile you are going to use Figure 25-3 Configuring SQL Mail 384 840-9 ch25.F 8/ 28/ 01 12:55 PM Page 287 Session 25—Configuring SQL Server Mail 287 ... Server Agent to start up automatically once the SQL Server starts, in order to provide constant e-mail access Part V—Sunday Morning Session 25 Figure 25-4 Configuring SQL Server Agent Mail 384 840-9 ch25.F 8/ 28/ 01 12:55 PM Page 288 288 Sunday Morning Sending Mail through Extended Stored Procedures Once you have your mail configured properly you can use your SQL Server Query analyzer to send e-mails To do... you automate tasks on multiple servers? 384 840-9 ch25.F 8/ 28/ 01 12:55 PM Page 283 SESSION 25 Configuring SQL Server Mail Session Checklist ✔ Setting up your mail profile ✔ Configuring SQL Mail and SQL Server Agent Mail ✔ Sending mail through extended stored procedures ✔ Troubleshooting T he ability to communicate via e-mail is one of the useful features of SQL Server 2000 In this session you are going... them ¼ Though you configure SQL Mail and SQL Server Agent Mail separately, the procedures you use are essentially the same You can use one MAPI profile for both services ¼ You must configure your SQL Server Service and your SQL Server Agent Service to start on the same account as your Exchange service 384 840-9 ch25.F 8/ 28/ 01 12:55 PM Page 291 Session 25—Configuring SQL Server Mail 291 QUIZ YOURSELF... Administering Multiple Servers Part V—Sunday Morning Session 24 SQL Server 7.0 enables you to use SQL Server Agent in multi -server administration When administering multiple servers you need to set up one server as a master that manages jobs for the target servers and processes alerts from these target servers by means of event forwarding This feature pertains to networks with multiple SQL Server installations,... you must supply your domain password in order to use this service 384 840-9 ch25.F 8/ 28/ 01 12:55 PM Page 290 290 Sunday Morning Figure 25-6 Configuring SQL Server Service for a domain account REVIEW ¼ In order to use SQL Server Mail and SQL Server Agent Mail you need a MAPI-compliant mail client configured to connect to your e-mail server ¼ You need to set up mailboxes for these services and add to... 24-1) You configure your servers through MSX and TSX wizards, available from the Multi- Server Administration option on the right-click menu on the SQL Server Agent node; MSX refers to the Master server and TSX to the target server These are advanced features that you certainly need to learn more about — once you are through with this book 37 484 0-9 ch24.F 8/ 28/ 01 12:55 PM Page 282 282 Sunday Morning REVIEW . Morning2 68 36 484 0-9 ch23.F 8/ 28/ 01 12:55 PM Page 2 68 SQL Server Agent stored procedures These stored procedures are called behind the scenes to perform the tasks associ- ated with SQL Server Agent. automate tasks on multiple servers? Sunday Morning 282 37 484 0-9 ch24.F 8/ 28/ 01 12:55 PM Page 282 Session Checklist ✔ Setting up your mail profile ✔ Configuring SQL Mail and SQL Server Agent Mail ✔ Sending. features of SQL Server 2000. In this session you are going to learn how to set up and config- ure a mail account for SQL Server Agent and SQL Mail. Setting up Your Mail Profile SQL Server 2000 enables

Ngày đăng: 13/08/2014, 22:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan