SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Polymorphed Proc


Polymorphed Proc

Author
Message
Rudyx - the Doctor
Rudyx - the Doctor
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4777 Visits: 2503
IANAL^HDBA ???

Regards
Rudy Komacsar
Senior Database Administrator

"Ave Caesar! - Morituri te salutamus."
brewmanz
brewmanz
Mr or Mrs. 500
Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)

Group: General Forum Members
Points: 547 Visits: 406
IANAL = I Am Not A Lawyer
^H = control-H = backspace 1 char (old Teletype/CPM/MS-DOS control sequence), gives IANA = I Am Not A
DBA = .. this is left as an exercise for the reader !

I considered just using 'IANADBA', but this is not a standard (or maybe it is in this forum!)

Apologies for any confusion ...
DPhillips-731960
DPhillips-731960
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1248 Visits: 801
The reason it works is far simpler than it seems.

Essentially 2 calls are made:
1. Create a proc named "GO".
2. Execute that proc.

The code is loaded into memory when EXECuted. The loaded "in-memory" version of the code has no bearing on the PROC being created/modified/run/dropped, and as such remains unchanged.

The result set(s) and messages ultimately belong to the initial in-memory code EXECution, not the modified saved and re-executed instance(s).

Only when execution exits, does the code become vaporware.

The same is true about any PROC mod mid PROC. Changes do not operate until the next time the proc is called. For example, add another "EXEC GO" to the end of the code. You will get the same result, but also an error on the second EXEC, because it no longer exists to be loaded into memory and run again:
"
(1 row(s) affected)
Msg 2812, Level 16, State 62, Line 2
Could not find stored procedure 'GO'.
"

OK, maybe it doesn't sound simple... but the point is the initial code in memory is the control, everything else happens within that instance.
J-440512
J-440512
Right there with Babe
Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)

Group: General Forum Members
Points: 721 Visits: 949
I strongly object to such obfuscated programming. How many beginners who subscribed to this forum could be misled into thinking "wow ! clever trick I've got to find an occasion to use it".

For this reason, I fully agree with Peter Schott, except that since someone might be forced to deal with lousy code, I think the offender should be lynched and his house burned down to the ground.

OK, if you want to play with brainteasers. However, beginners and even intermediate SQL'ers would benefit a lot by reading topnotch articles such as the Running Count by Jeff Moden. Then you end up with considerably more useful and practical knowledge.

At least, the rewrite by Robert Edgson was useful in demonstrating how to do things properly.

I remember from the early 1980's the great Obfuscated C Code contest. One in particular was formatted so that the listing looked like the pi number, only it was calculating the value of e.
Aaron N. Cutshall
Aaron N. Cutshall
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1868 Visits: 1036
J (3/17/2009)
I strongly object to such obfuscated programming. How many beginners who subscribed to this forum could be misled into thinking "wow ! clever trick I've got to find an occasion to use it".

The purpose of these brainteasers is not to confuse anyone nor to encourage folks to write code that is not clear, but to get you to fully understand HOW things work. After all, what would be the fun in always working with clear examples that don't encourage you to think?

OK, if you want to play with brainteasers. However, beginners and even intermediate SQL'ers would benefit a lot by reading topnotch articles such as the Running Count by Jeff Moden. Then you end up with considerably more useful and practical knowledge.

As I mentioned, this isn't to confuse newbies, but to give us "old hands" a bit of a noggin nuggin'!

I remember from the early 1980's the great Obfuscated C Code contest. One in particular was formatted so that the listing looked like the pi number, only it was calculating the value of e.

I remember those contests quite well and rather enjoyed them. Again, the purpose was not to encourage obfuscated coding in production use but to provide brain stimulation. Don't get so upset over it!


"...when ye are in the service of your fellow beings ye are only in the service of your God." -- Mosiah 2:17
Chris Harshman
Chris Harshman
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4893 Visits: 3962
I'm just suprised that SQL Server would let a stored procedure modify its own code. That's the real head scratcher here. Ermm
Steve Jones
Steve Jones
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: Administrators
Points: 62452 Visits: 19102
I have edited the question to show that this is a bad idea and include a warning to newbies.

I think it's worth pointing this out, not so someone will go do this, but in case they stumble upon it, like mistyping an object identifier, they might better understand what's happening.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
DPhillips-731960
DPhillips-731960
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1248 Visits: 801
Chris Harshman (3/17/2009)
I'm just suprised that SQL Server would let a stored procedure modify its own code. That's the real head scratcher here. Ermm


Many code environs can allow this, especially in DBs where the SPs are actually stored within the system, not individual physical disk files. This is not unique only to SQL Server.

The trick is, it isn't modifying its' own code... the in memory version is modifying the saved version... it doesn't need to lock the file for writes, as it has read the whole thing in. Any internal sub-executions are also read into memory and the saved object is still free for ALTER/DROP.

Having a valid reason to do so is another issue entirely, but functionally there is no reason to dis-allow it.
ElSuket
ElSuket
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 109
Hello,

Basically GO is an OSQL and now an SQLCMD command, and had never been a SQL or T-SQL command.
GO matches the SQL identifiers, so it has to work.
Anyway, as it has been said before, this kind of naming has to be banned Wink

@++ Wink
Abhijit More
Abhijit More
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1111 Visits: 767
You should always know why this is working if you dn't know ask experts

in below examples it creates the procedure with name 'GO' just like 'CREATE PROC uspGO' it takes the character as a procedure name not as a GO keyword.

CREATE PROC GO AS BEGIN
EXEC ('ALTER PROC GO AS SELECT NULL')
EXEC GO
DROP PROC GO
END
GO
EXEC GO

Abhijit - http://abhijitmore.wordpress.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search