Sie sind auf Seite 1von 16

SQL SERVER 6.

5
Chapter-11/1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
OBJECTIVE
Extensibility of SQL Server 6.5
SCOPE
SQL Executive Managers
Extended Stored Procedures
Messaging Application Programming Interface (MAPI)
Using OLE with SQL Server
SQL Distributed Management Objects (SQL-DMO)
Open DataBase Connectivity (ODBC)
SQL SERVER 6.5
Chapter-11/2
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
SQL EXECUTIVE MANAGERS
SQL Server is capable of extending beyond its environment i.e.
a) Certain tasks and statements can be scheduled and automated. (Using SQL
Executive Managers)
b) Their are components of SQL Server which enable to develop sophisticated
client/server applications that tap the power of SQL Server. ( Using Extended
stored procedures, OLE, ODBC, DB-LIB)
SQL Executive Managers
SQL Executive is a component of SQL Enterprise Manager.

It is a Microsoft Windows NT service that provides :

(a) Scheduling capabilities
(b) Event alerting capabilities
(c) Replication capabilities

These capabilities allow the scheduling and administration of SQL Server events.



DIAGRAM











SQL Executive
Alert Manager Event
Manager
Task Manager Replication
Manager
SQL SERVER 6.5
Chapter-11/3
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.

Following table defines the services of SQL Executive Managers:

Services Definition

Alerts Notifications that are automatically invoked when events occur.
Designated administrators are notified by means of e-mail or pagers.
Events Anything that posts events to the Windows NT Event Log with a
source of SQL Server ( either directly from SQL Server or using
xp_logevent extended stored procedure or RAISERROR.
Tasks Corrective measures that are automatically fired when events occur.
Can be implemented as Transact-SQL scripts or Windows NT
command-shell scripts.
Can be scheduled for regular execution as well as being tied to an
event.
Replication Ability to transfer data across multiple servers.


Event, Alert and Task Managers

Using SQL Enterprise Manager, server events, alerts and tasks can be created.

Event Manager

Event Manager processes and responds to SQL Server events which are posted to
Windows NT Event Log.
Windows NT log is the central location for application event processing.
It notifies the SQL Executive Service about an event.

Alert Manager

Alerts provide an early warning mechanism for administrators.
Alerts can be defined on any SQL Server event.
They can be associated with the operator or a group of operators who will receive
notifications by e-mail or pager. ( Operators along with their e-mail or pager are
created using Alert Manager.) To support this, Microsoft Mail (provided with
Window NT) must be configured.
Tasks can also be automatically invoked in response to the alert.

Task Manager

Task Manager allows an administrator to define, build and automate administrative
tasks that can be scheduled for:

(a) Execution at a specified time and date
(b) Executed on demand
SQL SERVER 6.5
Chapter-11/4
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.


Many administrative functions can be automated such as:


(a) Database/Log backups
(b) Index rebuilds
(c) Data loads etc.

After the creation of a task, it can be scheduled by the SQL Executive.

Administrators control the :

(a) Occurrence ( Daily, Weekly, Monthly and so on)
(b) Frequency ( Once only, Once every day, Once every hour and so on)
(c) Duration of the task.


SQL Alert Engine

DIAGRAM























The following steps are involved in processing an alert.

SQL Server

msdb

system tables
msdb
Windows NT Event
Log

SQL Executive
1.
3.
Send Page or
e-mail to operator
Fire:
Transact-SQL
Window NT
Cmd Shell
4.
5.
2.
SQL SERVER 6.5
Chapter-11/5
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.


SQL Executive starts up and registers itself as a callback application for the Windows
NT Event Log Service.
When an event occurs ,the SQL Server records the error information in the Windows
NT Event Log using xp_logevent or RAISERROR.
When an error is recorded in the Window NT Log, SQL Executive is notified that an
error from the SQL Server has occurred.
SQL Executive retrieves the error information from the Window NT Event Log.
SQL Executive then reads the sysalerts table of the msdb database to determine if any
alerts and tasks are defined for this event.
It matches the Windows NT Event Log to the sysalerts table. The match is based on
the following:

Windows NT Event Log msdb..sysalerts

Event Source sysalerts..Even_Source
Event Description sysalerts..Message_ID
Event Description sysalerts..Severity


The Event Source and the Event Description information in the Windows NT Event
Log must match sysalerts..Event_source and either Message_ID or Severity
information.
If a matching alert is found, the defined action takes place ( sending e-mail, page or
firing off associated task)
If no alerts are defined for this event, the event can be forwarded to the Alert Server
for processing.
SQL SERVER 6.5
Chapter-11/6
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.

EXTENDED STORED PROCEDURES

Extended Stored Procedures provide a way to dynamically load and execute a
function within a Dynamic Link Library (DLL).
Actions outside the SQL Server can be easily triggered.
External information can be easily returned to SQL Server.
Extended Stored Procedures are external to SQL Server, so they must be specified in
the case ( lowercase) in which they were designed.
Return Status Codes and Output parameters are supported which are identical to their
counterparts in regular stored procedures .
Extended stored procedures are categorised by their use as follows:

Integrated Security - Used for security purposes. Some examples are xp_logininfo and
xp_grantlogin.

Extended Stored Procedure Description

xp_logininfo Reports the account,its type, privilege level and the
permission path by which the account has to access
SQL Server.
xp_grantlogin Grants SQL Server access to a Windows NT based
group or user.

SQL Mail - Used for sending and receiving mails. For example, xp_sendmail and
xp_readmail.

Extended Stored Procedure Description

xp_sendmail Sends a message or a query results set or an
attachment to the specified recipient.
xp_readmail Reads a mail message from the SQL Server inbox.
Used by the sp_processmail system stored
procedure to process all mail in the SQL Server
inbox.

General - For example xp_cmdshell and xp_logevent.

Extended Stored Procedures Description

xp_cmdshell Executes a given command string as an Operating
System command shell and returns any output as
row of text.
xp_logevent Logs a user defined message in he SQL Server Log
file or in the Windows NT Event Viewer.
SQL SERVER 6.5
Chapter-11/7
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.


Internal - A number of extended procedures exist for internal use by other stored
procedures. For example, xp_readmail.

User-defined - Additional extended procedures can be created by programmers using
Microsoft Open Data Services.

SQL Server can call an extended stored procedure as the result of a trigger or a user
can issue an EXECUTE command, specifying the name of the extended stored
procedure and supplying any parameters required.


EXAMPLES

Example 1

xp_logevent, error_number message, [severity]

DECLARE @tabname varchar(20)
DECLARE @username varchar(20)
DECLARE @message varchar(230)
SELECT @tabname = member
SELECT @username = USER_NAME()
SELECT @message = The table + @tabname + is not owned by the user +
@username
EXEC xp_logevent 60000, @message, Informational

OUTPUT:
The message ( with variables passed to the message ) is logged in the Windows NT Event Viewer.
60000 is a user defined error number greater than 50000.
Severity is specified as one of the three : Informational, Warning, or Error.


Example 2

xp_cmdshell command [, no_output]


EXEC master..xp_cmdshell dir *.exe

OUTPUT:

Returns a list of Executable files.


SQL SERVER 6.5
Chapter-11/8
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.


Managing Extended Stored Procedures

Registering new extended stored procedures

Once an extended stored procedure is created using ODS,it must be added to SQL
Server by using the system stored procedure sp_addextendedproc.
It adds entries to the sysobjects table to register the name of the new extended
procedure in the master database.
It adds the name of the DLL as an entry into the syscomments table.
Only the SA can add extended stored procedures.
They must be in the master database.

Syntax::

sp_addextendedproc function_name, dll_name

EXAMPLE
sp_addextendedproc xp_diskfree, xp

OUTPUT:

xp_diskfree is the name of the function to call within the DLL.
xp is the name of the DLL.
Extended stored procedure called xp_diskfree gets registered.



Dropping an extended stored procedure

The system stored procedure sp_dropextendedproc is used to drop the extended
stored procedure.

Syntax:

sp_dropextendedproc function_name

EXAMPLE
sp_dropextendedproc xp_diskfree

OUTPUT:
The extended stored procedure xp_diskfree is dropped from the sysobjects table.



SQL SERVER 6.5
Chapter-11/9
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.

Displaying help on an extended stored procedure

The system stored procedure sp_helpextendeproc displays the currently defined
extended functions and the name of the DLL to which the function belongs.
If a function name is given, only information about that function is displayed.
Syntax:
sp_helpextendedproc [ function_name]
EXAMPLE
sp_helpextendedproc xp_diskfree
OUTPUT:
The name and DLL of the extended stored procedure is displayed.


name dll
----- ------
xp_diskfree xp.dll



Messaging Application Programming Interface (MAPI)

SQL Server includes extended stored procedures that allow SQL Server to send and
receive mail messages through a built-in messaging application programming interface
(MAPI) in Window NT. This capability is called SQLMail.

SQLMail can be used to send and receive messages when
A SQL Server alert occurs
A SQL Server Performance Monitor Threshold is exceeded.
A scheduled task succeeds or fails

SQLMail messages can consist of short text strings, output from a query or an attached
file.

Using SQLMail extended stored procedures, messages can be sent from a trigger or
stored procedure. For example, a trigger can be set to send a message when changes
occur in a database.

The server must be configured as a mail client, before it can send or receive messages
through MAPI.
SQL SERVER 6.5
Chapter-11/10
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.


EXAMPLE
xp_sendmail @recipient = recipient [; recipient2; [...; recipientn]]

[, @message = message ]
[, @query= query ]
[, @attachments= attachments ]
[, @copy_recipients = recipient [; recipient2;[...;recipientn]]]
[, @subject= subject ]
[, @type = type ]
[, @attach_results = {true | false}]
[, @no_output = {true | false}]
[, @no_header = {true | false}]
[, @width= width]
[, @separator = separator]
[, @echo_error = {true | false}]
[, @set_user = user]
[, @dbuse = dbname ]

xp_sendmail @recipient = user1;user2
@query = SELECT * FROM sysobjects,
@subject=SQL Server Report,
@message=The contents of sysobjects:,
@attach_results=TRUE

OUTPUT:
Results of the query SELECT * FROM sysobjects are sent as a text file attachment to user1.
It includes a subject line for the mail and a message that will appear before the attachment.


The system stored procedure sp_processmail incoming mail messages, which can be
only a single query, from the inbox for SQL Server. It uses the extended stored
procedures xp_findnextmsg, xp_readmail, xp_deletemail.

It uses xp_sendmail to return the results set back to the user.



CLASSROOM EXERCISE

1. For an alert, what can be the two types of responses?

2. Write the extended stored procedure to send a message to a user called
user1 that the master database is full.





SQL SERVER 6.5
Chapter-11/11
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.

USING OLE WITH SQL SERVER


OLE Automation

OLE is a programming technology that exposes objects, properties, and methods of
an object application (SQL Server in this case) to other controller applications (like
VB).

Controller applications can then program the object application.

It allows applications to share objects for either administration or data access.

Thus, OLE Automation Controllers are applications and programming tools that
access OLE Automation Objects.

OLE Automation controllers can create OLE Automation objects that reside in
another application, and can manipulate those objects using properties and methods
that the OLE Automation Object supports.

An OLE Automation Server is an application that exposes OLE Automation objects
for use by OLE Automation Controllers.

An OLE Automation server is not capable of application programming.

DIAGRAM










OLE Automation has 2 main functions:
(a) Expose objects - OLE Automation exposes objects.
(b) Manipulate objects - After exposing objects, something must be done with the
object. An OLE Automation Controller program uses OLE Automation objects
exposed by other applications. Microsoft Visual Basic is an example of OLE
Automation Controller.




VB
SQL SERVER
OLE Automation
Server
OLE Automation
Controller
DMO
SQL SERVER 6.5
Chapter-11/12
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
SQL Distributed Management Objects (SQL-DMO)
Distributed Management Objects (DMO) was developed using OLE architecture.

DMO makes it possible for the developers to directly access SQL Server database
objects.

Though system data can be accessed by referencing these objects, they are not
designed for full data management.

The key is to expose interfaces for all SQL Server Management functions.

SQL-DMO are 32-bit OLE automation objects for the Windows 95 and Windows NT
Operating Systems.

SQL Enterprise Manager, a tool that simplifies managing multiple server
environment utilizes the OLE service, which provide easier administration of tasks
since relationship between the objects are known.
DIAGRAM
OLE AND SQL SERVER
The complete SQL-DMO type library is contained in the SQLOLE65.TLB file,
located by default in the C:\MSSQL\BINN directory.

It contains information abut all SQL-DMO objects , properties and methods, and is
usually used by the OLE Automation Controllers such as Visual Basic.
SQL Enterprise Manager VBA, VC++, etc.
SQL OLE
SQL Server Object application
Controller
application
SQL SERVER 6.5
Chapter-11/13
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
In providing a consistent OLE interface to all SQL Server administration functions
and services, DMO includes commands and tasks that are implemented in many
ways, some are:
SQL Server system stored procedures (sp_addtype,)
Open Data services Extended Stored Procedures (xp_sendmail,)
Transact-SQL statements
SQL Performance Monitor scheduled events
SQL Server system catalog contents
SQL Server DMO Model
The following define the components of the SQL Server Object Model:
Object and Collection Multiple objects of the same type. (For examples all databases in
a server.
Object only The primary component of SQL Server. ( For example, Server, database,
table, views and so on)
Collection only A group of related objects associated with a higher ranking object in the
hierarchical structure.
For example, database is an object that contains a collection of tables associated with the
database.
A collection implies ownership between an object and its collection.
For example, in Server.databases, the server owns the collection of databases.
Database Object properties
Object properties are single-named pieces of information maintained for an object.
For example, Server.name, Server.Version and so on.
These are generally simple datatypes.
For example, a column in table is a property of the table, but is also an object in itself.
Database Object methods
Object methods are actions that are associated with objects.
For example, Server.Start, Server.Stop.
SQL SERVER 6.5
Chapter-11/14
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
OPEN DATABASE CONNECTIVITY
SQL Server driver enables applications to access data in SQL Server through ODBC
interface.
The ODBC driver is installed automatically when SQL Server client software is installed
on Win NT, Win 95 or Win 3.1 computers.
ODBC has following characteristics:
It is a call level interface for accessing many databases from Windows applications.
It allows to write one application that can access many different data sources by
using different ODBC drivers.
It is a layer of abstraction between the application and the underlying database
system.
It is responsible for taking your request for information and changing it into the
language the database engine will understand and use for retrieving the information
in the database.
Thus, a common set of calls and methodologies can be developed without having to
worry about the subtleties of the underlying database.
DIAGRAM
DBase SQL Server
Dbase driver
SQL Server
driver
ODBC DLL
Application
Network
Client
SQL SERVER 6.5
Chapter-11/15
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
CLASSROOM EXERCISE
1. Define the term OLE Automation

2. What is the function and purpose of ODS?
3. What is Open DataBase connectivity?
SQL SERVER 6.5
Chapter-11/16
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
SUMMARY
SQL Executive is a Microsoft Windows NT service that provides :

(a) Scheduling capabilities
(b) Event alerting capabilities
(c) Replication capabilities
Extended Stored Procedures provide a way to dynamically load and execute a function
within a Dynamic Link Library (DLL).
SQL Mail - SQL Server includes extended stored procedures that allow SQL Server to
send and receive mail messages through a built-in messaging application programming
interface (MAPI) in Window NT.
OLE is a programming technology that exposes objects, properties, and methods of an
object application to other controller applications .
OLE Automation Controllers are applications and programming tools that access OLE
Automation Objects.
DMO makes it possible for the developers to directly access SQL Server database
objects.
ODBC allows to write an application that can access many different data sources by
using different ODBC drivers.

Das könnte Ihnen auch gefallen