0% found this document useful (0 votes)
118 views

Stored Procedure: Complied by John .K & Bharat

Stored procedures are a precompiled set of t-sql statements. Stored procedures can be used to perform a particular task. A STORED PROCEDURE can be encrypted to hide sensitive data.

Uploaded by

asasidhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Stored Procedure: Complied by John .K & Bharat

Stored procedures are a precompiled set of t-sql statements. Stored procedures can be used to perform a particular task. A STORED PROCEDURE can be encrypted to hide sensitive data.

Uploaded by

asasidhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Complied by John .

K & Bharat

STORED PROCEDURE

1. What is a Stored Procedure? -


It’s a set of T-SQL statements that are precompiled and form a
logical unit and perform a particular task. SQL Server compiles
each stored procedure once and then reutilizes the execution
plan. This results in tremendous performance boosts when stored
procedures are called repeatedly. SPs can only return a true or a
false (1/0). May or may not return a value
Benifits using SPs :
• Execution plan retention and reuse
• Query autoparameterization
• Encapsulation of business rules and policies
• Application modularization
• Sharing of application logic between applications
• Access to database objects that is both secure and uniform
• Consistent, safe data modification
• Network bandwidth conservation
• Support for automatic execution at system start-up

2. What do you consider are the best reasons to used


stored procedures in your application instead of passing
Transact-SQL code directly to SQL Server?
First and foremost, a stored procedure is a precompiled set of
code, where passing T-SQL through languages such as VB, Visual
FoxPro, etc., means that the set of code needs to be compiled
first. Although T-SQL within VB, etc., can be prepared before
running, this is still slower than using a stored procedure.

Then, of course, there is the security aspect, where, by building a


stored procedure, you can place a great deal of security around it.
When dealing with sensitive data, you can use an encrypted
stored procedure to hide sensitive columns, calculations, and so
on.

Finally, by using a stored procedure, I feel that transactional


processing becomes a great deal easier and, in fact, using nested
transactions become more insular and secure. Having to deal with

Jan 2007
Complied by John .K & Bharat

transactions within code that may have front end code, will slow
up a transaction and therefore a lock will be held for longer than
necessary.

3. What are some techniques for writing fast performing


stored procedures?
Fast performing stored procedures are like several other areas
within T-SQL.
• Revisiting stored procedures every six months or so, to
ensure that they are still running at their optimum
performance is essential.
• Work with as short a transaction area as possible, as lock
contention will certainly impact performance.
• Recompiling your stored procedures after index additions if
you are unable or not wishing to restart SQL Server, will also
ensure that a procedure is using the correct index, if that
stored procedure is accessing the table which has received
the new index.
• If you have a T-SQL command that joins several tables, and
it takes a long time to return a value, first of all check out
the indexes. But what you may find tends to help, is to break
down the code and try to determine which join is causing the
performance problem. Then analyze this specific join and see
why it is a problem.
• Always check out a stored procedure's performance as you
build it up by using the SHOWPLAN commands.
• Also, try to use EXISTS, rather than a JOIN statement. An
EXISTS statement will only join on a table until one record is
found, rather than joining all the records .
• Also, try to look at using subqueries when you are trying to
find a handful of values in the subquery statement, and
there is no key on the column you are looking up on.

4.Can you give an example of Stored Procedure? - sp.xls


sp_helpdb - Reports information about a specified database or all
databases.

Jan 2007
Complied by John .K & Bharat

sp_who2 - Provides information about current Microsoft® SQL


Server™ users and processes. The information returned can be
filtered to return only those processes that are not idle.

sp_renamedb - Changes the name of a database.

sp_configure - Displays or changes global configuration settings


for the current server.

sp_dboptions - Displays or changes database options.


sp_dboption should not be used on either the master or
tempdb databases. sp_dboption is supported for backward
compatibility. Use ALTER DATABASE to set database options.

sp_spaceused – Displays the number of rows, disk space


reserved, and disk space used by a table in the current database,
or displays the disk space reserved and used by the entire
database.

These are a set of system defined stored procedures. We can also


have user defined stored procedures which can be called in
similar way.

5.How will you test the stored procedure taking two


parameters namely first name and last name returning
full name?
Execute the store procedure and supply two parameters

6. How do you measure the performance of a stored


procedure?
• Look to see if it is recompiling each time store procedure is
executed.
• Check the execution plan to see what kind of index it is
using.
• Update statistics and check if performance is improved for
store procedure.

7. Difference between Function and Procedure-in general?

Jan 2007
Complied by John .K & Bharat

• Stored procedure returns always integer value by default


zero. Function return type could be scalar or table or table
values.
• Stored Procedure are precompiled execution plan. Functions
are not.
• Stored Procedure returns more than one value at a time.
Function returns only one value at a time.
• Function should return a value but S.Procedure may or may
not return a value.
• Function does not return the images or text. SP returns all.
• Functions are used for computations. SP can be used for
performing business logic.

8. Difference between Function and Stored Procedure?


Parameters used within stored procedure let’s to pass value as
input and receive passed back value as output. But the outward
flowing parameter cannot be used as data elsewhere. Rather it
simply indicates either success or failure of the stored procedure.
But functions can used the output they produce as data in
subsequent operational steps.
SP can have a return statement or sometimes they don’t. But
functions should have a return statement specified.

9.Can a stored procedure call another stored procedure. If


yes what level and can it be controlled?
Yes a sp can call another sp. They can call up to 32 levels.

10. Can a stored procedure call itself (recursive). If yes


what level and can it be controlled?
Yes a sp can call itself .Maximum of 32 levels.

11. Explain sp_configure commands, set commands?


sp_configure- Displays or changes global configuration settings
for the current server.
sp_setapprole-Activates the permissions associated with an
application role in the current
database.

Jan 2007
Complied by John .K & Bharat

Sp_setnetname-Sets the network names in sysservers to their


actual network computer names for remote instances of SQL
Server. This procedure can be used to enable execution of remote
stored procedure calls to computers that have network names
containing invalid SQL Server identifiers.
sp_setreplfailovermode-Allows you to set the failover operation
mode for subscriptions enabled for immediate updating with
queued updating as failover. This stored procedure is executed at
the Subscriber on the subscription database.
sp_settriggerorder-Specifies which AFTER triggers associated
with a table will be fired first or last. The AFTER triggers that will
be fired between the first and last triggers will be executed in
undefined order.

12. What is an extended stored procedure? Can you


instantiate a COM object by using T-SQL?
An extended stored procedure is a function within a DLL (written
in a programming language like C, C++ using Open Data Services
(ODS) API) that can be called from T-SQL, just the way we call
normal stored procedures using the EXEC statement. See books
online to learn how to create extended stored procedures and
how to add them to SQL Server.

Yes, you can instantiate a COM (written in languages like VB, VC+
+) object from T-SQL by using sp_OACreate stored procedure.
Also see books online for sp_OAMethod, sp_OAGetProperty,
sp_OASetProperty, sp_OADestroy. For an example of creating a
COM object in VB and calling it from T-SQL, see 'My code library'
section of this site.

13. If a stored procedure is taking a table data type, how


it looks?
A table variable behaves like a local variable. It has a well-
defined scope, which is the function, stored procedure, or batch in
which it is declared.
Within its scope, a table variable may be used like a regular
table. It may be applied anywhere a table or table expression is

Jan 2007
Complied by John .K & Bharat

used in SELECT, INSERT, UPDATE, and DELETE statements.


However, table may not be used in the following statements:

INSERT INTO table_variable EXEC stored_procedure


SELECT select_list INTO table_variable statements.

table variables are cleaned up automatically at the end of the


function, stored procedure, or batch in which they are defined.
table
 variables used in stored procedures result in fewer
recompilations of the stored procedures than when temporary
tables are used.
Transactions
 involving table variables last only for the duration
of an update on the table variable. Thus, table variables require
less locking and logging resources.

14. Syntax
• Create procedure - Creates a stored procedure, which is a saved
collection of Transact-SQL statements that can take and return user-
supplied parameters.

CREATE PROC [ EDURE ] procedure_name [ ; number ]


[ { @parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]

[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]

[ FOR REPLICATION ]

AS sql_statement [ ...n ]

• Alter procedure - Alters a previously created procedure, created by


executing the CREATE PROCEDURE statement, without changing
permissions and without affecting any dependent stored procedures or
triggers.

ALTER PROC [ EDURE ] procedure_name [ ; number ]


[ { @parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH

Jan 2007
Complied by John .K & Bharat

{ RECOMPILE | ENCRYPTION
| RECOMPILE , ENCRYPTION
}
]
[ FOR REPLICATION ]
AS
sql_statement [ ...n ]

• Drop procedure - Removes one or more stored procedures or


procedure groups from the current database.

DROP PROCEDURE { procedure } [ ,...n ]

Jan 2007

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy