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 12»»

Stored procedures Expand / Collapse
Author
Message
Posted Monday, August 3, 2009 8:32 PM
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, August 18, 2014 6:13 AM
Points: 984, Visits: 350
Comments posted to this topic are about the item Stored procedures
Post #764538
Posted Tuesday, August 4, 2009 1:07 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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...
Post #764608
Posted Tuesday, August 4, 2009 1:55 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 25, 2014 6:37 AM
Points: 1,385, Visits: 1,242
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.
Post #764633
Posted Tuesday, August 4, 2009 2:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:47 AM
Points: 2,496, Visits: 2,377
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!!!!
Post #764638
Posted Tuesday, August 4, 2009 7:24 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 1:19 PM
Points: 1,346, Visits: 4,777
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
Post #764789
Posted Tuesday, August 4, 2009 8:48 AM
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: Wednesday, August 20, 2014 11:27 PM
Points: 3,901, Visits: 3,634
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.
Post #764881
Posted Tuesday, August 4, 2009 8:49 AM
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: Thursday, July 17, 2014 10:56 AM
Points: 3,924, Visits: 1,607
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.
Post #764884
Posted Tuesday, August 4, 2009 8:56 AM
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: Wednesday, August 20, 2014 11:27 PM
Points: 3,901, Visits: 3,634
Yes, I am a little worried about Artur.
Post #764892
Posted Tuesday, August 4, 2009 11:42 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 3:12 PM
Points: 2,351, Visits: 2,696
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


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"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
Post #765031
Posted Friday, August 7, 2009 10:55 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
FYI: the BOL link posted after the explanation does not confirm anything said in the explanation.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #767370
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse