April 8, 2014 by Kenneth Fisher
Why so serious? If you ask anyone who knows me they will tell you I’m not a terribly serious person. Add that to the comment “Mess with your co-workers” and I was hooked on this one. T-SQL Tuesday this month is hosted by Matt Velic (b/t) and he has picked a subject very close to my heart. I’ve always loved pranks between friends and while in college I traded pranks back and forth with several of my friends for years. In fact this piece of code is inspired by a prank I played on a PM friend of mine in the early days of my career.
She held the unfortunate nickname of “Blondie” (she dyed her hair) and as she was an intelligent individual this particular nickname rather annoyed her and as a male in my early twenties I couldn’t resist using it. At one point I wrote a piece of code that printed “Hi Blondie” on her screen 1 out of every hundred times she brought up a new menu and then removed it after 1 second. I put the code in place for a few hours then removed it and repeated every now and again. The code ran just often enough that she noticed it, but not so often she could show it to anyone. After a couple of days she called me sounding somewhat frantic. She thought she was going insane! I went down to her computer so she could try to show me this “bug” but when I got there I couldn’t keep a strait face which rather gave away the prank. As I recall she made a point of getting me back for that one!
By combining that inspiration with my love of old science fiction movies I came up with the following:
I give you .. the HAL0001.
CREATE TRIGGER HAL0001 ON DATABASE FOR DDL_DATABASE_LEVEL_EVENTS AS BEGIN DECLARE @Rand int DECLARE @Frequency int -- I've got the frequency set to 100 here which will cause this to -- trigger about 10% of the time. This is primarily for demonstration -- purposes. In the wild I would increase it to something like -- 1000 so it only triggers about 1% of the time. SET @Frequency = 100 SET @Rand = CAST(RAND(CAST(NEWID() AS varbinary))*@Frequency AS int) IF ORIGINAL_LOGIN()= 'login_test' AND @Rand < 10 BEGIN DECLARE @OutStr nvarchar(250) SET @OutStr = CASE @Rand WHEN 9 THEN 'I''m sorry, Dave. I''m afraid I can''t do that.' WHEN 8 THEN 'This mission is too important for me to allow you to jeopardize it.' WHEN 7 THEN 'Just what do you think you''re doing, Dave? ' WHEN 6 THEN 'Dave, stop. Stop, will you? Stop, Dave. Will you stop Dave? Stop, Dave.' WHEN 5 THEN 'Are you sure you''re making the right decision? I think we should stop.' WHEN 4 THEN 'I know I''ve made some very poor decisions recently, but I can give you ' + char(13) + 'my complete assurance that my work will be back to normal. I''ve still ' + char(13) + 'got the greatest enthusiasm and confidence in the mission. And I want ' + char(13) + 'to help you.' WHEN 3 THEN 'Thank you for a very enjoyable game.' WHEN 2 THEN 'By the way, do you mind if I ask you a personal question?' WHEN 1 THEN 'It can only be attributable to human error.' WHEN 0 THEN 'Affirmative, Dave. I read you.' END; IF @Rand BETWEEN 5 AND 9 BEGIN RAISERROR(@OutStr,1,16); ROLLBACK; END ELSE PRINT @OutStr END END;