advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
CoDe Magazine
More on Stored Procedures
Partners & Affiliates
advertisement
advertisement
advertisement
CoDe Magazine
Subscribe to CoDe Magazine
Average Rating: 3.5/5 | Rate this item | 10 users have rated this item.
Email this articleEmail this article
 
Building a Stored Procedure Generator
Creating basic data access stored procedures is time consuming and boring work. Relieve the tedium by writing code that writes these stored procedures for you. 

advertisement
e all know that the most efficient way to access data from a database is to use stored procedures. For most applications, these stored procedures follow the same basic design whereby you list every field that you need to retrieve or save. For large tables or a large number of tables, writing these stored procedures can be very cumbersome and prone to typographical errors.

This article demonstrates how you can build your own stored procedure generator. You can then modify and enhance this stored procedure generator to tailor the results to your stored procedure style.

Define Your Stored Procedure Structure
Stored procedures that perform complex and unique operations are best done manually. But most stored procedures perform basic database operations such as retrieve, insert, update, and delete. Often, these operations are accomplished with stored procedures that look identical except for the actual table and field names. These types of stored procedures are prime candidates for a stored procedure generator.

A stored procedure generator is basically an application that, when run, creates a stored procedure script. You can then save the script within a Database project and run it against your database to create the stored procedure. Details on using Database projects are provided later in this article. See the sidebar More On Stored Procedures for useful links to background information on stored procedures.

The first step in building a stored procedure generator is to define the basic structure for your most common types of stored procedures.

Let's use the Customers table from the Northwind database as an example. A script that defines a stored procedure to retrieve all the fields within a table given a unique ID might look like this:

   CREATE PROCEDURE dbo.CustomersRetrieveByID_sp 
      @CustomerID nchar(5) 
   AS
      SELECT  
         CustomerID, CompanyName, ContactName, 
         ContactTitle, Address, City, Region, PostalCode,  
         Country, Phone, Fax 
      FROM Customers 
      WHERE CustomerID = @CustomerID
A stored procedure generator is basically an application that, when run, creates a stored procedure script.
This example uses a five-character string for the primary key, but best practices often dictate using a unique, meaningless, numeric key (such as that defined with an identity column in SQL Server). If your tables use a numeric key, just change the data type of the stored procedure parameter.

Another common data-retrieval stored procedure collects all the key values for all the rows in the table. You can bind the result of this stored procedure to a drop down list or other user-interface element for user selection. For example, to define a stored procedure that returns a list of key fields for all customers, your stored procedure script might look like this:

   CREATE PROCEDURE dbo.CustomersRetrieveList_sp 
   AS
      SELECT CustomerID, CompanyName, ContactName, 
             ContactTitle, Address 
      FROM Customers 
      WITH (NOLOCK)
This case requires no parameter because it retrieves all the rows and includes them in the list. If instead of deleting rows you add a status field to your table to mark rows as active or deleted, then you will need to add a WHERE clause to retrieve only those rows that have an active status.

This article contains the code to generate both of these common types of "retrieve" stored procedures. You may want to add more types, such as insert, update, and delete stored procedures. If so, you should also define the structure for those types of procedures. Common practice is to create one stored procedure that supports insert, update, and delete operations, passing in the ADO RowState value as a parameter that determines which operation to perform.

  Next Page: Retrieve the Column Data
Page 1: IntroductionPage 3: Generate the Script
Page 2: Retrieve the Column DataPage 4: Finish the User Interface
© Copyright Component Developer Magazine and EPS Software Corp., 2006
Untitled
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES