Wednesday, August 10, 2011

Making Mistakes - T-SQL Tuesday 021

SQL Tuesday Wednesday
I'm a database developer and sometimes a DBA, as you'll probably have realised, despite the amount of food and yarn that appears on this blog! Like all developers, I've made mistakes. Like all honest developers, I try my best to admit them, learn from them, and move towards being a better developer.

A better consultant than I (or, at least, one better recognised as such!), Adam Machanic, has posted a challenge to those of us in the SQL Server community who claim to be honest. You can see it by clicking on the caption of the logo on the right (I can't make the logo itself a link, as it is really a link to the page itself!).

The challenge is to publish a blog  post today (Aug 10th, 2011) talking about the bad code we've written. The aim is to provide people with examples of what not to do.

null values
One new thing you learn when you start out with databases is the concept of "I don't know". Rather like the famous statement "there are things that we know we don't know, and there are things that we don't know we don't know" implies, these unknowns have to be treated with care. One learns to use tri-valued logic (True, False, null (or "I dunno!"), for example). One also learns that, because of nulls, there are sometimes queries where the sums don't always add up as you might expect. Here's an example.

You have a table to report from. You've made it by pulling data from tables supplied by colleagues from their parts of the system. One of the fields defines the type of an item - let's say possible values are Computer, Printer, Router, and Unknown. As this is just a temporary table you're got in memory to organise the data for reporting from, you've not bothered with any referential integrity ...

Computer        2706
Printer          522
Router            47
Unknown          174
--------        ----
Total           3449


So you create your report and all looks good, until you see that the sum of the items that you've produced doesn't match the total sum - in fact, it's 551 too small. The real error is not in the query here that outputs from a sproc for an SSRS report: it's in the underlying assumptions!

select Type, count(Type) as 'Quantity'
from @taReport
where Type is not null
group by Type
order by Type
The programmer didn't realise that sometimes the Type might be unknown, so added the where Type is not null clause quite reasonably to avoid the report row saying that there were zero entries with type "NULL" (and, anyway, that always showed a zero!).

Adding this line just before the select statement would convert the null values into Unknowns, which is really what they are!

update @taReport set Type = 'Unknown' where Type is null

The Unknowns number goes to 725 and the total to 4000, which is the correct total number of objects!

Moral of the Story: A database contains a representation of the real world. It isn't a complete representation, to be sure, but so far as it goes, it must be accurate and complete. In this case the programmer made an assumption about the data quality that wasn't warranted.

Obsolete Code
When one works with a database with some history, such as when one is hired into a company with an already-functioning system, it behooves one to enquire into the history of the system. In one project I was investigating the reasons behind the terrible performance of some queries. One reason that I found was the use of functions in the where clauses of these queries - a well-known bad practice. Investigating further, I found that the functions (there were two or three only, used in a number of places) were very simple, and easily replaced with an existing native function in SQL Server 2000.

It was only after I had tracked down and fixed these function calls that I had a chance to learn more about the system history. It had originally been written in Access only, and these functions didn't exist so had to be written by hand! When the database part of the system was moved to SQL Server the code was re-written, apparently without any concern for whether any pieces were actually necessary!

Moral: Think about what you're doing! you may be creating something totally unnecessarily!

TTFN

Saturday, August 06, 2011

A Colour Nook!!

The wonderful AG, the Keeper of the Books, allowed me to get a new eReader a couple of weeks ago to replace the Pocket PC (picture, right, from Wikipedia) I've had since about 2001. Don't get me wrong, now: the HP Jornada 568 has lasted a very long time for any piece of electronics, was relatively easy to program, has a nice clear screen for reading (a little small, I'll admit), and, recently, has been frequently the object of great curiosity from many people, asking if that was a prototype for a new Apple machine. To be honest, a piece of electronics that lasts 10 years of fairly continuous use definitely gets my respect.

Anyhow, it's coming to the end of its life, with the batteries starting to lose their ability to hold a decent charge and the beast as a whole becoming prone to testy reboots, so I finally decided that a new reader was needed. The problem was, as many have found, one of choice. Many people never moved to an eReader until Amazon introduced the Kindle, so their choice was easy, as they had no history of books to bring forward. I, on the other hand, have around 200 books in a variety of formats - .LIT, .EPB, .PDF, .CHM, .ePub, etc. - that I would really rather not buy all over again.

So, I looked around and found that the Nook Color is really a kind of mini-tablet, running Android 2.2. You could get an emulation on a PC in 2010, so I started playing with it there back then. I also started looking for ways of converting my eBooks (very few of which are Secure, or locked, luckily). I found a program called calibre, billing itself as an eBook manager program, that runs on Linux and Windows, and which will convert a number of formats into ePub, which is the Nook's "native" or preferred format.


Anyhow, I got my new toy a couple of weeks ago, and a few extras that I, for one, really consider necessary. These include a cover, of course, but one that can, as you can see, be used as a stand to hold the Nook at a convenient reading angle.

The next extra, equally important in the short term, but essential for the long term, is a supply of film screen covers to protect the capacitative screen from damage.

Finally, as I have used the small HP for some ten years and am totally accustomed to using a stylus to work the on-screen keyboard, a stylus. The one you see to the left is a rooCase, and comes in the form of a foam tip to an otherwise rather attractive ball-point.

So, it was a few days of play to explore the Nook, get used to expanding and shrinking the screen (annoying, that - web sites used to be written to self-adjust in the days of the old HP!). Anyway, it's certainly got some nice features. One thing I could do with is a set of smaller keys (as I'm not using my fat fingertips) with some extras, rather than having to continually hit an alt key to switch to another keyboard full of numbers and symbols. The text presentation is, on the whole, very good and at least the equal of that in the Microsoft Reader software on the Jornada, and the idea of swiping ones fingers on the screen to turn pages very quickly becomes natural. Then, after getting well stuck in to The Art of SQL, it was time to convert books from other formats. I can only say that calibre worked without any fuss or problems. It uses all the cpu power it can get its fingers on, and definitely isn't the fastest program on the planet, but the output (ePub in my case) is just as good as the originals that I fed it.  

Do I have any grouses about the Nook Color? Well, one or two. I'd like to be able to see the list of books that I have to select from in the equivalent of Windows Explorer's Detail view, instead of icons with only a quarter of the title in view. Of course, that may be possible - just a feature that I haven't found yet.
The other thing that surprised me to start with was the sheer weight of the thing! 654 grammes (with the cover that I bought), 1.44 pounds. Compared to the HP, which only weighed a few ounces, the Nook is a huge monster, and definitely takes getting used to - when reading in bed, for example.

Overall opinion: very favourable. I'd definitely recommend it to people with existing eBooks: ePub and PDF are readable immediately. I'm very happy, and can see the end of paper book purchases in this house looming closer.
TTFN

Saturday, July 16, 2011

New Foods ..... New Technologies

We went off to the H&L Food Store on Rivers Avenue in Charleston today. Very much the usual Asian food supermarket, except rather larger than most on the east coast. We cruised up and down the aisles, wondering what we could send to the far-flung DD ... some sweet things here, maybe some dried squid there ... (well, maybe not!). Certainly not the whole dried Croaker, neatly packaged, very flat, and staring beadily back at the prospective customer with a fixed and glazed stare.That would certainly freak granny out! Then there were lots of green teas in different packages for great-gran, Thai curry paste and ginger sweets for me, and some nice tea for the AG. Peas for the Offspring. Looks like there'll be a box heading out west very soon!

The AG rather liked that grooving on this little 85-cent can of peas, so we got it .... but it says "Dry Peas" on the outside, and, when I shake it, it definitely has water inside. Should they be "Self-Dry Peas"?

Food
So, it'll be stir-fry tonight, methinks. We have rice in the fridge from a few days ago, which is good, as it'll be dried out and not too wet to be fried. Chopped Kielbasa (thin medallions quartered) and maybe some other meats. Celery, mushrooms, some cooked carrots, and red bell peppers, all chopped and cooked a bit. I'll beat some eggs and then cook them in the wok to get a kind of scrambled egg, and then add the rice. After that the warmed meat and veggies go in to get properly hot before serving.

Tech
Finishing a revision go-through of LearnDevNow's SSRS 2008 course, before working therough The Art Of SQL Report Writing on Aranya's site (see Tech List on sidebar). I'm looking towards taking PragmaticWork's SSAS course as soon as I can, as I can see a need for it coming soon.

Home
It's been rather nice, living down here in Charleston for a while, so it's a shame that it may be ending soon. We've really been enjoying late-night swims together, without all the kids and without the glaring sunshine. We'll see what the future brings. Fun, hopefully, and enjoyment.
TTFN

Tuesday, July 12, 2011

An Example of Computer Evolution

I'm writing this on a laptop with a 17" screen, an attached 19" monitor, 4 GB of RAM, about 300 GB of disk space (mostly used, unfortunately), a couple of terabyte drives attached, and a 2 GHz AMD processor. Fairly normal, you might think, except that this PC was bought in 2006, making it five year old! Not surprisingly, it runs Windows XP.

The other day I downloaded a small piece of software in order to do some work, and was cautioned that the software required that my PC should have the following minimum assets:

The following minimum computer hardware and software requirements must be
met in order to access or retrieve the materials herein: IBM
compatible or Macintosh personal computer system with internet connectivity;
available hard disk drive space of at least 50 kb; a computer pointing device,
such as a mouse; 128-bit SSL (Secure Socket Layer) compatible browser, such as
Netscape 4.0 or Internet Explorer 4.0, or higher; Adobe Acrobat® Reader ® 5.0,
computer monitor capable of displaying internet web pages and graphics;
graphical user interface operating system, such as Windows 95, OS8 or later.


Seeing as I was actually viewing the requirements on an internet site, the requirements for internet connectivity and monitor capabilities seemed a little redundant. However, I find that I have forgotten how long ago it is that a PC would function with less free disk space than 50 kilobytes! Certainly well before 1996 and the advent of the Web for most people!

Still, it all worked flawlessly, which just goes to show you how well Sir Tim and his colleagues at CERN designed the Web in the first place.

TTFN

Sunday, July 10, 2011

Oops

I generally a clumsy oaf in many things - like steaming three fingers while cooking this evening!
Oops.

sometimes you just can't believe
how well you can be treated
we're told "forgive others" but that's
been difficult since we were created

I forget the timezone and ring too soon
I'm a klutz and drop the china on the floor
I misspell your town's name
and spill chocolate down your pure white blouse !

somehow you know that it isn't intended
that forgetting anniversaries is just how I am
your speciality seems to be forgiving me
which just helps me love you more
TTFN

Wednesday, July 06, 2011

From BJs to Costco

For the last few years we've been BJ's customers and very happy ones too. However, our nearest BJ's is now a couple of hundred miles away, so the question was: who now? Here's a few things we've found at Costco here in Charleston that are nice:
  • A cool room where all the berries and pre-packed fruit and veggies are kept.
  • A huge pile of Canon EOS Rebel T2i cameras (18 MPxl) at $800.
  • Single-unit Magic Bullets at $45 (with at least six mugs). Ours is still in NJ, but all the mugs will interchange.
  • Interesting and nice specialty breads - like cheese bread and various herb breads.
  • A good selection of frozen foods (I didn't find the 3-packs of peas and corn that BJ's has, but I haven't explored everywhere yet!).
  • A good variety of fresh food - fruit, veggies and meat - all well-kept (except for one occasion where the bottom layer of boxes of bananas pretty much dissolved and when someone got to them ... well, they still smelled nice, but were just about liquid!
There's a Sams Club that's nearer, but not nearly as good for fresh food. It has a lot of pre-packaged things - far more as a percentage than Costco.

The local Walmart (almost a mile away) has good fresh food too. On the other hand, I've been to a couple of the local store chain, Piggly Wiggly, and will definitely leave it!

TTFN

Saturday, July 02, 2011

A Phishing Email I Just Had To Share!

Welcome again from South Carolina! We're settling in to life here - a routine of getting up, having breakfast, going to work, coming home, eating a snack for dinner, going for a swim at 10 pm, and then sleeping the night away. Not such a terrible life. Work isn't too terrible either - after over three weeks of very compressed and rushed training (which is an unheard of luxury for someone on contract but which, in this case, was absolutely necessary) I find that I, at least, appear to be making some positive contributions to the project I'm assigned to, and am actually having my efforts recognised and appreciated. How nice!!!

That Spam
Anyhow, to the subject. I got this email this morning - twice!

ANTI TERRORIST & MONITORY CRIME DIVISION
FBI HEADQUARTERS IN WASHINGTON , D.C.
FEDERAL BUREAU OF INVESTIGATION
J. EDGAR HOOVER BUILDING
935 PENNSYLVANIA AVENUE, NW WASHINGTON , D.C. 20535-0001
www.fbi.gov

Dear Fund Beneficiary,

Some time ago, your friends from Africa and London, I mean the people that introduced you to the project approached you and requested you assist them conclude a money transfer deal they had with you, they requested you to assist them by removing the original contractors name, from the bank vetting computer and replacing them with your name and your details in order to make you appear as the rightful beneficiary of this funds. You agreed and they said you will share the money with them as soon as your name appears as the beneficiary.


So this is just a clue to show you that we are investigative and have all details to persecute you, but we shall give you the opportunity to receive the funds and make it clean, since it is coming into our dear country, but if you do not co-operate then we shall take negative actions.

Maybe you think, that we are joking , but by the time we start taking actions you would know how serious we are, you do not have his document in your files, if you did the account would not have been freezed. We did not believe this at first , but when we saw the wire we had no option than to contact you. It has come to the attention of our Money Trafficking investigation department, that you have some funds valued at U.S $8.3Million to your name , The said payment is awaiting adjudication and credited to your name this funds are from Inheritance 'willed 'to you from Nigeria C.B.N precisely and bank in London With full concern of The F.B.I and the Internal Revenue Service(IRS) 
You are under an observational /Investigation in connection with money laundering.
If your funds comes from a legitimate and legal source ,the proper guidelines for you to recover the right of transaction is for you to provide the DIPLOMATIC IMMUNITY SEAL OF TRANSFER (DIST) so that your funds will be legally processed and recorded and accounted for and then finally released to you .

FEDERAL BUREAU OF INVESTIGATION (ANTI-TERRORIST AND MONEY LAUNDERING DEPARTMENT )Is Here to wipe out terrorism , and will stop at no length in doing our duty for the American people .

You have 48 hours to produce legal proof of the below frozen wired transaction number coded:3456711 owned by you , You do not have any rights to receive this funds if the documented legal wire information is not complete. The very heart of FBI operations lies in our investigations--which serve, as our mission states, 'toprotect and defend the United States against terrorist and foreign intelligence threats and to enforce the criminal laws of the United States.' We currently have jurisdiction over violations of more than 200 categories of federal law. So you can see that the we can track you down through Investigative programes.We have your address an

Note ,the funds are very legal and from a good source, so as a matter of National interest you have to provide the documents to certify the money as clean funds. The funds are in the Citibank New York right now, and will be released to you as soon as this document is procured, even if you do not want the funds you still have to obtain the documents to clear your name as, the funds are already here, so the decision is yours. If you have the document then forward it to us immediately, if you do not have the documents then contact us immediately so that we can advice you on what it will take and where to obtain the documents.

Note: that you have just 48 hours to complete this process of national interest.

NOTE: If you failure to produce the above requirement in the next 48 hours, legal action will be taken immediately from our office
 
Sincerely,
 
Yours In Service.


Agent Shawn Henry,
Assistant Director, FBI Cyber Division.
Email : fbiofffice1@qatar.io


CONFIDENTIAL NOTICE:This E-MAIL may contain information that is privileged, confidential, and exempt from disclosure under applicable law. If you are not the intended recipient, any dissemination, distribution, copying, or use of this document is strictly prohibited. If you have received this communication in error, please notify us by email at the E-MAlL listed above to arrange for the destruction or return of the original document to us. Thank you
© 2011 Federal Bureau Of Investigations All Rights Reserved


As you can imagine, I was somewhat amused. I get rather a lot of spam - currently about 60 messages per day that I am aware of, although it has been as low as 20 and as high as 120 over the last few years - and it's quite rare that something like this gets through the spam filters that my email provider has in place, so I thought I'd share it with you. Be amused, but please don't get taken in by this sort of thing when it lands in your inbox!

That's it. Have a good holiday weekend.
TTFN

Found Food

I have published quite a few recipes here on my blog over the last few years, and I hope that all my readers have tried at least some of the...