by Viper
16. February 2009 08:47
While creating a new web application, as part of user management feature I was added bunch of tables and some stored procedures. When I executed my stored procedure sp_adduser I kept getting the error Procedure or function sp_adduser has too many arguments. This drove me nuts. I checked every single line of code, looked at execution of stored procedure in profiler. Everything looked good. I have 4 parameters defined for my stored procedure and one is of type OUTPUT. Then I thought since I am using SQL Server 2008, there may be something different about how OUTPUT parameters are handled. I could not find anything different about it in SQL Server 2008 documentation. Then I decided to create my database in SQL Server 2005 and executed my code against it. Same error from SQL Server 2005 as well. At this point I figured that there is something else going on with this stored procedure. Since SQL Server only tells you name of parameter if there is one missing. So there was no way I was going to get name of parameter that was extra from the error message. So I decided to remove OUTPUT parameter from my call. Then I get following error.
Procedure or function 'sp_adduser' expects parameter '@loginname', which was not supplied
Now this error message was interesting. My stored procedure does not have @loginname parameter defined. That hinted me that my call is colliding with some other stored procedure in database and that stored procedure's name is same as what I have created. I looked at System Stored Procedures. Thats where I found stored procedure sys.sp_adduser defined as below.
procedure [sys].[sp_adduser]
@loginame sysname, -- user's login name in syslogins
@name_in_db sysname = NULL, -- user's name to add to current db
@grpname sysname = NULL -- role to which user should be added.
After I changed name of my stored procedure, everything worked fine. This was kind of interesting debugging exercise. And it showed me that SQL Server query engine first matches count of parameters before it checks individual stored procedure names and other related metadata. And that perfectly makes sense from optimization point of view. Anyways, when you get these kidn of weired errors, do check if there is some system stored procedure or function name that may be colliding with yours.