Tip

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

    Requires Free Membership to View

, 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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.