fn_OBJECT_TYPE

2

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.

CREATE FUNCTION dbo.fn_OBJECT_TYPE (@Object_Id INT) 
RETURNS nvarchar
AS
BEGIN
DECLARE @type_desc nvarchar(120)

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

RETURN @type_desc
END
GO
About these ads

2 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.

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

Follow

Get every new post delivered to your Inbox.

Join 163 other followers

%d bloggers like this: