browse by category or date

Consider you have a stored procedure that perform a SELECT operation for an object which is characterized by the ‘Start Date’ and ‘End Date’. Let’s assume we have the following TABLE:

Table Name: Events

Columns:

EventID int
EventName Varchar(64)
EventDesc Varchar(512)
EventStartDate DateTime
EventEndDate DateTime

And let’s assume that we have the following stored procedure:

CREATE PROCEDURE [dbo].[ListFilteredEvents]
	-- Add the parameters for the stored procedure here
	@StartDate datetime,
	@EndDate datetime
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

        -- Insert statements for procedure here
	SELECT
		Events.EventID,
		Events.EventName,
		Events.EventDesc,
		Events.EventStartDate,
		Events.EventEndDate
	FROM 
		Events
	WHERE 
		Events.EventStartDate>= @StartDate AND
                Events.EventEndDate<= @EndDate
	ORDER BY 
		Events.EventStartDate
END

Now what will happen if somehow the SP caller will pass blank value as the StartDate and/or EndDate? Error (Exception will be thrown) of course. If we have access to the source code of the application that execute this stored procedure, we could easily error-prevention at the application level. But if our access is only limited to database level, we could do the following:

CREATE PROCEDURE [dbo].[ListFilteredEvents]
	-- Add the parameters for the stored procedure here
	@StartDate datetime,
	@EndDate datetime
AS
BEGIN
   IF @StartDate IS NULL
   BEGIN
      -- Set it with the smallest value of DateTime data type
      SET @StartDate=CAST('1753-01-01' as DATETIME) 
   END

   IF @EndDate IS NULL
   BEGIN
      -- Set it with the biggest value of DateTime data type
      SET @EndDate=CAST('9999-12-31' as DATETIME)
   END

   BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

        -- Insert statements for procedure here
	SELECT
		Events.EventID,
		Events.EventName,
		Events.EventDesc,
		Events.EventStartDate,
		Events.EventEndDate
	FROM 
		Events
	WHERE 
		Events.EventStartDate>= @StartDate AND
                Events.EventEndDate<= @EndDate
	ORDER BY 
		Events.EventStartDate
   END
END

This way we could prevent Error/Exception being thrown whenever a blank value is passed into the stored procedure.

Important Update: Saturday, 21 April 2007 13.43 PM -- Thanks for Bro MCA for pointing out a better solution =)

CREATE PROCEDURE [dbo].[ListFilteredEvents]
	-- Add the parameters for the stored procedure here
	-- Set it with the smallest value of DateTime data type
        @StartDate DateTime='1753-01-01',
	-- Set it with the biggest value of DateTime data type
        @EndDate DateTime='9999-12-31' 
AS 
BEGIN
   -- SET NOCOUNT ON added to prevent extra result sets from
   -- interfering with SELECT statements.
   SET NOCOUNT ON;

   -- Insert statements for procedure here
   SELECT
	Events.EventID,
	Events.EventName,
	Events.EventDesc,
	Events.EventStartDate,
	Events.EventEndDate
   FROM 
	Events
   WHERE 
	Events.EventStartDate>= @StartDate AND
        Events.EventEndDate<= @EndDate
    ORDER BY 
    Events.EventStartDate
END

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

I was just browsing through MSDN website when I noticed something that all this time I considered it very trivial. Apparently in VB, you need to give every line apostrophe (‘) to make that line a comment (I know this). And you can’t use the underscore (_) to continue the comment, you still need to give the apostrophe again in the next line (now that is new).

Very contrast compared to other language such as C# (or other similar language in that matter, like C/C++, Java), you can use double slashes (//) to start a comment. To make multiple-line comment, you need to start it with /* and end it */

The comparison becomes a moot point if you are using Visual Studio. You can easily select the lines, and press Ctrl+K+C to comment it, and Ctrl+K+U to uncomment the selected lines.

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

It is indeed a sad moment for us. An Education institution where pupils learn from their masters has been tainted with blood. In United States, we have another school rampage that resulted 31 people dead and dozens injured. Meanwhile in Indonesia, a government-sponsored School caused a furore by covering up a student’s death.

United States with its gun ownership law is not foreign to school shooting. The Columbine incident, the more recent shooting at Amish elementary school and the latest shooting at Virginia Tech. Hopefully US will introduce a better gun-law to protect its citizens.

Meanwhile in Indonesia, many bloggers are expressing their anger and emotion with the latest killing from Institute of Public Administration(IPDN). The government-sponsored school has been known for its semi-military education style, and its students who are killed by their classmates/seniors. An online petition to close down the school has been created.

Hardono’s take

US Federal/State Government should put a metal detector in every school in United States. They could change the budget, instead of allocating so much money for Iraq, the money could be used to fortify every school in US. If the shootings are moved from schools to malls, the malls should also be fortified with metal detectors. If the same thing happened to the beaches, the beaches should also be fortified with metal detectors. Hmm.. shouldn’t they do a simpler approach by making the public access to firearms difficult/near to impossible?

STPDN should be closed and rent the building to other school such as UI/Binus/IPB, this way we won’t waste the investments that government has made in IPDN. And I believe the Indonesian government should hire more professionals with management education background. This way, we will have a government with a better management qualities, and with more resistance to corruption.

And lastly, my thoughts and prayers are for families of the victims. I pray for your strength and your well-being, bracing through this remorseful moment. For the rest of us, I hope we could do something to make this world a better place to life.

Notable bloggers with their opinion on IPDN Scandal:

  1. Kang Kombor
  2. Bro Fertob

List of management school in the region (replacements for IPDN?):

  1. Singapore Management University
  2. Institut Pengembangan Manajemen Indonesia
  3. Magister Management – Univ. Gadjah Mada
  4. The Complete List

The online petition to close down IPDN:

  1. Petition to close down IPDN

Reading on School Shootings in US:

  1. Columbine High School Shooting
  2. Amish School Shooting
  3. Virginia Tech. Shooting
  4. List of other School massacres

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant: