Jane's Technical Stuff

Wednesday, August 20, 2008

SQL Snap


At last September's BarCamp Brighton I saw the CSS Specificity Snap cards and this gave me an idea. So, armed with a concept of showing the different ways of producing the same output using the SQL Server 2005 flavour of SQL I started generating SQL statements. Alex was kind enough to make them pretty, and last week I ran a Madgex ILP session to play.

There are 26 cards in my pack, each card has a SQL statement, and a letter. The letter is used for the crib sheet to allow me to easily spot the matches.



I prepared by producing two piles of 13 cards, with a match in both piles. I then put one pile in the middle of the table face down, and dealt out the rest of the cards to the 5 people who were playing. I placed the 3 spare cards on the table face up so that we could all see them and proceeded to turn them over one by one. When a card was turned over everyone looked at the SQL on the overturned card, and checked their cards and the spare cards to see if there was a match. If a match wasn't spotted then I explained what the SQL was doing, and at only one point did I have to say which letter the matching card would have. During this I had quite a few comments about functions people didn't recognise - especially COALESCE and NULLIF. I also explained some of the performance, and functional differences between the SQL statements when they were seen - so for instance I explained the differences between DELETE and TRUNCATE but I still I have these as a match because they can provide the same result and provided an interesting talking point.

The second game we played was a memory game where I placed all of the cards on the table face down and everyone turned over 2 cards at a time. If the SQL on the cards resulted in the same output, then it was a match and they took the cards away. If they didn't then the cards got turned back over and the next person had a go.

All in all this session took about 30 minutes, and resulted in quite a lot of noise and laughter (so much so that someone came and closed the door of the room we were in).

The following table contains the Letter Code, SQL statement, and the matching Letter code. They are ordered so that the matches are grouped together.
CodeSQLMatch
VSELECT CAST(GETDATE() AS VARCHAR(11))G
GSELECT CONVERT (VARCHAR(11), GETDATE())V
ZSELECT name FROM sysobjects WHERE xtype = 'P'Q
QSELECT name FROM sys.proceduresZ
DSELECT sysobjects.name, syscolumns.name FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE sysobjects.xtype = 'u'A
ASELECT sysobjects.name, syscolumns.name FROM sysobjects, syscolumns WHERE sysobjects.id = syscolumns.id AND sysobjects.xtype = 'u'D
USELECT getdate()O
OEXEC('SELECT getdate()')U
JDELETE FROM TestT
TTRUNCATE TABLE TestJ
YSELECT ISNULL(NULL,1)W
WSELECT COALESCE(NULL,1)Y
PSELECT [name], xtype FROM sysobjects ORDER BY xtypeM
MSELECT [name], xtype FROM sysobjects ORDER BY 2P
HSELECT NULLIF(1,1)R
RSELECT CASE 1 WHEN 1 THEN NULL ELSE 1 ENDH
ISELECT [name], xtype FROM sysobjects ORDER BY 1N
NSELECT [name], xtype FROM sysobjects ORDER BY [name]I
XSELECT CAST(GETDATE() AS VARCHAR(20))F
FSELECT CONVERT (VARCHAR(20), GETDATE())X
SSELECT name FROM syscolumns WHERE id = ( SELECT id FROM sysobjects WHERE xtype = 'u' AND name = 'Jobs' )K
KWITH objects (id) AS
( SELECT id FROM sysobjects WHERE xtype = 'u' AND name = 'Jobs' ) SELECT name FROM syscolumns INNER JOIN objects ON syscolumns.id = objects.id
S
LINSERT INTO Job (JobID, PrimaryJobTypeID) VALUES (1,1)B
BINSERT INTO Job (JobID, PrimaryJobTypeID) SELECT 1,1L
CDECLARE @sMessage AS VARCHAR(20) SET @sMessage = 'Hello'E
EDECLARE @sMessage AS VARCHAR(20) SELECT @sMessage = 'Hello'C

Labels: , ,

// posted by Jane @ 8:17 PM   save to del.icio.us

Comments:

Brighton Bloggers   This page is powered by Blogger. Isn't yours?   rss Sussex Digital - focusing on the Sussex digital community