Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234»»

Polymorphed Proc Expand / Collapse
Author
Message
Posted Monday, March 16, 2009 2:26 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 2:29 PM
Points: 3,193, Visits: 2,290
IANAL^HDBA ???





Regards
Rudy Komacsar
Senior Database Administrator

"Ave Caesar! - Morituri te salutamus."
Post #676919
Posted Monday, March 16, 2009 2:44 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, June 16, 2014 4:03 AM
Points: 488, Visits: 403
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 ...
Post #676940
Posted Tuesday, March 17, 2009 1:08 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, August 10, 2012 6:08 PM
Points: 1,156, 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.
Post #677229
Posted Tuesday, March 17, 2009 7:52 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, July 14, 2014 12:33 PM
Points: 440, Visits: 929
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.

Post #677451
Posted Tuesday, March 17, 2009 8:02 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 24, 2014 2:26 PM
Points: 1,090, Visits: 541
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
Post #677468
Posted Tuesday, March 17, 2009 8:20 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 10:03 AM
Points: 1,851, Visits: 2,010
I'm just suprised that SQL Server would let a stored procedure modify its own code. That's the real head scratcher here.
Post #677497
Posted Tuesday, March 17, 2009 8:43 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 6:20 PM
Points: 33,078, Visits: 15,192
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
Post #677527
Posted Tuesday, March 17, 2009 3:32 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, August 10, 2012 6:08 PM
Points: 1,156, 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.


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.
Post #677936
Posted Tuesday, March 17, 2009 3:33 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, June 15, 2013 1:31 AM
Points: 24, Visits: 85
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 ;)

@++ ;)
Post #677937
Posted Wednesday, April 8, 2009 6:48 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, June 23, 2014 6:03 AM
Points: 920, Visits: 702
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
Post #693024
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse