Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - Ending Stored Procedures Properly
(Continued from previous topic...)
How To End a Stored Procedure Properly?
Where the end of the "CREATE PROCEDURE" statement structure?
The answer is simple, the end of the statement batch.
Even if you are using a "BEGIN ... END" statement block,
the stored procedure structure is not going to end at the end of the statement block.
It will continue to the end of the statement batch, usually the GO command.
The tutorial exercise gives you a good example:
DROP PROCEDURE ShowFaq;
DROP TABLE Faq;
-- How this statement batch will be executed?
CREATE PROCEDURE ShowFaq AS BEGIN
PRINT 'Number of questions:';
SELECT COUNT(*) FROM Faq;
PRINT 'First 5 questions:'
SELECT TOP 5 * FROM Faq;
CREATE TABLE Faq (Question VARCHAR(80));
Number of questions:
Msg 208, Level 16, State 1, Procedure ShowFaq, Line 3
Invalid object name 'Faq'.
What happened here was that the "CREATE TABLE" statement was not execueted.
It was included as part of the stored procedure "ShowFaq".
This is why you were getting the error "Invalid object name 'Faq'."
(Continued on next topic...)
- What Are Stored Procedures?
- How To Create a Simple Stored Procedure?
- How To Execute a Stored Procedure?
- How To List All Stored Procedures in the Current Database?
- How To Drop an Existing Stored Procedure?
- How To Create a Stored Procedure with a Statement Block?
- How To End a Stored Procedure Properly?
- How To Generate CREATE PROCEDURE Script on an Existing Stored Procedure?
- How To Get the Definition of a Stored Procedure Back?
- How To Modify an Existing Stored Procedure?
- How To Create Stored Procedures with Parameters?
- How To Provide Values to Stored Procedure Parameters?
- What Are the Advantages of Passing Name-Value Pairs as Parameters?
- Can You Pass Expressions to Stored Procedure Parameters?
- How To Provide Default Values to Stored Procedure Parameters?
- How To Define Output Parameters in Stored Procedures?
- How To Receive Output Values from Stored Procedures?
- How To Create a Local Temporary Stored Procedure?
- Can Another User Execute Your Local Temporary Stored Procedures?