.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

SQL For Programmers – Stored Procedure Basics

September 16, 2008 By: Dave

trav-035 Now that we’ve established a reason for using them, let’s continue with the basic syntax of the stored procedure. I think you’ll find that it is not that dissimilar to writing a procedure in any other language. Although, as we move on, I think you’ll find that the syntax we have to use to get anything done is quite cryptic.

The first thing that makes any procedure a procedure is the ability to define the procedure name and define the parameters that can be passed. This is also true of stored procedures.

The basic structure of a stored procedure looks like:

CREATE PROCEDURE procedureName
(
/* Parameters go here */
)
AS
/* Body of the Procedure
   goes here. */

If you later needed to change this procedure, you would use this syntax:

ALTER PROCEDURE procedureName
(
/* Parameters go here */
)
AS
/* Body of the Procedure
   goes here. */

If you don’t have any parameters to pass, you can leave out the parentheses like so:

CREATE PROCEDURE procedureName
AS
/* Body of the Procedure
   goes here. */

You can define parameters to your procedure using comma delimited lists of @ prefixed names. You can name them whatever you want. Also keep in mind that SQL is not case sensitive. When you define the variables, you will also need to specify the type and, optionally, the size of the variable.

So to pass in a string and an integer, you would pass in:

CREATE PROCEDURE procedureName
(
   @StringVar as VARCHAR,
   @IntVar as Int
)
AS
/* Body of the Procedure
   goes here. */

If you wanted to limit the size of the string to 50 characters, you would use:

CREATE PROCEDURE procedureName
(
   @StringVar as VARCHAR(50),
   @IntVar as Int
)
AS
/* Body of the Procedure
   goes here. */

Then, if you wanted to use those variables as part of your where clause, you would use:

CREATE PROCEDURE procedureName
(
   @StringVar as VARCHAR(50),
   @IntVar as Int
)
AS
   SELECT fieldOne, fieldTwo, etc FROM tableName
   WHERE stringField=@stringVar AND intField=@intVar

You can find a full list of datatype definitions at the Microsoft site:

http://msdn.microsoft.com/en-us/library/ms187752.aspx

By default, the parameters are input only. I’ve found that that is all I need. But you may need to return a value through a parameter for some reason. You can do that by defining the parameter as an OUTPUT parameter:

CREATE PROCEDURE procedureName
(
   @StringVar as VARCHAR(50),
   @IntVar as Int OUTPUT
)
AS
/* Code that does something
   meaningful */
   SELECT @IntVar = 20

Notice you have to assign the output parameter a value.

All stored procedures either return nothing or they return a set of rows. So a better way of writing the stored procedure so that it returns 20 is to use something like this:

CREATE PROCEDURE procedureName
(
   @StringVar as VARCHAR(50),
   @IntVar as Int OUTPUT
)
AS
/* Code that does something
   meaningful */
   SELECT 20 as intField

where intField could be any meaningful name that could be used as a column name.

 

Other post in SQL For Programmers
  • SQL For Developers - 9 Reasons to bother - August 13th, 2008
  • SQL for Programmers - CREATE/DROP Database - August 20th, 2008
  • SQL For Programmers - CREATE TABLE - August 22nd, 2008
  • SQL For Programmers - ALTERing the TABLE - September 1st, 2008
  • SQL For Programmers - Finding a String - September 3rd, 2008
  • SQL For Programmers - Finding IN a List - September 8th, 2008
  • SQL For Programmers - Stored Procedures (Better than LINQ) - September 10th, 2008
  • SQL For Programmers - Stored Procedure Basics - September 16th, 2008
  • Basic SQL Commands - SQL For Programmers - September 18th, 2008
  • SQL IF/WHILE Blocks - SQL For Programmers - September 30th, 2008
  • SQL WHILE - SQL For Programmers - October 2nd, 2008
  • Temporary Tables - SQL For Programmers - October 7th, 2008
  • SQL CURSOR - SQL For Programmers - October 13th, 2008
  • SQL CURSOR Performance - SQL For Programmers - October 22nd, 2008
  • Random in SQL - SQL For Programmers - November 4th, 2008
  • SQL - Filtering WHERE condition on two rows - November 26th, 2008
  • SQL - Transactions - April 15th, 2009

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • SQL – Filtering WHERE condition on two rows
  • Random in SQL – SQL For Programmers
  • SQL WHILE – SQL For Programmers
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: .net, ms-sql, sql, tsql

One Response to “ SQL For Programmers – Stored Procedure Basics ”

  1. # 1 SQL Server Stuff « QuantuMatrix’s Weblog Says:
    April 16th, 2009 at 12:05 am

    [...] SQL For Programmers – Stored Procedure Basics – September 16th, 2008 [...]

← DotNetNuke Modules – Finding The Page a Module is On
DotNetNuke Modules – Caching →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    September 2008
    S M T W T F S
    « Aug   Oct »
     123456
    78910111213
    14151617181920
    21222324252627
    282930  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.