T-SQL coding challenge
This is an oldie, but a goodie. It's an article by Ken Thompson about, among other things, writing self-replicating programs. Thompson is one of my heroes and someone who's had a profound impact on the industry. His list of accomplishments is too long to get into here, but let me summarize it by saying that he was one of the original authors of Unix, invented the B programming language (the precursor to C), UTF-8 encoding, regular expressions, and some of the earliest sophisticated chess programs. It would be hard to overstate the influence he has had on computer science and computer technology.
Back to the article. It touches on the educational value of writing a self-replicating program. I've learned something from this exercise every time I've done it. So, today's challenge is to do this in T-SQL -- to write a T-SQL script that replicates itself. Please read Thompson's article before beginning. Then post back here with your solutions. I know a lot of you like to email me directly, and that's fine, but if you'll post your answers here, everyone can benefit from them. I'll commit to perusing through these and posting my comments about your solutions asap.
Comments
Anonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
Here we go (feel free to delete my prior comments):
create procedure GetMe
as
declare @text varchar(500);
select @text = text from sysobjects o
inner join syscomments c on c.id = o.id
where o.name = 'GetMe';
exec ('drop procedure GetMe');
exec(@text);
GOAnonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
I know :)
Working on that now... -much- harder, to say the least!Anonymous
October 25, 2005
I think this works but it's a little ugly. It's an interesting problem that I've never actually attempted before.
declare @string varchar(8000)
declare @quote char(1)
declare @asterisk char(1)
declare @s char(1)
declare @percent char(1)
set @quote = char(39)
set @asterisk = char(42)
set @s = char(115)
set @percent = char(37)
set @string = 'declare @string varchar(8000)
declare @quote char(1)
declare @asterisk char(1)
declare @s char(1)
declare @percent char(1)
set @quote = char(39)
set @asterisk = char(42)
set @s = char(115)
set @percent = char(37)
set @string = %s
set @string = replace(@string, @asterisk, @quote)
set @string = replace(@string, @quote + @percent + @s + @quote, @quote + @string + @quote)
set @string = replace(@string, @quote + @percent + @s + @quote, @asterisk + @percent + @s + @asterisk)
print @string
'
set @string = replace(@string, @asterisk, @quote)
set @string = replace(@string, @quote + @percent + @s + @quote, @quote + @string + @quote)
set @string = replace(@string, @quote + @percent + @s + @quote, @asterisk + @percent + @s + @asterisk)
print @stringAnonymous
October 25, 2005
Not nice but seems to work ok.....
declare @x varchar(1000)
declare @y varchar(20)
set @y = ''''
set @x = 'declare @x varchar(1000) declare @y varchar(20) set @y = set @x =
select substring (@x,1,57) + substring (@y,1,4) + substring (@y,1,4) +
substring (@y,1,4) + substring (@y,1,4) + substring (@x,57,9)
+ substring (@y,1,4)
+ substring (@x,1,1000)
+ substring (@y,1,4)
+ substring (@x,67,1000)
'
select substring (@x,1,57) + substring (@y,1,4) + substring (@y,1,4) +
substring (@y,1,4) + substring (@y,1,4) + substring (@x,57,9)
+ substring (@y,1,4)
+ substring (@x,1,1000)
+ substring (@y,1,4)
+ substring (@x,67,1000)Anonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
Paul Ibison: I think yours works better if you use PRINT rather than SELECT. If you use SELECT, I get a blank column header that's not in the original. Other than that, the only differences with the original are formatting, so I think this one satisfies the criteria. Nice job!Anonymous
October 25, 2005
David Jones: looks like you nailed it. Nice job!Anonymous
October 25, 2005
How about?
declare @x varchar(1000)
declare @y varchar(20)
set @y = ''''
set @x = 'declare @x varchar(1000) declare @y varchar(20) set @y = set @x =
select substring (@x,1,57) + substring (@y,1,4) + substring (@y,1,4) +
substring (@y,1,4) + substring (@y,1,4) + substring (@x,57,9)
+ substring (@y,1,4)
+ substring (@x,1,1000)
+ substring (@y,1,4)
+ substring (@x,67,1000)
'
dbcc inputbuffer (@@SpID)Anonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
Any clues?Anonymous
October 25, 2005
How many lines does this count as? (Hopefully it doesn't wrap. It's 5 lines in my editor.)
declare @ varchar(1000)
set @ = 'declare @ varchar(1000)
set @ = "declare @ varchar(1000)"
exec sp_executesql N"print substring(@,1,33)+char(39)+@+char(39) set @ = replace(@,char(34),char(39)) print substring(@,61,8000) ", N"@ varchar(1000)", @=@'
exec sp_executesql N'print substring(@,1,33)+char(39)+@+char(39) set @ = replace(@,char(34),char(39)) print substring(@,61,8000) ', N'@ varchar(1000)', @=@Anonymous
October 25, 2005
A shorter version:
declare @ char(444)
set @ = 'declare @ char(444)
set @ = "declare @ char(444)"
exec sp_executesql N"print left(@,29)+char(39)+@+char(39) print right(replace(@,0x22,0x27),113)",N"@ char(444)",@'<br>exec sp_executesql N'print left(@,29)+char(39)+@+char(39) print right(replace(@,0x22,0x27),113)',N'@ char(444)',@Anonymous
October 25, 2005
The comment has been removedAnonymous
October 25, 2005
Another route....
set nocount on
create table #m(E1 varchar(30), P1 int, V1 varchar(255))
insert into #m(E1,P1,V1)
exec('DBCC INPUTBUFFER (@@spid) WITH NO_INFOMSGS')
declare @EventInfo varchar(255)
select @EventInfo = V1 from #m
print @EventInfoAnonymous
October 25, 2005
Wow, is my solution ugly. It is the stored proc that drops and recreates itself:
create proc x
as
declare @sql1 varchar(200)
set @sql1 = 'drop proc x'
exec(@sql1)
declare @sql2 varchar(600)
set @sql2 = '
create proc x
as
declare @sql1 varchar(200)
set @sql1 = ''drop proc x''
exec(@sql1)
declare @sql2 varchar(600)
set @sql2 = xyz
set @sql2 = replace(@sql2, ''x''+''yz'', '''''''' + replace(@sql2, '''''''', '''''''''''') + '''''''')
exec(@sql2)
'
set @sql2 = replace(@sql2, 'x'+'yz', '''' + replace(@sql2, '''', '''''') + '''')
exec(@sql2)Anonymous
October 25, 2005
Improved version with a drop table...
set nocount on
create table #m(E1 varchar(30), P1 int, V1 varchar(255))
insert into #m(E1,P1,V1)
exec('DBCC INPUTBUFFER (@@spid) WITH NO_INFOMSGS')
declare @EventInfo varchar(255)
select @EventInfo = V1 from #m
drop table #m
print @EventInfoAnonymous
October 25, 2005
Cleaned up a bit:
----
create proc x
as
drop proc x
declare @sql2 varchar(600)
set @sql2 = '
create proc x
as
drop proc x
declare @sql2 varchar(600)
set @sql2 = xyz
set @sql2 = replace(@sql2, ''x''+''yz'', '''''''' + replace(@sql2, '''''''', '''''''''''') + '''''''')
exec(@sql2)
'
set @sql2 = replace(@sql2, 'x'+'yz', '''' + replace(@sql2, '''', '''''') + '''')
exec(@sql2)
go
----Anonymous
October 25, 2005
How about the following:
declare @s as varchar(10);
set @s = 'abc';
declare @sql as nvarchar(max);
set @sql =
(select text
from fn_get_sql(
(select sql_handle
from sys.sysprocesses
where spid = @@spid)));
/
-- uncomment to omit the replication code from output
set @sql =
left(@sql,
charindex(N'declare @sql as nvarchar(max);',
@sql) - 1);
/
print @sql;Anonymous
October 25, 2005
Itzik: Great to see you on here. IMO, using fn_get_sql is just another way of getting at your own source. It's a riff on the syscomments/sp_helptext approach. In this case, it happens to be pulling the text from server memory. Where did that text come from? Syscomments, of course :-)
Can you think of a solution like the others where the code actually produces itself as output without accessing a cached copy externally?Anonymous
October 25, 2005
You mean something like:
print replace(space(1)+char(39)+space(1)+char(39)+char(41),space(1),'print replace(space(1)+char(39)+space(1)+char(39)+char(41),space(1),')
Actually there's even a shorter one:
<start batch><end batch>
That is, a void batch. ;-)Anonymous
October 26, 2005
Well, Itzik's solution is shorter, but this was how I worked it out:
DECLARE @code varchar(500)
SET @code = '))93(rahc + edoc@ + )93(rahc ,)311(rahc ,)edoc@(ESREVER(ECALPER TNIRP
q = edoc@ TES
)005(rahcrav edoc@ ERALCED'
PRINT REPLACE(REVERSE(@code), char(113), char(39) + @code + char(39))Anonymous
October 26, 2005
Itzik: Yes, this is what I was talking about.
Justin: Nice effort. This works, too.Anonymous
October 26, 2005
Mine original solution was a bit longer than Itzik's, but I see that he could make his six bytes shorter by using 'char(0)' instead of 'space(1)':
print replace(char(0)+char(39)+char(0)+char(39)+char(41),char(0),'print replace(char(0)+char(39)+char(0)+char(39)+char(41),char(0),')Anonymous
October 27, 2005
Steve Kass came up with this method of making it even shorter:
print
replace(0x2027202729,0x20,'print
replace(0x2027202729,0x20,')
:)Anonymous
February 04, 2006
Today’s entry is another T-SQL puzzle. Steve Kass took the prize for the best solution to my last T-SQL...Anonymous
May 17, 2006
Nice site!
[url=http://xzimaxcr.com/lzgx/txgu.html]My homepage[/url] | [url=http://ybsibsnb.com/fmqe/rddo.html]Cool site[/url]Anonymous
May 17, 2006
Great work!
<a href="http://xzimaxcr.com/lzgx/txgu.html">My homepage</a> | <a href="http://hwbhrldc.com/ofek/nawp.html">Please visit</a>Anonymous
May 17, 2006
Well done!
http://xzimaxcr.com/lzgx/txgu.html | http://khojhezn.com/ygae/zdcm.htmlAnonymous
August 26, 2006
The comment has been removedAnonymous
August 26, 2006
Very many thanks for a good work. Nice and useful. Like it!Anonymous
August 27, 2006
Very informative post about <a href="http://home-owner.kogaryu.com/consolidation-debt-loan-unsecured.html"">http://home-owner.kogaryu.com/consolidation-debt-loan-unsecured.html" title="consolidation debt loan unsecured">consolidation debt loan unsecured</a> and [URL=http://home-owner.kogaryu.com/consolidation-debt-loan-unsecured.html]consolidation debt loan unsecured[/URL]Anonymous
August 28, 2006
Hi, nice site, good work! Thank you!,Hi, nice site, good work! Thank you!Anonymous
August 30, 2006
hi
<A HREF=http://d0gma.info/antivirus/map.html>antivirus software</A> <A HREF=http://d0gma.info/antivirus/>antivirus</A>good luckAnonymous
August 31, 2006
hi
<A HREF=http://info.best-pills.org/sitemap.html>best pills</A> <A HREF=http://info.best-pills.org>info</A>good luckAnonymous
August 31, 2006
hi
<A HREF=http://info.best-pills.org/sitemap.html>best pills</A> <A HREF=http://info.best-pills.org>info</A>good luckAnonymous
August 31, 2006
hi
<A HREF=http://info.best-pills.org/sitemap.html>best pills</A> <A HREF=http://info.best-pills.org>info</A>good luckAnonymous
September 03, 2006
Very informative post about <a href="http://nowinsurance.bravehost.com/annual-insurance-travel.html"">http://nowinsurance.bravehost.com/annual-insurance-travel.html" title="annual insurance travel">annual insurance travel</a> and [URL=http://nowinsurance.bravehost.com/annual-insurance-travel.html]annual insurance travel[/URL]Anonymous
September 04, 2006
Thank you for this great post about <a href="http://eteamz.active.com/yourbusiness/files/interest-only-mortgage-rate.html"">http://eteamz.active.com/yourbusiness/files/interest-only-mortgage-rate.html" title="interest only mortgage rate">interest only mortgage rate</a> and [URL=http://eteamz.active.com/yourbusiness/files/interest-only-mortgage-rate.html]interest only mortgage rate[/URL]Anonymous
September 05, 2006
(fgvof)
Try Look here!!! it's great
<a href= http://fitness-new-york-56767sf7.dunetribune.info >fitness new york</a><br>[url=http://fitness-new-york-56767sf7.dunetribune.info]fitness new york[/url]<br><a href= http://facility-fitness-56767sf7.dunetribune.info >facility fitness</a><br>[url=http://facility-fitness-56767sf7.dunetribune.info]facility fitness[/url]<br><a href= http://fitness-wear-56767sf7.dunetribune.info >fitness wear</a><br>[url=http://fitness-wear-56767sf7.dunetribune.info]fitness wear[/url]<br><a href= http://fitness-xsport-56767sf7.dunetribune.info >fitness xsport</a><br>[url=http://fitness-xsport-56767sf7.dunetribune.info]fitness xsport[/url]<br>
(typhv)Anonymous
September 07, 2006
(pvnrv)
Try Look here!!! it's great
<a href= http://fitness-new-york-56767sf7.dunetribune.info >fitness new york</a><br>[url=http://fitness-new-york-56767sf7.dunetribune.info]fitness new york[/url]<br><a href= http://facility-fitness-56767sf7.dunetribune.info >facility fitness</a><br>[url=http://facility-fitness-56767sf7.dunetribune.info]facility fitness[/url]<br><a href= http://fitness-wear-56767sf7.dunetribune.info >fitness wear</a><br>[url=http://fitness-wear-56767sf7.dunetribune.info]fitness wear[/url]<br><a href= http://fitness-xsport-56767sf7.dunetribune.info >fitness xsport</a><br>[url=http://fitness-xsport-56767sf7.dunetribune.info]fitness xsport[/url]<br>
(vuobi)Anonymous
September 07, 2006
(awmdj)
Try Look here!!! it's great
<a href= http://fitness-new-york-56767sf7.dunetribune.info >fitness new york</a><br>[url=http://fitness-new-york-56767sf7.dunetribune.info]fitness new york[/url]<br><a href= http://facility-fitness-56767sf7.dunetribune.info >facility fitness</a><br>[url=http://facility-fitness-56767sf7.dunetribune.info]facility fitness[/url]<br><a href= http://fitness-wear-56767sf7.dunetribune.info >fitness wear</a><br>[url=http://fitness-wear-56767sf7.dunetribune.info]fitness wear[/url]<br><a href= http://fitness-xsport-56767sf7.dunetribune.info >fitness xsport</a><br>[url=http://fitness-xsport-56767sf7.dunetribune.info]fitness xsport[/url]<br>
(tzhsh)Anonymous
September 07, 2006
Very informative post about <a href="http://debtofpay.250free.com/broadband-internet-provider.html"">http://debtofpay.250free.com/broadband-internet-provider.html" title="broadband internet provider">broadband internet provider</a> and [URL=http://debtofpay.250free.com/broadband-internet-provider.html]broadband internet provider[/URL]Anonymous
September 13, 2006
<a href=" http://tramadolsale.oberl.info/tramadol-medicine.html ">tramadol medicine</a>Anonymous
September 13, 2006
<a href=" http://tramadolsale.oberl.info/tramadol-medicine.html ">tramadol medicine</a>Anonymous
September 14, 2006
The comment has been removedAnonymous
September 14, 2006
[URL=http://http://replicarolexwatch.ir.pl]replica-rolex-watch[/URL]<a href="http://http://replicarolexwatch.ir.pl">replica rolex watch</a>Anonymous
September 16, 2006
The comment has been removedAnonymous
September 19, 2006
Best site, good job! This i found in internet <a href="http://hydrocodone-l.blogspot.com/">Hydrocodone</a>">http://hydrocodone-l.blogspot.com/">Hydrocodone</a> [url=http://hydrocodone-l.blogspot.com/]Hydrocodone[/url] goodbye!Anonymous
September 19, 2006
Nice site <a href="http://debt-consolidation-loan-c.blogspot.com/">Debt">http://debt-consolidation-loan-c.blogspot.com/">Debt Consolidation</a> [url=http://debt-consolidation-loan-c.blogspot.com/]Debt Consolidation[/url] bye!Anonymous
September 22, 2006
The comment has been removedAnonymous
September 23, 2006
Hello, you have nice site <a href="http://hometown.aol.com/shlyapa2/cellular-phone.html">Cellular">http://hometown.aol.com/shlyapa2/cellular-phone.html">Cellular Phone</a> [url=http://hometown.aol.com/shlyapa2/cellular-phone.html]Cellular Phone[/url] goodbye.Anonymous
September 23, 2006
<a href="http://hometown.aol.com/shlyapa2/home_for_sale.html">Home">http://hometown.aol.com/shlyapa2/home_for_sale.html">Home for Sale</a> [url=http://hometown.aol.com/shlyapa2/home_for_sale.html]Home for Sale[/url]Anonymous
September 23, 2006
<a href="http://hometown.aol.com/shlyapa2/home_for_sale.html">Home">http://hometown.aol.com/shlyapa2/home_for_sale.html">Home for Sale</a> [url=http://hometown.aol.com/shlyapa2/home_for_sale.html]Home for Sale[/url]Anonymous
September 25, 2006
Great site <a href="http://car-insurance-p4zw.blogspot.com">Car">http://car-insurance-p4zw.blogspot.com">Car Insurance</a> [url=http://car-insurance-p4zw.blogspot.com]Car Insurance[/url] best regards Paul.Anonymous
September 26, 2006
Good site <a href="http://car-insurance-0.blogspot.com/">Car">http://car-insurance-0.blogspot.com/">Car Insurance</a> [url=http://car-insurance-0.blogspot.com/]Car Insurance[/url] bye.Anonymous
September 30, 2006
Very interesting and good point about <a href="http://winmoney.50megs.com/small-business-liability-insurance.html"">http://winmoney.50megs.com/small-business-liability-insurance.html" title="small business liability insurance">small business liability insurance</a> and [URL=http://winmoney.50megs.com/small-business-liability-insurance.html]small business liability insurance[/URL]Anonymous
September 30, 2006
Amazing site, useful <a href="http://parishilton-paris.blogspot.com/ ">Paris Hilton</a> [url=http://parishilton-paris.blogspot.com/]Paris Hilton[/ur] bye!Anonymous
October 01, 2006
Very good site <a href="http://car-insurance-.blogspot.com/ ">Car Insurance</a> [url=http://car-insurance-.blogspot.com/]Car Insurance[/ur] goodbye!Anonymous
October 01, 2006
Good site <a href="http://black-jack-black.blogspot.com/ ">Black Jack</a> [url=http://black-jack-black.blogspot.com/]Black Jack[/url] goodbye!Anonymous
October 01, 2006
Good site! <a href="http://mariah-carey-mariah.blogspot.com/">Mariah">http://mariah-carey-mariah.blogspot.com/">Mariah Carey</a> [url=http://mariah-carey-mariah.blogspot.com/]Mariah Carey[/url] goodbye!Anonymous
October 01, 2006
Great site <a href="http://car-insurance-.blogspot.com/ ">Car Insurance</a> [url=http://car-insurance-.blogspot.com/]Car Insurance[/url] bye!Anonymous
May 29, 2009
PingBack from http://paidsurveyshub.info/story.php?title=ken-henderson-s-weblog-t-sql-coding-challengeAnonymous
June 12, 2009
PingBack from http://cellulitecreamsite.info/story.php?id=5758Anonymous
June 13, 2009
PingBack from http://quickdietsite.info/story.php?id=3Anonymous
June 16, 2009
PingBack from http://fixmycrediteasily.info/story.php?id=4333Anonymous
June 17, 2009
PingBack from http://patioumbrellasource.info/story.php?id=1381Anonymous
June 19, 2009
PingBack from http://edebtsettlementprogram.info/story.php?id=22615Anonymous
June 19, 2009
PingBack from http://mydebtconsolidator.info/story.php?id=7548Anonymous
June 19, 2009
PingBack from http://debtsolutionsnow.info/story.php?id=1189