« Philly.net Code Camp 2009.2 Saturday October 17, 2009 | Main | Get the name of the executing SQL Stored Procedure, UDF, or trigger inside the object »
Friday
Oct162009

Split full db object names into parts with a little help from a friend

so you have a string in T-SQL such as:

MyProdSvr.ReallyGoodDB.SomeSchema.MyFavoriteTable

Woldn't it be nice if there was an easy mecanism to crack that code.

Try This:


DECLARE @MyObjectFullName	sysname

SET @MyObjectFullName = 'MyProdSvr.ReallyGoodDB.SomeSchema.MyFavoriteTable'

SELECT
	  PARSENAME(@MyObjectFullName, 4) AS ServerName
	, PARSENAME(@MyObjectFullName, 3) AS dbName
	, PARSENAME(@MyObjectFullName, 2) AS SchemaName
	, PARSENAME(@MyObjectFullName, 1) AS ObjectName

 

ServerName dbName SchemaName ObjectName
MyProdSvr ReallyGoodDB SomeSchema MyFavoriteTable

 

{Thanks Chuck}

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.