Looping through records in SQL Server Stored Procedure

Someone who’s pretty good programming on the front end, but not so crash hot on the backend, eg SQL Server, asked me the other day about looping through recordsets in a SQL Server Stored Procedure.

It is of course possible to use a cursor and FETCH but most SQL programmers frown on cursors in stored procedures. Cursors involve grabbing a bunch of records and locking them, there can be a substantial performance hit. They should really only be used as a last resort, for example, where you don’t have a good enough unique key value on your rows (although there are ways around that one usually as well).

The easiest way I know to look through records is to employ a temporary table, here’s a simple example:

 

-- Create a temporary table to hold the records to be updated
DECLARE @uniqueId int
DECLARE @TEMP TABLE (uniqueId int)
-- Insert into the temporary table a list of the records to be updated
INSERT INTO @TEMP (uniqueId)
SELECT uniqueId FROM myTable
-- Start looping through the records
WHILE EXISTS (SELECT * FROM @TEMP)
BEGIN
-- Grab the first record out
SELECT Top 1 @uniqueId = uniqueId FROM @TEMP
PRINT 'Working on @uniqueId = ' + CAST(@uniqueId as varchar(100))
-- Perform some update on the record
UPDATE myTable SET myField = 'something or other' WHERE uniqueId = @uniqueId
-- Drop the record so we can move onto the next one
DELETE FROM @TEMP WHERE uniqueId = @uniqueId
END

 

 

 

 

 

How to Set Consultancy Fees

Came across this fantastic nugget the other day:

Some
consultants set their rates by the project. They estimate the number of
hours they expect to spend on a project, then multiply by their hourly
rate.

However,
some consultants set their project fees using the value the client
derives from the consultant’s advice. There’s an old joke about
physicist Niels Bohr that illustrate this principle.

A
company’s machine breaks down. The company’s owner, an old school chum
of Niels Bohr, calls in the physicist for help in fixing it.

Bohr examines the machine. He draws an X on the side and says, "Hit it right here with a hammer."

The
company’s mechanic hits the machine with a hammer. It springs into
action. The company’s owner thanks Niels Bohr profusely and sends him
on his way.

A few days later, the owner receives an invoice from Bohr for $10,000. Shocked, the owner phones Bohr!

"Niels! What’s this $10,000 invoice? You were only here for 10 minutes! Send me a detailed invoice."

Bohr agrees to send the invoice. A few days later, the company’s owner opens a new invoice.

INVOICE
Drawing X on the side of your machine                             $       1
Knowing where to put the X                                             $ 9,999
———————————————————————————————–
Total                                                                             $10,000

HP All in One Printer and Snow Leopard

Went to scan a document yesterday on the HP Photosmart 3310, but my Mac couldn't see the scanner – the pop up scan window kept saying scanner not found.

Played with it for ages, using the HP Device Manager thingy, but apparently the scanner was gone. But could print fine.

Finally found this document on the HP site. What do you know. The Snow Leopard upgrade breaks the HP scanner system.

But not to worry – because now it's all built into Mac, you can scan direct from Preview (very handy, straight to PDF which was what I wanted in the first place); Image Capture; or just from the Print and Fax settings.

You'll need to delete the printer from System Preferences > Print and Fax, and add it again, but that's the work of a moment.

Despite the initial frustration, actually very happy. But damn it's annoying when something simply disappears after an update and you have to go hunting for a solution. I did check for updates on the HP software and it said it was the latest version. What would be nice was if HP pushed an update that alerted you to these changes.

Other projects will pay for hare-brained scheme – NOT!!!!!

Just been reading Kenneth Davidson “Other projects will pay for Rudd’s hare-brained scheme” in The Age today. I think he is just plain wrong in his assertion that “the high-speed broadband network should be built incrementally”. Using his arguments the gas and electricity companies wouldn’t install pipes and wires to my house until I discovered I needed to have a shower or turn on a light.

He misses the point entirely. Innovation over the centuries has been driven by bands of individuals coming across a service or infrastructure and creating new and exciting purposes and products.

Staying in the technology sector for a moment, both the Apple iPhone Apps Store and Facebook Applications systems were infrastructure ideas created by the companies without a complete vision of what would result. But they were visionary enough to comprehend that by constructing a framework that enabled developers across a broad range of competencies and resource availability to create new software, they could stimulate a world of innovation. And that’s exactly what has happened. Both companies now have available tens of thousands of services and applications produced by third parties. Some of the programs are terrible. Others are striking in their innovation. All that was required was a faith in the inventiveness of their customers.

We started our internet company in our lounge room in 2000, back before broadband was even available to us. We sticky taped solutions together at almost no cost using what was available. Six years later we sold the company for several million dollars.

