To Perform repetitive T-SQL task has to be executed within an
application, then the best repository for it is a program called a stored
procedure, stored in SQL Server. Storing the code inside the SQL Server
object gives us many advantages, like:
Create Procedure ProcedureName
@paramter1 Datatype
@parameter2 datatype
begin
-------
------
-----
end
- Security due to encryption
- Performance gains due to compilation
- Being able to hold the code in a central repository:
- Altering the code in SQL Server without replicating in several different programs
- Being able to keep statistics on the code to keep it optimized
- Reduction in the amount of data passed over a network by keeping the code on the server
- Hiding the raw data by allowing only stored procedures to gain access to the data
You may have executed some ad-hoc queries for tasks
like inserting data, querying information in other systems, or creating new
database objects such as tables.
All these tasks can be placed within a stored
procedure, so that any developer can run the same code without having to
recreate the T-SQL commands. Also, generalizing the code for all values makes
it generic and reusable.
Stored procedures are more than just tools for
performing repetitive tasks. There are two main types of stored procedure –
system stored procedures and user-defined stored procedures.
We also have
extended stored procedures that can reside as either system or user-defined
types. Extended stored procedures give functionality that is not necessarily
contained within SQL Server, like allowing DOS commands to run and working with
e-mail.
It is also possible to create your own extended stored
procedures.
System
Stored Procedures
In SQL Server, many administrative and informational
activities can be performed by using system stored procedures. Every time we
add or modify a table, make a backup plan, or perform any other administrative
function from within Enterprise Manager, we actually call a stored procedure
specifically written to complete the desired action.
These stored procedures
are known as system stored procedures, which are functions that enhance the
basic functionality of SQL Server itself, either by extending the functionality
of an existing system stored procedure or by creating new functionality that
enhances the basics of what is already there.
System stored procedures are prefixed by sp_, so it is
not advisable to use sp_ for any of the stored procedures that we create,
unless they form a part of our SQL Server installation. Creating a stored
procedure prefixed with sp_ and placing it in the master database will make it
available to any database without the need to prefix the stored procedure with
the name of the database
User Stored Procedures :
A user stored procedure is any program that is stored
and compiled within SQL Server (but not in the master database) and prefixed
with sp_. User stored procedures can be categorized into three distinct
types:
- User stored procedures
- Triggers, and
- User defined functions
Create Procedure ProcedureName
@paramter1 Datatype
@parameter2 datatype
begin
-------
------
-----
end
No comments:
Post a Comment