Web Development - How do you approach it?

Goog Feed, General, ColdFusion, SQL    Comments (3)


I work for a school district, and I am the first developer they have every had. So I've been developing web apps for them for about six months now using ColdFusion with Model-Glue and SQL, all authentication is done through Active Directory.

Lately I started thinking about the future and how to best do development. When I was hired they had a list of projects they needed done, and they needed to be done yesterday. So I began coding, coding, coding. No design docs, not project scope docs, no database diagrams, nothing of the sort.

My first thought was to slow down and start a process for each project thrown my way. It may be more tedious, time-consuming and boring, but I really think long-term, a well laid out play with an approval process up front lends itself to successful projects.

I am a big fan of the Interface-Driven approach to web development, but I wanted to get some feedback from the community...

What methods/steps do you employ when taking on a project?
Do you build out pieces and release them or wait till the project is complete?
What type of documentation is used and why?
What have you found most beneficial to you in your experience with this?

Thanks in advance for your input!

Calculating Age In SQL

Goog Feed, General, SQL    Comments (3)


So, I was building an application at work that required me to know the age of an individual before further processing could be done on the record.

I thought this would be a piece of cake with the "dateDiff(yy,date1,date2)" function. That only gives the year difference. For instance if today's date is 10/16/2007 and you we're born on 10/17/1981, the dateDiff function would tell you that you're 26 years old when in fact you don't turn 26 till tomorrow.

This became problematic for me because I needed to know to the day how old this individual was at any given moment.

After a bit of browsing and small modifications on my end here is what I ended up using.

SELECT DATEDIFF(YY, '10/15/1981', getDate()) - CASE WHEN( (MONTH('10/15/1981')*100 + DAY('10/15/1981')) > (MONTH(getDate())*100 + DAY(getDate())) ) THEN 1 ELSE 0 END as Age

What the above SQL statement does is first grab the year difference. Then the CASE statement takes the month multiplied by 100 and adds the day of the date to that total. It also does the same thing for the second date passed in. It then evaluates the two and either leaves the year alone or subtracts one from it.

For this example I hard-coded the date of "10/15/1981" into the statement, but in my real-life example that became a sub-query based on an ID. Hope you find this as useful as I did.

ColdFusion: Stored Procedures For Beginners

General, ColdFusion, SQL    Comments (2)


So, you are a beginner in ColdFusion and you want to know the quick and dirty way to build a stored procedure and call it in ColdFusion.

Step One: Open SQL Server Enterprise Manager and browse to your database, you will find a section called "Stored Procedures", right click on it and select "New Stored Procedure".

Step Two: A window "Stored Procedure Properties" window will open where you will drop in your t-SQL code for running a query. Save it and you are good to go. Below you'll find the window and example code.

CREATE PROCEDURE dbo.getStuff
@firstname varchar(50)
AS

SELECT dbo.[User].firstName AS Firstname, dbo.[User].lastName AS Lastname, dbo.[User].email AS EmailAddress, dbo.Role.name AS Role,dbo.Organization.Name AS OrgName, dbo.Permission.name AS PermissionName
FROM dbo.RolePermission INNER LOOP JOIN
   dbo.UserOrganization INNER LOOP JOIN
dbo.[User] ON dbo.UserOrganization.UserID = dbo.[User].userID INNER LOOP JOIN
dbo.UserOrganizationRole ON dbo.UserOrganization.UserOrganizationID = dbo.UserOrganizationRole.userorganizationid INNER LOOP JOIN
dbo.Role ON dbo.UserOrganizationRole.roleid = dbo.Role.roleId ON dbo.RolePermission.roleid = dbo.Role.roleId INNER LOOP JOIN
dbo.Organization ON dbo.UserOrganization.OrganizationID = dbo.Organization.OrganizationID INNER JOIN
dbo.Permission ON dbo.RolePermission.permissionid = dbo.Permission.permissionID
WHERE dbo.[User].firstName = @firstname
GO

Step Three: Open up a ColdFusion document and drop the following code in to call the stored procedure. You'll need the datasource to the database where the strored procedure lives, the stored procedure name, and a name you will call your result, also pass in any parameters that the stored procedure needs.

<cfset myFirstname = "Joe" />

<cfstoredproc datasource="aDatasource" procedure="getStuff">
   <cfprocresult name="myQueryResultSet" />
   <cfprocparam type = "IN" CFSQLType="cf_sql_varchar" value="#myFirstname#" dbvarname="@firstname">
</cfstoredproc>

Dump your stored procedure result.

<cfdump var="#myQueryResultSet#">

And just like that, you've got results. A stored procedure is really quite simple and very useful.

Why use stored procedures?

Many times, depending on what you are doing a stored procedure is much quicker. Everytime you write a query in ColdFusion, the SQL server takes it and says... "What would be the most efficient way to run this query?" It then runs it. Whereas with a stored procedure, the "Execution Plan" of how to run the query most efficiently is stored in the system so the next time you call the stored procedure, it doesn't have to evaluate and come up with an "Execution Plan."

The main reason for using them is speed. You'll have to evaluate for yourself based on your situation at hand, what is quicker for the task at hand. And no, I don't recommend using stored procedures for every little query, but for some of the bigger more complex ones... WHY NOT???

NOTE:

Microsoft SQL and Sybase among others use Transact SQL as the language to write a stored procedure. It is extremely robust and evolved and it may behoove you to learn some of the functionality available to you to perform more advanced operations (branching logic, looping over results, etc...).

An excellent reference book on this subject can be found here.