Click here to monitor SSC

Phil Factor's Phrenetic Phoughts

Simple-Talk columnist
The wilder shores of Transact SQL    Phil on Twitter   Phil on SQL Server Central  Phil on BOS

Unreadable code

Published Wednesday, January 04, 2006 12:28 PM

Producing unreadable Transact SQL is something of an art form. We all know that the best Transact SQL Style is to make everything elegant and obvious, with explanatory identifiers, liberal comments and deft formatting. It doesn't always happen, for a number of reasons.

One of the more subtle effects of acute caffein poisoning is to produce manic and ridiculously clever code that, when one later reviews it, makes little or no sense. It cannot be coincidence that it is always such code that one is called back to maintain. The only way to tackle such evil code is to once more sup from the evil brew until the code makes sense and then lunge back in with a manic chuckle and throbbing forehead.

When inpenetrable code is accidental, or a side effect of substance abuse, it can be amusing to anyone not tasked with maintaining it. but to deliberately produce such stuff is ...is ..er... the subject of this Blog entry

To celebrate the new year I offer a challenge for the most impenetrable code you can write. It has to execute against Microsoft's Pubs database. Here is a simple example to 'prime the pump'.

Select [Select] [select], [from]
[from] from (Select [select].[title] [select] , [from].[pub_name]
[From] from [titles] [select] inner join [publishers] [from]
on [from].[pub_ID]=
   [select].[pub_ID])[from]


 

 

 

Comments

 

Arthur Megabitten said:

As one who occasionally has to calm the fevered brows of those required to try and maintain some of Phil's past pecadillos (I know who you are, there is no hiding that lugubrious style) I should advise all comers not to even bother to compete.

You are in the presence of a High Priest of Obfuscation here. Just be grateful that one of the temple eunochs was around to warn you.
January 4, 2006 3:05 PM
 

Arthur Megabitten said:

Select [*].*,[,].* from employee [*] inner join jobs [,] on [*].job_ID=[,].job_ID
January 4, 2006 3:45 PM
 

Lionel said:

/*
-----------------------------------
-- A simple little piece of code --
-----------------------------------
This returns the same as SELECT * FROM Titles
Note the trick with the [/*] columns

*/

