Creating a System Stored Procedure

  • Comments posted to this topic are about the content posted at

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

  • Surely, this is what the MODEL database is for?

  • Uh, no. The MODEL database is the database from which any new database is created. It is best not to make changes to it unless you want those changes to appear in any new database you create on that server. For example, say you want to create a stored procedure in every new database you create. You'd put that in the MODEL database. You typically would not put a stored procedure you just want to have someplace on the server so you can use it when you need it in the MODEL DB.

    -- J.T.

    "I may not always know what I'm talking about, and you may not either."

  • Yes, I know what MODEL is for, its just that it has been hammered into me since SQL6.5 that the MASTER is and always should be sacrosanct.

    Robert did bring up the issue of MASTER rebuilds losing your procedures.

    Personally either the MASTER or MODEL approach is something that I would only do in a development or testing environment in any case.

  • Man, this article does not go into any depth.  Your example proc has capitals in it, which Microsoft specifically says not to do.  The script for a system proc should follow this format:










      DECLARE @ReturnString varchar(350)

      SELECT @ReturnString =

      UPPER(SUBSTRING(@inputString,1,1)) +







    SELECT fn_proper('scott')

    The full article is available at:


    Signature is NULL

  • Personally, I do not see there is any special reason to create a regular SP in master database. But we know that an extended SP should be created (registered) in master database.

  • Let me answer the responses to my article so far in this way.  First, this article was not written to discuss this subject in any depth.  It is targeted toward the beginning DBA and SQL Programmer.  I didn't believe it needed to go into great depth since it will be rare that a need to create one comes up. 

    I found a need a few years ago and so created one.  The only system stored procedure I have ever created and placed in the master database is the one I talked about briefly in the article.  It simply scripts a table with its indexes and default values.  The script can be found in SQL Server Centrals script library for those interested.

    Next,  I believe it is useful to learn all about SQL Server.  Even things that you would never consider doing in a production environment.  So, if you consider this to be one of those things you will never do, that is fine.  I don't think it will hurt you to know how to create a system stored procedure and be aware of the possible problems associated with doing so.

    I believe there are many schools of thought on how code should be generated.  I learned from my mentor to put SQL in upper case and to put variable names with the first letter of each significant word in upper case and the rest of the characters in lower case. 

    Should you wish to see some different styles go look at the system stored procedures that Microsoft created.  Some examples of the different styles they used are found in sp_configure (almost every character is in lower case), sp_denylogin (some if's are in upper case and some in lower case),  and sp_dropmergearticle (here keywords in the same query are both upper case and lower case).

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

  • Personally I couldn't give a damn about the "don't use capitals" issue.

    For the pedants out there I do understand about case sensitivity.

    What I care about is whether or not there is a consistent policy on layout and formatting that is applied throughout a development.

    As Robert says, for MS to preach about layouts and formats is a case of the kettle calling the pot black.

    I work with a number of clients and they all have their own standards, but at least they are using a standard.

    The fact that you can create a system SP is interesting.  Whether you should or not is an entirely different matter and subject to opinion, and we all know what Dirty Harry said about opinions.

  • This is a little off-topic.  Your modivation for creating a system SP was to have it available to all DBs.  I have a similar question with regard to UDFs.

    Is it possible to create User Defined Functions that are available to all databases in SS 2000 SP3?

    Best Regards,


  • Sorry, I should have been more clear about "capitals in proc".  I meant in the proc name, not in the actual code.

    Apparently this is not required, system procs with uppercases works just fine.  They also talk about using:



    Apparently these rules only apply to system FUNCTIONS, not system procs.  there must be more leniency for some reason.

    Jim, check the link I provided above. 

    Signature is NULL

  • Calvin,

    This is perfect!!!!  Exactly what I was looking for.

    Thank you very much.

  • Ok... let's ask the next logical question...

    Let's say that I DON'T want to use Master for anything but I would like to have a central repository of common code for use across any and all databases much like you can do with Master. For example,

    In Database "A", I have a table called "TableA". In Database "B", I have a stored proc called "ReadTableA". While Database "A" is the current database, I want to execute the "ReadTableA" stored procedure which is located in Database "B" and still have it use "TableA" from Database "A".

    Does anyone know how that can be done without the use of dynamic SQL? I've tried synonyms and can execute "ReadTableA" from Database "A", but it expects "TableA" to be in Database "B" where "ReadTableA" lives.

    It would be real handy for this to work... we're building multiple client databases and I'd like all of the code to be centralized without having to put it in Master.

    Personally, I don't believe it can be done without dynamic SQL but I thought I'd ask.

    --Jeff Moden

    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • [p]Oh dear. This article really should have explained more. As it stands, I'm afraid it is rather misleading. Although it is right in saying that many such procedures will work in some ways as system stored procedures just by putting them in the Master database and by prefixing them with 'sp_', there are differences (which vary from version to version). One important one is that until the sp has been registered as a system stored procedure, it won't be able to see the Object Catalog views for the database, only the master ones. The only reason that the example works is that it uses the old system tables, which for compatibility reasons can be seen. Unfortunately, anyone who takes the advice in this article will become very frustrated. Only if a stored procedure is flagged as being MS_Shipped will it behave in every way as the author suggests. The code in the article will work, but it will not if you use the Object Catalog Views. This won't work. ...[/p]

    create PROCEDURE sp_FindTableNames


    @ColumnName varchar(128)

    ) AS

    SELECT *

    FROM sys.objects

    WHERE object_id IN


    SELECT object_id

    FROM sys.columns

    WHERE name = @ColumnName


    AND type = 'U'


    [p]You can get around this by registering the stored procedure but there is no way of undoing it. You have to delete and recreate if you need to alter it. Also, this stored procedure is officially 'undocumented' though well known, so you use it at your peril and (all the usual caveats).[/p]

    if not exists (SELECT 1 FROM SYS.OBJECTS WHERE NAME = 'sp_FindTableNames' and IS_MS_SHIPPED=1)

    EXEC sp_ms_marksystemobject 'sp_FindTableNames'

    [p]My advice is that it is best not to create a system stored procedure unless you fully understand the repercussions, and have the necessary permi9ssions. I agree it is very magical to have your own special versions of SP_Who, and SP_Help, but please use caution. It is probably best to have a copy of your stored procedure in the database that you are developing, though I agree that this entails a lot more housekeeping and ls likely to leave unwanted scaffolding on your nice new database, if you don't tidy up.[/p]

    Best wishes,
    Phil Factor

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply