Azure managed instance ... Could not find stored procedure 'sys.sp_MS_marksystemobject'

Gokhan Varol 111 Reputation points
2020-10-30T21:22:42.1+00:00

I am a sysadmin at Azure managed instance, I am trying to create system procedures in master database and mark them as system using [sys].sp_MS_marksystemobject] procedure in Azure Managed instance but I get object not found on sp_MS_marksystemobject.

USE master
GO
EXEC [sys].[sp_MS_marksystemobject] [sp_help_revlogin];

is failing with
Msg 2812, Level 16, State 62, Line 3 Could not find stored procedure 'sys.sp_MS_marksystemobject'

SELECT name, SCHEMA_NAME(schema_id) From sys.all_objects WHERE name = 'sp_MS_marksystemobject'
is returning the objectname and sys as schema

SELECT OBJECT_ID('[sys].[sp_MS_marksystemobject]')
returns null

How can I create my own sp_* named system procedures in master database and mark them as system?

Azure SQL Database
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. David Browne - msft 3,851 Reputation points
    2020-10-30T21:52:32.823+00:00

    When you create a procedure with the sp_ prefix in Master it's callable in the context of any database. The only additional thing sp_MS_marksystemobject appears to do is to make the static SQL in the procedure resolve object names in the context of the current database, rather than the context of the database containing the stored procedure.

    You can accomplish this without using an undocumented procedure, as dynamic SQL always resolves object names with respect to the current database, not the database containing the stored procedure.

        use master
        go
        create or alter procedure sp_test
        as
        begin
          declare @sql nvarchar(max)= N'
    
          select * from sys.objects
    
          ';
          exec sp_executesql @sql
        end
    
       go 
        --and execute
    
        exec msdb..sp_test
    

    So all you have to do is take the body of your "system stored procedure" and paste it into a nvarchar(max) literal string, escaping any single quotes by replacing ' with '', and run it with sp_executesql.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.