Muujware : Journals : Matthew's Journal : June 30, 2004
About Matthew
Name: Matthew W. Jackson
Email:
Position: PC / Programming / Game / Music Composition Geek

View Matthew's Personal Profile
T-SQL Quine
Posted on June 30, 2004 at 1:33 AM CST/CDT
  [Previous]  [Next]
I have been trying for a while to write a quine query in SQL. For the geeks who care what I'm talking about but don't know what a quine is, read about it here.

Anyway, quines are usually written by assigning part of the code to a string variable and writing it more than once in the program's output: once for the actual code and once for the literal assigning the string variable.

Now, there are probably ways to cheat and write an easy quine in SQL Server that uses some internal command to display the contents of a view or stored procedure.

And I know that a quine could be written as a stored procedure, since stored procedures can assign local variables and execute complex logic.

However, I wanted to write the quine using a single SELECT query. Recently, I stumbled upon a quine written in Perl which uses string matching and replacement to create a single-expression quine. I was inspired, and I realized that this form of quine could be adapted into any SQL language with a string-replacement function.

Anyway, here's what I came up with:

SELECT REPLACE(REPLACE('SELECT REPLACE(REPLACE("$",CHAR(34),CHAR(39)),CHAR(36),"$") AS Quine',CHAR(34),CHAR(39)),CHAR(36),'SELECT REPLACE(REPLACE("$",CHAR(34),CHAR(39)),CHAR(36),"$") AS Quine') AS Quine

When executing this against any SQL Server database, the following rowset is returned:

Quine
SELECT REPLACE(REPLACE('SELECT REPLACE(REPLACE("$",CHAR(34),CHAR(39)),CHAR(36),"$") AS Quine',CHAR(34),CHAR(39)),CHAR(36),'SELECT REPLACE(REPLACE("$",CHAR(34),CHAR(39)),CHAR(36),"$") AS Quine') AS Quine

Try it for yourself if you have a copy of SQL Server or MSDE running somewhere. It shouldn't be hard to port this to PL/SQL (Oracle), but it's getting late.

Now to get to work on a non-trivial regular expression that only matches itself. Wow, is that even possible!? I know that you could use "trivial" to match the string trivial, but what about more complicated expressions? I don't think it is possible, but can it be proven? Maybe just a regex that matches itself and other strings. Once again, it would have to be less trivial than something like ".*" (which would of course match ANY string). On second thought, that's a pretty stupid idea. Forget I mentioned it.

Beware the Muuj
Comments on this post are closed.
Comment by [spam deleted]
Posted on September 26, 2006 at 8:30 PM EST/EDT
#
[spam deleted]
Comment by Mike Barton
Posted on September 26, 2006 at 11:42 PM EST/EDT
#
I think they're trying to write a bbcode quine!
Comment by Matthew
Posted on September 26, 2006 at 11:46 PM EST/EDT
#
I think they're trying to get their URLs banned.

Should I just ban "http://%.info/%"?
Comment by Mike Barton
Posted on September 27, 2006 at 12:41 AM EST/EDT
#
.biz too. and .li. Nothing good ever came out of Liechtenstein.
Comment by Michael
Posted on September 27, 2006 at 2:17 AM EST/EDT
#
I think the .info, .biz, and .cc just might be the second, lower tier of the Internet we've all been hearing about...
Comment by Matthew
Posted on September 28, 2006 at 5:05 PM EST/EDT
#
Maybe I'll just block any attempts to use bbcode URL tags.
Comment by Michael
Posted on October 2, 2006 at 12:32 AM EST/EDT
#
SANTA VACA!
Site
News
News Archives
Files
Polls
Journals
Profiles
Statistics
Recent Comments
RSS Feeds
User Options
Name:
Password:
  
Copyright © 2001-2014 Muujware