How to identify system objects when viewing list of SQL Server database objects?
I'm trying to list all the stored procedures from all the databases on my server, and I can't seem to filter out system objects reliably. I was using:
SELECT *
FROM sysobjects
WHERE id > 100
Which seems to work fine in every database except MSDB, which is full of a ton of stored procs with normal-looking IDs, but they're system stored procs. As far as I can tell, there's no way for me to filter out system stored procs u开发者_StackOverflow社区sing any of the values in the sysobjects table - does anybody else know of a value that can be used to filter?
They're all marked as type="P", which means it's a stored proc, but there seems to be no flag to specify if it's a system stored proc or a user one. I can use the sys.objects view and filter for "IsMsShipped=0", but I'd like something that also works on SQL 2000, so I'd prefer to use the older views (like sysobjects) if it's possible.
This works on my SQL Server 2008 R2 install. I don't see much at all except for user databases
SELECT
*
FROM
sys.objects
WHERE
OBJECTPROPERTY(object_id, 'IsMSShipped') = 0
You can change sys.objects to say, sys.tables and it still works, or use the "type" column to filter. Or use OBJECTPROPERTY(object_id, 'IsProcedure') etc.
Note: it's sys.objects in SQL Server 2005+
Note 2: OBJECTPROPERTY will work for SQL Server 2000 too:
SELECT
*
FROM
sysobjects
WHERE
OBJECTPROPERTY(id, 'IsMSShipped') = 0
SQL Server 2005 and higher
SELECT
SCHEMA_NAME(obj.schema_id) AS schema_name,
obj.name AS proc_name
FROM
sys.procedures obj WITH(NOLOCK)
ORDER BY
schema_name,
proc_name
SQL Server 2000
SELECT
USER_NAME(obj.uid) AS user_name,
obj.name AS proc_name,
FROM
sysobjects obj WITH(NOLOCK)
WHERE
(obj.status & 0x80000000) = 0
AND RTRIM(obj.xtype) IN ('P', 'RF')
ORDER BY
user_name,
proc_name
In certain SQL Versions (I don't know in what version this started), there is at least one scenario where a database can contain system objects that have an IsMSShipped property that is set to false.
With the ability to add database diagrams to a database, the follow objects are often added after an initial install, at the point when a user, say, in SSMS, initiates a request to create database diagrams.
I would modify the previously accepted answer to be as follows:
SELECT
*
FROM
sysobjects obj
WHERE
OBJECTPROPERTY(obj.id, 'IsMSShipped') = 0
AND (CAST(
CASE
WHEN (
SELECT
major_id
FROM
sys.extended_properties
WHERE
major_id = obj.id and
minor_id = 0 and
class = 1 and
name = N'microsoft_database_tools_support')
IS NOT NULL then 1
ELSE 0
END as bit)) = 0
This additional sub-query was take from code executed by SSMS when getting a list of (non-)system objects.
精彩评论