August 23, 2011 at 2:45 am
I too thought it would return 'this'.
I've read the documentation about 'sp_' and system stored procedures but I've always misunderstood until now.
The documentation doesn't really make clear what is a system stored procedure.
I always assumed it meant: any stored procedure defined in the master database, including the pre-defined routines that come with SQL Server.
Now I understand that system stored procedure only refers to the pre-defined routines.
So when SQL Server sees a stored procedure name starting with 'sp_':
First it checks to see if it is a pre-defined routine (like sp_who).
Then it checks the current database.
Then it checks the master database.
I think Microsoft needs to rewrite its documentation to make this clearer.
August 23, 2011 at 2:50 am
Christian Buettner-167247 (8/23/2011)
elbedata (8/23/2011)
It returns "that" when I test on SQL Server 2008, but I thought it should return "this"...Has this changed in later SQL versions? I am pretty sure I've learned that prefixing SP:s with "sp_" was a bad idea and older MS documents clearly states this.
Lars B
Better check "Naming Stored Procedures", it perfectly explains what is happening.
(sp_one in master is not a system stored procedure, therefore it is not "preferred" over the one in the current database).
yes got the idea if there is already created sp in master then it would execute otherwise if we create sp_one in master and in our database , then our DB Sp would executes first.
one question : how can we create a system procedure in master by ourselves ?
August 23, 2011 at 3:04 am
Hafiz Muhammad Suleman (8/23/2011)
one question : how can we create a system procedure in master by ourselves ?
I may be wrong, but as far as I know, you cannot create a system stored procedure--they are provided by Microsoft only.
You will find articles on the internet that will tell you by prefixing a stored procedure with sp_ and storing it in the master database you're creating a system stored procedure, but as Christian has already pointed out, you're not.
If MS ever decides to come up with a (real) system stored procedure that uses the same name as your user stored procedure, your code will be disregarded and won't execute any more.
Regards,
Michael
August 23, 2011 at 3:27 am
I executed the code against a 2008 server and a 2005 server and got this message both times:
[font="Courier New"]Msg 911, Level 16, State 1, Line 1
Database 'test2' does not exist. Make sure that the name is entered correctly.
Msg 2714, Level 16, State 3, Procedure sp_one, Line 3
There is already an object named 'sp_one' in the database.
(1 row(s) affected)[/font]
and this result:
[font="Courier New"]this [/font]
:ermm:
August 23, 2011 at 3:36 am
Good question, but:
Although the later BoL versions (SQL 2005, 2008,2008 R2, and Denali) are better, and make it clear that they are talking about the names of system stored procedures and not of any/all stored procedure beginning sp_ in the master database, the BoL reference given (to the SQL 2000 page) is extremely poorly worded (it even states that master is searched first for SPs whose name begins "sp_", without any restriction to system SPs). As SQL Server 2000 is now outdated, and mainstream support for it is no longer available, and the question doesn't specify a version, it seems odd to specify this version of the page as the reference for the answer.
edit: spelling
Tom
August 23, 2011 at 3:53 am
I think the reason for executing the sp under the user database rather than the master db is the last use statement was "Use Test1" and after that there is no use statement which means the current db is the "Test1" and that is the reason for executing the sp under the user db.
August 23, 2011 at 4:10 am
eric_van_uden (8/23/2011)
I executed the code against a 2008 server and a 2005 server and got this message both times:[font="Courier New"]Msg 911, Level 16, State 1, Line 1
Database 'test2' does not exist. Make sure that the name is entered correctly.
Msg 2714, Level 16, State 3, Procedure sp_one, Line 3
There is already an object named 'sp_one' in the database.
(1 row(s) affected)[/font]
and this result:
[font="Courier New"]this [/font]
:ermm:
Did you create test2 db?
M&M
August 23, 2011 at 4:36 am
I thought it would execute from master as well so got this one wrong. Never mind, I'd never create a user proc with the sp prefix anyway 🙂
August 23, 2011 at 4:42 am
...
August 23, 2011 at 4:44 am
mohammed moinudheen (8/23/2011)
eric_van_uden (8/23/2011)
I executed the code against a 2008 server and a 2005 server and got this message both times:[font="Courier New"]Msg 911, Level 16, State 1, Line 1
Database 'test2' does not exist. Make sure that the name is entered correctly.
Msg 2714, Level 16, State 3, Procedure sp_one, Line 3
There is already an object named 'sp_one' in the database.
(1 row(s) affected)[/font]
and this result:
[font="Courier New"]this [/font]
:ermm:
Did you create test2 db?
Script does not contain creation of or state the existence of test2 database.
August 23, 2011 at 5:09 am
good question!!!
August 23, 2011 at 5:27 am
Creating test2 db wasn't in the case description, so I didn't. So the SQL message correctly reflects this.
August 23, 2011 at 5:40 am
Should have read the syntax better. Thanks for the question even though I didn't get my point.
http://brittcluff.blogspot.com/
August 23, 2011 at 5:44 am
Thanks for the question. At least you sparked some controversy. Pointing out the now obvious, would have been much better to have included a note saying that test2 had been created, or to substitute the name of your own production database.
However, I sympathize - so easy to assume others share your frame of reference.
[font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
Connect to me on LinkedIn
August 23, 2011 at 5:58 am
ChrisP-374390 (8/22/2011)
The results could depend on the existence of database test2...
My fault for answering and assuming that test2 did not exist. Apparently it did exist in this question.
/* ----------------------------- */
Tochter aus Elysium, Wir betreten feuertrunken, Himmlische, dein Heiligtum!
Viewing 15 posts - 16 through 30 (of 72 total)
You must be logged in to reply to this topic. Login to reply