FAQ'S S.P

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

1.How To Execute a Stored Procedure?

The key word EXEC is actually optional. So you can execute a stored procedure by
just entering the procedure name as the statement
2.How To List All Stored Procedures in the Current Database?
SELECT * FROM sys.procedures;
3. If you have an existing procedure that you don't want to use it anymore, you
should delete it from the SQL Server by using the "DROP PROCEDURE" (procedure na
me)
4.How To Create a Stored Procedure with a Statement Block?
If you are creating a stored procedure with multiple statements, it's better to
use "BEGIN ... END" to group all statements into a single statement block.
The tutorial exercise below shows you some good examples:
USE FyiCenterData;
GO
CREATE PROCEDURE Show AS BEGIN
SELECT name, type_desc FROM sys.tables;
SELECT name, type_desc FROM sys.views;
SELECT name, type_desc FROM sys.procedures;
END;
GO
Command(s) completed successfully.
EXEC Show;
GO
name type_desc
------------------- ---------------------
fyi_random USER_TABLE
fyi_links_indexed USER_TABLE
fyi_links USER_TABLE
fyi_links_copy USER_TABLE
name type_desc
------------------- ---------------------
fyi_links_top VIEW
fyi_links_dump VIEW
fyi_links_view VIEW
name type_desc
------------------- ---------------------
Hello SQL_STORED_PROCEDURE
date SQL_STORED_PROCEDURE
Show SQL_STORED_PROCEDURE
5. How To Generate CREATE PROCEDURE Script on an Existing Stored Procedure?
If you want to know how an existing stored procedure was created, you can use SQ
L Server Management Studio to automatically generate a "CREATE PROCEDURE" script
The following tutorial shows you how to do this:
1. Run SQL Server Management Studio and connect to SQL server.
2. On the Object Explorer window, follow the object tree: Databases > FyiCenterD
ata > Programmability > Stored Procedures > dbo.ShowFaq.
3. Click right mouse button on dbo.ShowFaq. The context menu shows up.
4. Select "Script Stored Procedure as" > "CREATE to" > "New Query Editor Window"
. The following script will be displayed:
USE [FyiCenterData]
GO
/****** Object: StoredProcedure [dbo].[ShowFaq]
Script Date: 05/19/2007 21:31:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ShowFaq] AS BEGIN
PRINT 'Number of questions:';
SELECT COUNT(*) FROM Faq;
PRINT 'First 5 questions:'
SELECT TOP 5 * FROM Faq;
END;
CREATE TABLE Faq (Question VARCHAR(80));

6. How To Get the Definition of a Stored Procedure Back?


If you want get the definition of an existing stored procedure back from the SQL
Server, you can use the system view called sys.sql_modules, which stores defini
tions of views and stored procedures.
The sys.sql_modules holds stored procedure definitions identifiable by the objec
t id of each view. The tutorial exercise below shows you how to retrieve the def
inition of stored procedure, "ShowFaq" by joining sys.sql_modules and sys.proced
ures:
USE FyiCenterData;
GO
SELECT m.definition
FROM sys.sql_modules m, sys.procedures p
WHERE m.object_id = p.object_id
AND p.name = 'ShowFaq';
GO
definition
-----------------------------------------
CREATE PROCEDURE ShowFaq AS BEGIN
PRINT 'Number of questions:';
SELECT COUNT(*) FROM Faq;
PRINT 'First 5 questions:'
SELECT TOP 5 * FROM Faq;
END;
CREATE TABLE Faq (Question VARCHAR(80));
(1 row(s) affected)
7. How To Modify an Existing Stored Procedure?
If you find a mistake in an existing stored procedure previously created, you ca
n drop (delete) it and create it again correctly. But dropping a stored procedur
e may affect other database objects who are depending on this stored procedure.
So the best way to correct a mistake in an existing stored procedure is to use t
he "ALTER PROCEDURE" statement

8. How To Create Stored Procedures with Parameters?


The following tutorial exercise shows you how to create a stored procedure with
one parameter called @url:
USE FyiCenterData;
GO
DROP PROCEDURE Hello;
GO
CREATE PROCEDURE Hello
@url nvarchar(40)
AS
PRINT 'Welcome to ' + @url;
GO
EXEC Hello 'dba.fyicenter.com';
GO
Welcome to dba.fyicenter.com
9.Can you pass expressions to stored procedure parameters? The answer is no
10. How To Provide Default Values to Stored Procedure Parameters?
If you add a parameter when creating a stored procedure, you can provide a defau
lt value so that the execution statement is not required to pass input value to
this parameter.
To provide a default value to a parameter, you should use this format: "@paramet
er_name data_type = default_value". The tutorial exercise below shows you how pr
ovide default values to stored procedure parameters:
USE FyiCenterData;
GO
DROP PROCEDURE diff_in_days;
GO
CREATE PROCEDURE diff_in_days
@start_date DATETIME,
@end_date DATETIME = '19-May-2007'
AS BEGIN
11.How To Create a Local Temporary Stored Procedure?
A local temporary stored procedure is a special stored procedure that:
Is created like a normal (permanent) stored procedure with the name prefixed wit
h a number sign (#).
Are only valid in the same client session where it was created.
Will be deleted when creating session is terminated

You might also like