June 8, 2010 at 9:59 pm
Comments posted to this topic are about the item Stored Procedure Execution
The Redneck DBA
June 8, 2010 at 10:00 pm
Great question, thanks Jason!
The old don't start your SP names with SP_
I really wish I knew all of this 13 years ago... 😉
June 8, 2010 at 10:50 pm
thanks, great question... got it wrong though 🙁
A user-defined stored procedure that has the same name as a system stored procedure and is either nonqualified or is in the dbo schema will never be executed; the system stored procedure will always execute instead.
i have read the microsoft link; was wondering if the user-defined proc doesn't ever run then what is the purpose of creating that proc ? why can't an error be thrown instead ?
June 8, 2010 at 11:08 pm
Nailed it, because I assumed that "person" was not me. If I were "person" the answer would have been 5.
June 8, 2010 at 11:13 pm
ziangij (6/8/2010)
thanks, great question... got it wrong though 🙁A user-defined stored procedure that has the same name as a system stored procedure and is either nonqualified or is in the dbo schema will never be executed; the system stored procedure will always execute instead.
i have read the microsoft link; was wondering if the user-defined proc doesn't ever run then what is the purpose of creating that proc ? why can't an error be thrown instead ?
1) the user defined proc can be run explicitly, perhaps in within an EXEC(string) used in a proc that built the command with the user name as argument.
2)Or if that particular user (joeFoo) runs sp_whatever, and his/her username is attached to another proc with the same name (joeFoo.sp_whatever), then THAT proc executes.
June 9, 2010 at 12:50 am
I got wrong.Thought that it will give error the sp_ ...... procedure already exists if the procedure name alreay in master database.
Malleswarareddy
I.T.Analyst
MCITP(70-451)
June 9, 2010 at 1:04 am
forjonathanwilson (6/8/2010)
Nailed it, because I assumed that "person" was not me. If I were "person" the answer would have been 5.
Or if the default schema of the user executing it is person.
June 9, 2010 at 2:10 am
Avoiding uage of prefix sp_ improves the performance.
June 9, 2010 at 2:27 am
Very Nice question
I am not sure but the query is returning me 5....
I dont have the AdventureWorks db on the server...
Tried using the test database with dbo schema.... may be this is why the results are different for me
Prashant Bhatt
Sr Engineer - Application Programming
June 9, 2010 at 4:45 am
Good question Jason!
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 9, 2010 at 5:27 am
this does not work as explained with SQL 2008 R2.
sp or not, the local version of the stored procedure is executed first.
June 9, 2010 at 6:36 am
Executing the code as written... won't it return 5? "Use Adventureworks" points the "EXECUTE sp_ReturnSomething 5" to Adventureworks and runs that proc. What am I missing this early in the A.M.?
June 9, 2010 at 7:08 am
Avoiding uage of prefix sp_ improves the performance
When a stored procedure is executed using "sp_", SQL Server checks in the master first, as "sp_" is assumed to be reserved for a system stored procedure. Thus, the performance improvemnet would be caused by not having to go to the system to look first for the procedure.
In this case, executing the procedure as written causes the process to be executed from the master (system) where the code is
SELECT @Input + 2 AS Result
Steve Jimmo
Sr DBA
“If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
June 9, 2010 at 8:27 am
prashant.bhatt (6/9/2010)
Very Nice questionI am not sure but the query is returning me 5....
I dont have the AdventureWorks db on the server...
Tried using the test database with dbo schema.... may be this is why the results are different for me
Yes, if DBO is your default schema and you create the stored procedure in the DBO schema that returns 5, then that is the version of the stored procedure that will execute.
June 9, 2010 at 8:43 am
Great question, My first thought was the correct answer, but then I noticed that the procedure in the Master db was not prefixed by a schema so I had to do some reading to see how that would be handled.
I think the lesson here is never use sp in your stored procedure names and always qualify your calls.
Viewing 15 posts - 1 through 15 (of 31 total)
You must be logged in to reply to this topic. Login to reply