Professional Documents
Culture Documents
FAQ'S S.P
FAQ'S S.P
FAQ'S S.P
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));