I have come up with a set of what I feel are best practices, and would be very interested in feedback from the community. My most recent experience in this area has been with microsoft sql server 2000, so I will focus on that in this node.
CREATE PROCEDURE dbo.SOMETHING_SAVE @something_id int, @name varchar(50) AS /* Does blah blah -- Changed on 2006-09-07 to resolve ticket #570 */ -- Do stuff GO
Additionally, when moving from a development to production environment you need to be able to easily identify the changes that have been made.
By doing this you can restrict the level of access each database user is granted to a much finer level. Different interfaces into the system (e.g. admin vs external user) can have access to specific procedures which adhere to business logic.
Another way I like to use this is to have password verification be a black box.
And finally, it overs an additional protection from SQL Injection. If they can't touch the table, they are less dangerous.$sql = q{EXEC AUTH_USER @username = ?, @password = ?}; my ($user_id) = $dbh->selectrow_array($sql,{},$username,$password);
Examples: SOMETHING_BY_ID, SOMETHING_SAVE, SOMETHING_LIST. I often add an extra level to that by prefixing the name with an abbreviation of the intended interface. e.g.:
I find that this keeps related items grouped when ordered alphabetically.
The profiler tool is also useful for identifying bottlenecks.
An important thing to remember is that it will not intelligently switch what datatype is being compared, so if some of the fields are char and some are integer, it will try to make them all integer - and cause an error
To avoid this you need to have multiple CASE statements, with the WHEN clauses grouped by data type.
order by CASE @order WHEN 'name' THEN lastname WHEN 'state' THEN state END, CASE @order WHEN 'name' THEN firstname END, CASE @order WHEN 'user_id' THEN user_id END
CREATE PROCEDURE [dbo].[dynamic_example] @lastname varchar(50) = NULL, @firstname varchar(50) = NULL AS SELECT * from Users WHERE (@lastname IS NULL OR users.lastname LIKE @lastname) AND (@firstname IS NULL OR users.firstname LIKE @firstname) GO
EXEC dynamic_example @firstname = 'chris%'
In reply to Moving SQL from perl to Stored Procedures by imp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |