An advantage of using a stored procedure

An advantage of using a stored procedure
Shelley Powers

Suddenly, in most enterprises, ASP means Application Service Provider. But most programmers still know the acronym ASP as Active Server Page. This tip from Shelley Powers book Developing ASP Components, published by O'Reilly & Associates, looks at the advantages of using stored procedures in ASP components.


There is an advantage to using stored procedures from within ASP components. Notice from the code below that we can delete the author without necessarily knowing that there is a foreign key reference defined for the author's table, or even without knowing that there is an author's table. This is, to me, a real advantage to using stored procedures: data abstraction, or hiding data details from the process implementation.

ASP component call to a stored procedure, with error handling

try
{
	// get Request object and form collection
	vtRequest = iObjProps.GetProperty ("Request");
	iObjRequest = (IRequest)vtRequest.getDispatch();
	iObjReqDictionary = iObjRequest.getForm();

	// get author's last name
	vtRequest.putString("lastname");
	vtRequest = iObjReqDictionary.getItem(vtRequest);

	// create command
	Command cmd = new Command ();
	cmd.setActiveConnection(conn);
	cmd.setCommandType (AdoEnums.CommandType.STOREDPROC);
	cmd.setCommandText ("remauthor");

	// create parameter
	Parameters parms = cmd.getParameters();
	Parameter parm = cmd.createParameter("lastname",
		AdoEnums.DataType.VARCHAR, AdoEnums.ParameterDirection.INPUT,
                20, vtRequest.toString());

	// append to command parameters
	parms.append (parm);

	// execute
	conn.beginTrans();
	cmd.execute();
	conn.rollbackTrans();

	// end message
	Variant vtMessage = new Variant ("Transaction Successful");
		IObjResponse.Write(vtMessage);
	}
	catch (com.ms.wfc.data.AdoException e)  {
		Variant vtError = new Variant (e.getLocalizedMessage());
	iObjResponse.Write(vtError);
	}
}

Consider the scenario where the database schema changes, and the result is that the author ID is now defined as two fields, not one--an author classification identifier and the author's own specific subclassification number. If the ASP components accessing the database accessed the tables directly using SQL, each of these components would need to be modified to handle the changes. This becomes an expensive operation. Add to this the fact that some implementations of the application may be using an older database, one that does not have the split author ID, and you have a version as well as implementation problem.

Now, if the data access layer were itself implemented as stored procedures, the ASP component would access the same stored procedure name, remauthor, and pass in the same parameter, the author last name. The database stored procedure would handle the actual processing, and would work with a one-field author ID in an older database, and a two-part author ID in newer databases. The processing components would require no changes at all.


To learn more about Developing ASP Components, or to buy the book, click here.

What did you think of this tip? Good? Bad? Email, and let us know.


This was first published in March 2001

Dig deeper on Windows Operating System Management

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

This Content Component encountered an error
Close