October 19, 2012 by Kenneth Fisher

I recently started using OBJECT_NAME, OBJECT_SCHEMA_NAME and OBJECT_ID functions; unfortunately I really could have used OBJECT_TYPE but it doesn’t exist (yet?). So for now I wrote one of my own.

FYI I originally wrote it as sp_ and created it in the master db thinking that this would cause it to be available from any database. Unfortunately it appears that this doesn’t work for functions, only stored procedures, views and would you believe tables? I’d love it if someone would explain that one to me sometime.

RETURNS nvarchar
DECLARE @type_desc nvarchar(120)

SELECT @type_desc = type_desc
FROM sys.all_objects
WHERE object_id = @Object_Id

RETURN @type_desc

4 thoughts on “fn_OBJECT_TYPE

  1. sqlpathy says:

    Cool function…..ALso check OBJECTPROPERTY function from BOL. This comes very handy when one wants to filter specific objects from the database.

    • You know for whatever reason I didn’t even think of OBJECTPROPERTY. Turns out OBJECTPROPERTYEX(object_id, ‘BASETYPE’) will return the type of the object. It’s not the type_desc but certainly good enough.

  2. John says:

    Excellent, thank you. Exactly what I was after. Ended up using the following:

    SELECT distinct OBJECT_SCHEMA_NAME(referencing_id) [Schema], OBJECT_NAME(referencing_id) [Name] , OBJECTPROPERTYEX(referencing_id, ‘BASETYPE’) [Type], referenced_server_name [Server], referenced_database_name [Database]
    FROM sys.sql_expression_dependencies

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 806 other followers

%d bloggers like this: