Developing Matt

My Technical Journal

The Lazy Programmer

with one comment

There once was a lazy programmer who did nothing but sat around reading the newspaper at work.  Lazy programmer would listen to audio books, surf the web for amazon deals, and watch youtube videos.  Lazy programmer sat around and ate potato chips in between laughing at the jokes he found on forums he constantly monitored about his extravagant online gaming addiction that he sustained at night.  People would walk by and get irritated and complain to their coworkers.  Lots of murmuring about this programmer and the apparent lack of motivation to do the job.  But the boss never did anything, never said anything, never talked to the lazy programmer about this individual’s lack of apparent work ethic.  Everyone wanted the lazy programmers job, but the truth is no one could do the job.  The lazy programmer was worth his weight in gold because if something needed to be done lazy programmer would take care of things quickly and efficiently, because he didn’t like to work. 

Someday we will all be there, but it’s going to take some careful considerations of how to most effectively get things done smarter and more efficiently.  Here is a script that I use for various sundry things to retrieve column names and datatypes out of a table.  For example, if I need to create a procedure that has almost all the parameters in a table I will do something like this to get the declaration section of the procedure

select ‘@’ +
,datatype  = +  case when ‘varchar’  then ‘(‘ + CAST( s.length as varchar(15)) + ‘)’
when ‘char’  then ‘(‘ + CAST( s.length as varchar(15)) + ‘)’
else ” end
from syscolumns s
join systypes st on s.xtype = st.xtype
where = 1765581328 –-database id

And if I need to build a where clause out of these same parameters I would do something like this (although not an efficient way to do this for a good execution plan)

select ‘and (‘ + + ‘=@’ + + ‘ or @’ + + ‘ is null )’
from syscolumns s
join systypes st on s.xtype = st.xtype
where = 1765581328


Written by matt

June 11, 2010 at 12:38 pm

Posted in T-Sql

One Response

Subscribe to comments with RSS.

  1. Hey Matt Clingan, my name is Matt Conrad. I met you at a .NET user’s group meeting once a long time ago. Somehow I bumped into your blog a few days ago.

    I like using the sys tables as coding tools too. :) I suspect you already know this trick, but just in case you don’t, you can say:

    where = object_id(‘table_name’)

    instead of figuring out the numeric id beforehand.

    I like your blog. Happy coding.

    The other Matt

    August 14, 2010 at 1:31 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: