To get information about an object of a database, you can execute the sp_help stored procedure. Its syntax is: sp_help [ [ @objname = ] 'name' ] This stored procedure takes one optional argument. If you call it without passing an argument, it produces information about the database that is currently selected. This means that you should first specify a database before executing this procedure. Here is an example: USE Exercise; GO sp_help; GO To get information about a specific object, pass its name as argument. Here is an example: USE Exercise1;
GO
sp_help N'Employees';
GO
The sp_spaceused stored procedure allows you to know how much memory a database or one of its objects is occupying. The syntax of this procedure is: sp_spaceused [[ @objname= ] 'objname' ] [,[ @updateusage= ] 'updateusage' ] This procedure takes two optional arguments. If you execute it without an argument, you would get the different amounts of memory space the current database is using. To specify the database whose size you want to check, you should first select it. Here is an example:
To know the space that a particular object is using, pass it as argument. Here is an example:
To get details about the columns of a table, you can execute the sp_columns stored procedure. Its syntax is: sp_columns [ @table_name = ] object [ , [ @table_owner = ] owner ] [ , [ @table_qualifier = ] qualifier ] [ , [ @column_name = ] column ] [ , [ @ODBCVer = ] ODBCVer ] This procedure can take many arguments but one is required. The required argument is the name of the table whose columns you want to investigate. Here is an example: USE Exercise1; GO sp_columns N'Employees'; GO
The sp_refreshview stored procedure allows you to update the metadata of a view. The syntax of this procedure is: sp_refreshview [ @viewname= ] 'viewname' Here is an example that executes this procedure: USE Exercise;
GO
sp_refreshview N'People';
GO
To let you get information about a trigger, Transact-SQL provides a stored procedure named sp_helptrigger. Its syntax is: sp_helptrigger [ @tabname= ] 'table' [ , [ @triggertype = ] 'type' ]
Imagine you had created a custom data type for your database: USE Exercise; GO CREATE TYPE NaturalNumber FROM int; GO If you don't need such a data type any more, to assist you with removing it, Transact-SQL provides the sp_droptype. Its syntax is: sp_droptype [ @typename= ] 'type' This procedure takes one argument as the name of the custom data type you want to delete. Here is an example of executing it: sp_droptype NaturalNumber; GO
Imagine you had created a database and added some constraints to it. Here are examples: USE master; GO CREATE DATABASE Exercise1; GO USE Exercise1; GO CREATE TABLE Genders ( GenderID int identity(1, 1) not null, Gender nvarchar(20) not null, CONSTRAINT PK_Genders PRIMARY KEY(GenderID) ); GO CREATE TABLE Employees ( PersonID int identity(1, 1) not null, FirstName nvarchar(22) null, LastName nvarchar(22) not null, GenderID int CONSTRAINT FK_Genders FOREIGN KEY REFERENCES Genders(GenderID), HourlySalary money, CONSTRAINT PK_Persons PRIMARY KEY(PersonID), CONSTRAINT CK_HourlySalary CHECK (HourlySalary > 12.20), CONSTRAINT CK_Gender CHECK (GenderID BETWEEN 0 AND 4) ); GO To let you get information about the constraints in a database, Transact-SQL provides the sp_helpconstraint stored procedure. Its syntax is: sp_helpconstraint [ @objname= ] 'table' [ , [ @nomsg= ] 'no_message' ] This procedure takes an argument as the name of the object whose constraints you want to find out. The argument can be the name of a table. Here is an example: As you can see, this procedure produces all constraint reference, both the primary key and foreign key(s). If a table contains many constraints, the database creates a summary. Here is an example:
A stored procedure usually shows its result only when it executes. Sometimes, you wish to automatically execute it at the time of your choosing. This is possible using the sp_procoption built-in stored procedure. Its syntax is: sp_procoption [ @ProcName = ] 'procedure' , [ @OptionName = ] 'option' , [ @OptionValue = ] 'value'
The sp_settriggerorder built-in stored procedure allows you to specify the order by which your AFTER triggers should/must execute. Its syntax is: sp_settriggerorder [ @triggername= ] '[ triggerschema. ] triggername' , [ @order= ] 'value' , [ @stmttype= ] 'statement_type' [ , [ @namespace = ] { 'DATABASE' | 'SERVER' | NULL } ]
To give you the ability to send an email from a database, Transact-SQL provides the sp_send_dbmail stored procedure. This store procedure is created in the msdb database. This means that you must reference it when executing this procedure. It's syntax is: sp_send_dbmail [ [ @profile_name = ] 'profile_name' ] [ , [ @recipients = ] 'recipients [ ; ...n ]' ] [ , [ @copy_recipients = ] 'copy_recipient [ ; ...n ]' ] [ , [ @blind_copy_recipients = ] 'blind_copy_recipient [ ; ...n ]' ] [ , [ @subject = ] 'subject' ] [ , [ @body = ] 'body' ] [ , [ @body_format = ] 'body_format' ] [ , [ @importance = ] 'importance' ] [ , [ @sensitivity = ] 'sensitivity' ] [ , [ @file_attachments = ] 'attachment [ ; ...n ]' ] [ , [ @query = ] 'query' ] [ , [ @execute_query_database = ] 'execute_query_database' ] [ , [ @attach_query_result_as_file = ] attach_query_result_as_file ] [ , [ @query_attachment_filename = ] query_attachment_filename ] [ , [ @query_result_header = ] query_result_header ] [ , [ @query_result_width = ] query_result_width ] [ , [ @query_result_separator = ] 'query_result_separator' ] [ , [ @exclude_query_output = ] exclude_query_output ] [ , [ @append_query_error = ] append_query_error ] [ , [ @query_no_truncate = ] query_no_truncate ] [ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ] As you may guess, most of the arguments are optional. Otherwise:
Here is an example of executing this stored procedure: USE Exercise; GO EXEC msdb.dbo.sp_send_dbmail @profile_name = N'Central Administrator', @recipients = N'jaywiler@hothothot.net', @body = N'The Persons table has received a new record.', @subject = N'New Record'; GO Before executing this procedure, you should check the security settings on your server. Otherwise you may receive an error. Here is an example: Msg 15281, Level 16, State 1, Procedure sp_send_dbmail, Line 0 SQL Server blocked access to procedure 'dbo.sp_send_dbmail' of component 'Database Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Database Mail XPs' by using sp_configure. For more information about enabling 'Database Mail XPs', see "Surface Area Configuration" in SQL Server Books Online. To solve this problem, open a Query window and type the following: sp_configure N'show advanced options', 1; GO RECONFIGURE; GO sp_configure N'Database Mail XPs', 1; GO RECONFIGURE; GO |
|
||||||||||||||||||||||||
|