Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Creating a simple stored procedure



By:
Overview

As mentioned in the tutorial overview a stored procedure is nothing more than stored SQL code that you would like to use over and over again.  In this example we will look at creating a simple stored procedure.

Explanation

Before you create a stored procedure you need to know what your end result is, whether you are selecting data, inserting data, etc..

In this simple example we will just select all data from the Person.Address table that is stored in the AdventureWorks database.

So the simple T-SQL code excuting in the AdventureWorks database would be as follows which will return all rows from this table.

SELECT * FROM Person.Address

To create a stored procedure to do this the code would look like this:

USE AdventureWorks
GO

CREATE PROCEDURE dbo.uspGetAddress
AS
SELECT * FROM Person.Address
GO

To call the procedure to return the contents from the table specified, the code would be:

EXEC dbo.uspGetAddress
-- or
EXEC uspGetAddress
--or just simply
uspGetAddress

When creating a stored procedure you can either use CREATE PROCEDURE or CREATE PROC.  After the stored procedure name you need to use the keyword "AS" and then the rest is just the regular SQL code that you would normally execute.

One thing to note is that you cannot use the keyword "GO" in the stored procedure.  Once the SQL Server compiler sees "GO" it assumes it is the end of the batch.

Also, you can not change database context within the stored procedure such as using "USE dbName" the reason for this is because this would be a separate batch and a stored procedure is a collection of only one batch of statements.






More SQL Server Solutions











Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Notify for updates 


SQL tips:

*Enter Code refresh code     



Wednesday, September 13, 2017 - 5:30:17 AM - SANDEEP KUMAR Back To Top

 

 

its very helpful for all of us..Thank's


Monday, September 04, 2017 - 6:26:11 AM - Zia Sheikh Back To Top

Thanks Greg Robidoux!!! These tutorials are very helpful, keep it up.


Thursday, August 17, 2017 - 10:07:47 AM - sivakumar.sivaraj Back To Top

 very helpfull.


Monday, January 30, 2017 - 5:30:16 AM - niwar Back To Top

thank you


Tuesday, October 11, 2016 - 7:48:35 AM - Vrushali Wagh Back To Top

Very Helpful & simple to understand.


Saturday, June 04, 2016 - 1:15:02 AM - Yogesh Pandey Back To Top

VERY HELPFULL 


Tuesday, February 02, 2016 - 1:42:14 AM - bhavsinh Back To Top

how to put validation in user insert data in databse

 


Learn more about SQL Server tools