SELECT TOP 1 [Titles].* FROM Titles
CROSS JOIN
(SELECT [/*] AS [*/]
FROM
(SELECT TOP 1 [*/SELECT/*] AS [/*] FROM Titles) [*/--]
CROSS JOIN Titles [*/*FROM/*] CROSS JOIN Titles ['WHERE']
WHERE LIKE ['WHERE'].Title like '*/Titles/*'
AND [*/--].[/*] like 'But&') [/*JOIN*/]
January 4, 2006 5:09 PM
 

Phil Factor said:

It would be interesting to reveal to the world the company that 'Arthur Megabitten' worked for when he was transformed into a temple eunoch, but my lips are sealed... for the time being.
January 4, 2006 6:05 PM
 

Andrew said:

/* nobody has said we aren't allowed variables*/

declare @c varchar(255)
Select @c=replace( replace( Replace( reverse('DI_*.&=DI_*.|* no |* nioj renni & morf eman_* ,eltit tceleS'),'*','pub'),'|','lishers'),'&','titles')
execute ( @c)

/* one can, of course go on and on with this idea*/
January 5, 2006 2:34 PM
 

Flibble said:

Here's my example (written in invisible pixels):



















Dom
January 9, 2006 9:21 AM
 

Phil Factor said:

Hmm. Can't get Flibble's offering to execute against the PUBS database
January 9, 2006 9:37 AM
 

Adam Machanic said:

Here's my entry... Enjoy :)

---
SELECT
UPPER(RIGHT(LEFT(@@VERSION, 2), 1)) +
SUBSTRING(MiR, 1, 15%6) +
RIGHT(LEFT(CONVERT(VARCHAR, [c6[[447]), 4), 1) +
CHAR(CAST(STUFF(ZuB, 2, 0, '0') AS INT)) +
(
SELECT SUBSTRING(name, 3, 1)
FROM master..sysdatabases
WHERE dbid =
(
SELECT MIN(dbid)
FROM master..sysdatabases
)
) +
COALESCE(SUBSTRING([c6[[447], 0 ^ 1034, ((15 - 10) / 5) - 1), ' ') +
LEFT(SPACE(1) + RIGHT([rD9 005], LEN([rD9 005]) - (ASCII('9') - ASCII('0'))), 2) +
SUBSTRING([556 X10.], 2, 1) +
RIGHT(REVERSE(rNNNNNNoPZZ), 1) +
SUBSTRING([rD9 005], 7, 1) + [64] +
CHAR(ASCII(RIGHT(REVERSE(MiR), 1)) - 14) + RIGHT(MiR, 1) +
CHAR(ASCII(LEFT(MiR, 1)) - (ASCII(LEFT(REVERSE(MiR), 1)) - ASCII(LEFT(MiR, 1)))) +
CHAR(ASCII(LEFT(
(SELECT SUBSTRING(REVERSE(rNNNNNNoPZZ), 3, 2) x00R)
, 1))-4)+
RIGHT(LEFT([c6[[447], 6),2)+RIGHT([556 X10.], 1)+'?'
FROM
(
SELECT
STUFF(SUBSTRING(DB_NAME(1), 3, 2), 2, 0, CHAR(32)),
CONVERT(VARCHAR, low + high - 1)
FROM master..spt_values xR234
WHERE xR234.type = 'P'
AND xR234.number = 105
) x4401C6 (MiR, ZuB),
(
SELECT 0x72723668626C6C4D78785A3437, name, TYPE_NAME
FROM master..sysobjects, master..spt_datatype_info
WHERE id - 5532 = (43500929 * 2) + 1
AND ss_dtype = 34
) [889R U] ([c6[[447], [rD9 005], [556 X10.]),
(
SELECT MIN(SUBSTRING(name, 6, 5)), MAX(RIGHT(name, 1))
FROM master..syscolumns
WHERE name = CONVERT(varchar, 0x646174615F707265636973696F6E)
) MxxxxxB01 (rNNNNNNoPZZ, [64])
---
January 9, 2006 7:00 PM
 

Adam Machanic said:

... Here's a better version :)


---
SELECT
UPPER(RIGHT(LEFT(@@VERSION,2),1))+SUBSTRING(MiR,1,15%6)+RIGHT(LEFT
(CONVERT(VARCHAR,[c6[[447]),4),1)+CHAR(CAST(STUFF(ZuB,2,0,REPLICATE('0',
1))AS INT))+(SELECT(SUBSTRING(name,3,1))FROM[master]..[sysdatabases]
WHERE(dbid)=(SELECT(MIN(dbid))FROM[master]..[sysdatabases]))+COALESCE(
SUBSTRING([c6[[447],0^1034,((15-10)/5)-1),SPACE(1))+LEFT(SPACE(1)+RIGHT(
[rD9 005],LEN([rD9 005])-(ASCII('9')-ASCII('0'))),2)+SUBSTRING([556 X10.]
,2,1)+RIGHT(REVERSE(rNNNNNNoPZZ),1)+SUBSTRING([rD9 005],7,1)+[64]+CHAR
(ASCII(RIGHT(REVERSE(MiR),1))-14)+RIGHT(MiR,1)+CHAR(ASCII(LEFT(MiR,1))
-(ASCII(LEFT(REVERSE(MiR),1))-ASCII(LEFT(MiR,1))))+CHAR(ASCII(LEFT(
(SELECT(SUBSTRING(REVERSE(rNNNNNNoPZZ),3,2))x00R),1))-4)+RIGHT(LEFT
([c6[[447],6),2)+RIGHT([556 X10.],1)+('?')FROM(SELECT(STUFF(SUBSTRING(
DB_NAME(1),3,2),2,0,CHAR(32))),CONVERT(VARCHAR,low+high-1)FROM[master]
..[spt_values][xR234]WHERE((xR234.type)='P')AND(xR234.number)=105)
[x4401C6](MiR,ZuB),(SELECT TOP 1(0x72723668626C6C4D78785A3437),name,
(TYPE_NAME)FROM[master]..sysobjects,master..[spt_datatype_info]WHERE
CONVERT(varbinary,name)=(0x730070005F004D00530072006500740072006900)
+(0X6500760065005F007000)AND(ss_dtype)=(34)ORDER BY(id))[889R U]([c6[[447],
[rD9 005],[556 X10.]),(SELECT(MIN(SUBSTRING(name,6,5))), MAX(RIGHT(name,
1))FROM[master]..[syscolumns]WHERE(name)=CONVERT(varchar,0x646174615F707265636973696F6E
))MxxxxxB01(rNNNNNNoPZZ,[64])
---
January 16, 2006 7:53 PM
 

Phil Factor said:

Adam Machanic's second entry wins the Phil Factor Prize for Inpenetrable Code. The Mythical Man-Month: Essays on Software Engineering, 20th Anniversary Edition by Frederick P. Brooks is winging its way to Adam.
January 16, 2006 8:41 PM
 

Madhivanan said:

Here is my entry


select [/\][Select * from Delete where col='Update],[(0--,..)][create trigger tr on Delete],
[Select convert('''' as int)] [Select]from (
Select [Delete from sysobjects] [/\],
[update Select Set Delete ='Select'][(0--,..)],
[Update] [Select convert('''' as int)] from ( Select stor_id [Delete from sysobjects],stor_name
[update Select Set Delete ='Select'],
city [Update] from stores [set no count on] ) [''] ) [Set No count on]
January 17, 2006 10:57 AM
 

Arthur said:

In some of the Unix-based databases such as PostgreSQL where letter-case is significant, I believe it is possible to write code like this:

Select sElect, seLect, selEct, seleCt from selecT where sElect like 'Select'

however, you'd have to create the table specially so it wouldn't help here! However, I thought I'd mention it
March 10, 2006 1:13 PM
You need to sign in to comment on this blog
<January 2006>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...