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

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).

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).

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.

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).

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.

I executed the code against a 2008 server and a 2005 server and got this message both times:

Msg 911, Level 16, State 1, Line 1Database 'test2' does not exist. Make sure that the name is entered correctly.Msg 2714, Level 16, State 3, Procedure sp_one, Line 3There is already an object named 'sp_one' in the database.(1 row(s) affected)

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.