Our innovation today still emanates from our loungeroom – we have web servers in the family room cupboard. We constantly play with the available technologies to investigate how they can be alternatively utilised or reorganised to produce new products and services. Under Davidson’s regime we would not have access to the new infrastructure – not being a big company, or hospital or university.

I notice Davidson has left schools off his list of those privileged organizations who should receive preferential treatment. My eleven year old daughter, who runs online forums, writes on her blog and is constantly searching for new ways to use her internet connection and computer will be very disappointed. He’s ignoring one of the most innovative groups in our country – the kids who, by the time the NBN is finished in eight or nine years, will be moving into the workforce, starting businesses, and creating community and economic value.

History also shows us that major projects, particularly technology based,  rolled out incrementally are rarely completed. Progress slows, attention and resource is diverted, specifications and impetus and politics divert from the path.

Kevin Rudd’s vision on this is bold to be sure. Yes there are massive challenges. But the technology is proven. And put that technology in the hands of individuals and not restrict it to the privileged few, and the aggregate can only benefit Australia and the rest of the world.

Niche Content Millionaire

090406 Book Cover 200px

Just putting the final touches to our new eBook, Niche Content Millionaire and sending drafts off to various friends for review.

Niche Content Millionaire will be released this month. But you can make sure you secure your copy by registering now.

It’s
a cool experience when you’re sitting in a lawyer’s office high up in a
down town office block and someone casually hands you a check for a
couple of million dollars. It’s even more cool strolling into the
nearest bank branch and depositing the check – then rushing out to the
ATM to check your account balance.

We’ve been involved
with niche online content as a business since 2000. In that time we’ve
launched commercially successful web sites in Australia, the USA and
the UK. Our sites have attracted tens of thousands of subscribers, and
many times that number of casual visitors. In late 2006 we sold the
largest of our businesses for more than $US1 million.

Our
book is not designed to be a text book, nor a biography, rather it’s a
distillation of our story, married with advice and ideas that we
believe assisted us along our journey to success. We learnt our lessons
the hard way. We hope that you will read the book, learn and then be in
a position to apply those lessons to your own online, or offline,
business venture.

http://www.NicheContentMillionaire.com

ASP Coding Conventions

A client has asked that I compile some information about ASP coding conventions and standards to pass to their coding team to ensure maintenance of standards.

I've compiled this list (PDF)
. Maybe a bit filtered to suit the specific project, but interested in feedback.

SEO Secrets

People who know me, or who browse my blog and various writings, will know I'm not one for hawking products. But finished reading an eBook by fellow Australian Glenn Murray called 'SEO Secrets' and am making an exception. People who know me may have also heard me on my SEO (Search Engine Optimisation) soapbox.

I tend to lean towards the notion that paid SEO is the dark arts. I've worked with a bunch of clients who have paid companies for SEO work, and none of it has impressed me. I had one client last year forking out $400 a month, and the firm had basically broken every rule in the Google book – hidden links on the home page (reams of them); white text links; using a flash page to hide SEO text – you name it. On my advice they dropped the SEO firm, and we built a whole new site, using tried and true conventions, and hey presto, they rank as well or better for their keywords. And save $400 a month.

I fell over Glenn's book when looking for something completely unrelated, I was hunting around for good examples of an eBook being sold online in preparation for Fiona and I releasing our book (more on this in a week or two I hope). I liked his site, I liked he was Australian, I liked the fact that he really must be a copywriter because his sales pitch is the complete antithesis of the hyped up high pressure rubbish you so often see by the con merchants flogging their tired old paid information booklets.

So I did what I almost never do, and bought the book. And am glad I did. The majority of the content is not a major revelation to me, because I've been building sites for a long time, and generally feel I'm across the big issues with regard to coding and optimising content on a site. We kinda did this all before at our previous www.artshub.com.au business – with literally hundreds of thousands of pieces of content on the site network it became second nature.

But Glenn's book is a clear, well written, concise confirmation of my prior knowledge, plus throws in a few nuggets of 'oh of course' information that I know will prove useful in the future. And all nicely organised in a format I can easily dip into as required.

If unlike me you are not across the SEO issues, but you have a web site, and you want to know what all the SEO fuss is, then I reckon this is a good purchase.

Glenn concludes his book with:

"Search Engine Optimization is not a black art; it’s a science. There are defined rules and proven methodologies."

OK, I'm going to hold to my assertion that it's still the dark arts. There are so many variations, permutations and circumstances that, when teamed with the fact you are dealing with unknown Google algorithms, there is no guarantee. But Glenn has done a good job of laying the known facts on the table, and offering a clear path through the maze.

You can buy Glenn's book from his web site by clicking this link. And yes, I'll be upfront and say I've signed up to his Affiliate Program and yes I get a commission.

It’s time to live on your wits

I woke up this morning and realised that the Black Saturday bushfire tragedy marked more than just a terrible loss of life and property. So I sent off a twit , but now realise there is more to say.

In Australia there is a saying ‘she’ll be right mate’. It’s a kind of colloquial way of expressing a confidence in the future. If a friend is feeling down, we’ll console them with a ‘she’ll be right’ assurance, and buy them another beer.

Despite a year of ever-increasing doom and gloom news about the economy, the people on the street seemed not to be taking it personally. Whilst others were losing their jobs, unless you had a direct connection, the idea of recession seemed remote. We also forget how many people don’t even read a newspaper or watch the news, we employ several Generation Ys in our coffee shop, and it’s remarkable how little they consume of traditional media. Of course they are all Facebookers, or MySpacers, which is fabulous and lovely, but not a great way of finding out the world is descending into chaos brought on by a bunch of greedy financiers and an incompetent government regulatory framework.

On Black Saturday more than 200 Victorian’s lost their lives in bushfires. 7,000 or more were left homeless. And this wasn’t thousands of miles away, it’s happened in our backyard, only an hour or two’s drive from Melbourne. Over the past few weeks everyone has now realised they have a connection to the disaster, some incredibly painful links – they’ve lost a friend or relative; others more tenuous – we recently met a woman whose sister was married in front of a blazing reception centre, they took the wedding photos, then ran for their lives. Some prominent people died. Brian Naylor, the news anchor for decades on television was killed along with his wife. Several entertainers and other notable people also died.

My realisation this morning is that in the past few weeks ‘she’ll be right’ isn’t cutting the mustard. The world is in chaos. It’s finally hit home to individuals that they are going to be affected. And they are withdrawing, hibernating til the storm passes.

We had one of our best trading days ever at our coffeeshop on Black Saturday, despite the incredible heat. We closed at 4pm, so it was too early for any of the terrible news to filter through. All we knew was that it was a searingly hot day, and that some bushfires had been started. But since that day takings are down 30%, without exception, day after day. Many of our regulars are not showing their faces, we’re being kept open by the backpackers coming in for the free Wi Fi.

People have woken up from their comfortable, consumer, credit driven slumber.

It’s why we’re taking radical action in our lives, and starting to move assets into cash. You can’t mess around at the edges on this stuff, to survive you have to change your context, change your interpretation of the world.

We had several business projects on the board this year. We’ve completely changed our attitudes to them, we’re bringing forward our internet, IP based projects, that require little up front investment; and putting on hold projects that require significant investment in cash or assets. We’re getting rid of every single piece of debt within the next couple of months, right down to the credit cards. We’re off loading any bits and pieces of shareholdings, left-over company assets.

It’s time to live on your wits. To dig into your personal intellectual assets, to build a safety net of non-devaluing assets, like cash and personal ability, and protect yourself and your family.

AAPT is a rip off

One of our shops has a phone line with AAPT – just history, that’s the company the premises were connected with when we took over. Been paying $30 a month line rental. Got letter in mail today from AAPT saying our old plan no longer available, and we’re being moved onto a plan called AAPT At Work – NTC. The small print in the attached brochure says line rental is $39.95 a month. That’s a 30% increase!

So I check the APPT web site. There are only 2 business plans listed, the one mentioned above and a second called AAPT At Work. It’s $29.95 a month, with a 12 month contract. I rang AAPT, on hold for 15 minutes, the guy tells me I can’t have the $29.95 a month for a reason I don’t really understand.

He confirmed the $39.95 a month is the only plan available to us.

How do these companies get away with this? Do they presume that we won’t read the small print, or be too apathetic to take action?

How can a company increase its rates by more than 30%? I’m sure I’ve read somewhere AAPT is in financial difficulty. They deserve to fail.

Taking Parents for Granted

I’m starting to get pretty cheesed off by the organizers of our kids
actvities who continually take parents for granted.

Largest daughter has her end of year cheerleading ‘display’ (read:
showing where all those fees went this year).

Programmed for 5pm-7pm. Give up work early, organize with better half
she’ll pick up son from his after school drama class, and she’s happy
to concurrently with that happy also to mind the 2 year old and figure
out dinner.

Get to Wesley. Told daughter will be ‘on’ at maybe 6.30pm if we’re
lucky, later if they are behind.

So now have to sit around for at least 1.5 hours.

Spend 10mins doing the few emails that can be dealt with from my
phone. Kick myself for not bringing the specs and plans for our new
cafe for review. So forces to watch dozens of other peoples’ children
doing poorly organized gym routines.

How much better if they broke it into 4 half hour blocks and scheduled
all the various groups into the blocks so we could plan when we needed
to be there.

But biggest daughter did look pretty cool when they finally hit the
floor. And it was 6.15pm so quite remarkably they were ahead of time.

I just really get annoyed when there’s a presumption parents will sit
around endlessly waiting for their poppets’ moments of fame.