|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, March 28, 2013 7:38 AM
Points: 894,
Visits: 317
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, March 27, 2013 1:25 AM
Points: 152,
Visits: 96
|
|
nice question
------------------------- - Name? - Abu Dalah Sarafi. - Sex? - 3 times a week! - No, no. Male or Female? - Male, female, sometimes camel...
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, April 14, 2013 8:55 AM
Points: 1,383,
Visits: 1,212
|
|
damn, really nice question, completely threw me. I was thinking about the fact that "sp_*" procs still need an extra property defined in order to act on objects (tables etc) that belong in the current database.
This sample illustrates what messed me up:
use master go create table LocalTable (testcolumn nVarChar(255)) go INSERT INTO LocalTable SELECT 'This data is in master!' go create proc sp_test as select db_name() AS DBNameFromFunction, (select count(*) from sysobjects) AS ObjectCountFromSystemView, (SELECT TOP 1 testcolumn FROM LocalTable) AS DBNameFromLocalObject go create database testdb go use testdb go create table LocalTable (testcolumn nVarChar(255)) go INSERT INTO LocalTable SELECT 'This data is in testdb!' go sp_test go use master go sp_test go use master go drop database testdb drop proc sp_test drop table LocalTable go
You expect the "LocalObject" result column to show the results of that table in the current context database, but it does not - it shows you the contents of that table in the master database!
I had a very hard time understanding (or finding any documentation / help on) this behaviour when I first started writing "sp_*" procs, but I haven't looked in a while; I guess it's generally discouraged anyway 
If you really do want to change this behaviour (and see the correct result in the "LocalObject" column returned by the proc above), you need to use the "sp_ms_marksystemobject" stored procedure. Please note, it only makes sense to do this if you know that your proc will only ever be called from databases that will contain the local tables you are referencing.
In my case it makes sense for some management procs that need to work on dozens or hundreds of databases with essentially the same structure - it helps avoid using less-safe and harder-to-read "EXEC()" or "sp_executesql" calls.
(I was tempted to go write my own version of this QotD with the "trick", but felt it infringed a little on Anders' question - maybe something like that is coming anyway)
Hope this helps someone, Tao
http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:04 AM
Points: 1,968,
Visits: 1,819
|
|
Artur Sokhikyan (8/4/2009) nice question ------------------------- - Name? - Abu Dalah Sarafi. - Sex? - 3 times a week! - No, no. Male or Female? - Male, female, sometimes camel...
Strong!!!!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 9:05 AM
Points: 1,089,
Visits: 4,538
|
|
For those that have html tags inside the question (I'm using Chrome), the question shows up properly in the email... and I posted it below:
use master go create database testdb go create proc sp_test as select db_name() go sp_test go use testdb go sp_test go use master go drop database testdb drop proc sp_test go
- Jeff
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 PM
Points: 3,390,
Visits: 3,403
|
|
| Yes, really nice question that illustrates the perils of naming a stored procedure using the naming convention 'sp_' which is used for system stored procedures.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Friday, March 15, 2013 2:43 PM
Points: 3,924,
Visits: 1,554
|
|
Artur Sokhikyan (8/4/2009) - Name? - Abu Dalah Sarafi. - Sex? - 3 times a week! - No, no. Male or Female? - Male, female, sometimes camel...
ROFL .... Your signature made my day.
SQL DBA.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 PM
Points: 3,390,
Visits: 3,403
|
|
Yes, I am a little worried about Artur.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:05 PM
Points: 2,117,
Visits: 2,209
|
|
Thanks, great question. Usually "great question" means one where I don't know enough to get it right, so I was happy to get this one right, and for the right reason!
- webrunner
------------------- "The chemistry must be respected." - Walter White
"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'" Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855,
Visits: 9,374
|
|
|
|
|