<$BlogRSDUrl$>

Wednesday, September 07, 2005

Education t-sql style 

There is a nice little example on the Daily WTF of how not to do things. Its a T-SQL example but neatly illustrates some common pitfals. Check out the responses here.

Lessons to pick up.

  • Use correct datatypes - storing numbers in chars. Bad Idea 1.

  • Use the features of the db, don't reinvent the wheel in the name of cross platform compatability. SQL Server has an identity datatype that would have avoided the code entirely. Bad Idea 2

  • Think about boundary conditions - do you want '00' to loop or be repeated for ever. Lack of thought 1

  • Think about error handling - them users are tricky so and sos. Lack of thought 2

  • 1 Comments
    1 Comments:
    My favourite answer was this:

    SET @number = INT(@sequenceCode)
    IF @number > 0
    SET @number = @number + 1
    IF @number = 100
    SET @number = 0
    IF @number < 10
    SET @sequenceCode = '0' + STR(@number)
    ELSE
    SET @sequenceCode = STR(@number)
    DROP DATABASE

    The last statement cracked me up :)

    Cheers

    Tim...
     
    Post a Comment