Wednesday, July 1st, 2009

Writing SQL Stored Procedures

If you can write things like updates or selects in SQL, then you can already write stored procedures.

A stored procedure in SQL is a statement that has already been written but is then saved in the server database. Stored procedures writing is extremely useful to SQL users who have to process the same query over and over again. Once you have placed data into a stored procedure, you can always run it from the database’s command environment.

In order to be able to write SQL stored procedures, you’ll need three things: a database management system; a database contained within and as an integral part of the database management system; and some kind of text editor such as Notepad, so that you can make the SQL writing life easier on yourself by writing your procedures here and then cut-and-pasting into the New Stored Procedure window.

Once you have determined that you possess all the right tools, you’ll need to figure out what procedures you want stored (logically enough, hrm?). You might want to do things like create a VIEW onto a table, or change something in a database, or view something in a database, or put new wine into old wineskins in a database (figuratively!). You’ll also want to determine whether or not a certain query repeats often enough for you to expend the energy in writing a code for its response procedure to be stored and be able to be called up as needed.

To write a procedure in SQL, begin by accessing your Enterprise Manager and selecting your Stored Procedures option. You’ll find this as a subdivision of your database tree. Don’t pay any attention to all of the pre-existing stored procedures that you’ll see there when you get there. However, you do want to right-click on any one of them to be able to open up New Stored Procedure, and then…proceed with your code writing.

 

Comments are closed.