Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

orphaned extended properties in SQL Expand / Collapse
Author
Message
Posted Tuesday, January 15, 2013 7:59 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, August 18, 2014 2:07 PM
Points: 18, Visits: 151
Is it possible to have orphaned extended properties in SQL.
And if so what is best practice to clean them up?
And how are they caused?
Post #1407279
Posted Tuesday, January 15, 2013 8:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:39 AM
Points: 12,906, Visits: 31,985
it depends;
you cannot create an extended property that points to an object(includes the parameters for level0type/@level1type/@level2type) without the object actually existing.

an example:
 
EXEC sys.sp_addextendedproperty
@name=N'ColumnDescription',
@value=N'Credit Card Expiration Date' ,
@level0type=N'SCHEMA',
@level0name=N'dbo',
@level1type=N'TABLE',
@level1name=N'CustomerOrder',
@level2type=N'COLUMN',
@level2name=N'Order_CCExp'



Msg 15135, Level 16, State 8, Procedure sp_addextendedproperty, Line 37
Object is invalid. Extended properties are not permitted on 'dbo.CustomerOrder.Order_CCExp', or the object does not exist.


if you delete the object, the extended properties for that object get deleted too.

CREATE procedure [dbo].[sp_find]         
@findcolumn varchar(50)
as
begin
set nocount on
select sysobjects.name as TableFound,syscolumns.name as ColumnFound
from sysobjects
inner join syscolumns on sysobjects.id=syscolumns.id
where sysobjects.xtype='U'
and (syscolumns.name like '%' + @findcolumn +'%'
or sysobjects.name like '%' + @findcolumn +'%' )
order by TableFound,ColumnFound
end
GO
EXEC sys.sp_addextendedproperty
@name = N'Version',
@value = N'9.0.154.90',
@level0type = N'SCHEMA', @level0name = 'dbo',
@level1type = N'PROCEDURE', @level1name = 'sp_find';
EXEC sys.sp_addextendedproperty
@name = N'Purpose',
@value = N'simple tool to find column or table names that are LIKE the inputed value',
@level0type = N'SCHEMA', @level0name = 'dbo',
@level1type = N'PROCEDURE', @level1name = 'sp_find';
--show all extended properties
SELECT objtype, objname, name, value
FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'PROCEDURE', 'sp_find', NULL, NULL);
--get just the "Version" that i created:
SELECT objtype, objname, name, value
FROM fn_listextendedproperty ('Version', 'schema', 'dbo', 'PROCEDURE', 'sp_find', NULL, NULL);


you can create extended property nave/value pairs that are not connected to anything, so i wouldn't call them stranded, but they might not make sense to someone.
EXEC sys.sp_addextendedproperty  
@name = N'Version',
@value = N'9.0.154.90'

EXEC sys.sp_addextendedproperty
@name = N'InstanceName',
@value = N'LOWELL\SQL2008'



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1407291
Posted Tuesday, January 15, 2013 3:39 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:49 PM
Points: 2,030, Visits: 3,030
As Lowell states, they're not really orphaned.

Extended properties that aren't explicitly subordinated to a db object apply to the db itself.

That is, if you add extended properties w/o specifying a specific object, they become extended properties of the db itself, so only dropping the db would drop those extended properties.

Otoh, if you add extended properties to a column, for example, they "belong" to that column and automatically get dropped with the column (or owning table) itself.


SQL DBA,SQL Server MVP('07, '08, '09)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1407511
Posted Tuesday, January 15, 2013 5:50 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 4:14 AM
Points: 3,618, Visits: 5,254
Another cool thing about extended properties is that, when you add them (for example) to a table or to a column, when you use SSMS to script that table's creation, the script includes adding the current extended properties. So they are easily transferred between databases.

Does anyone besides me find it irritating that you have to know whether a property exists or not and then either add it or insert it? You'd think the system maintenance SPs could be made smart enough to handle this case.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1407527
Posted Wednesday, January 16, 2013 7:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, August 18, 2014 2:07 PM
Points: 18, Visits: 151
Thanks for all the great replies. I'm learning alot.
I was working on reviewing extended properties we currently have and forgot to join to sys.objects. I was joined to sys.table. So I thought I was looking at orphans. Brain Fart!

Me and my team are using Red-gate's SQLDOC for our DB Docs. We have a built nightly PowerShell that run SQLDOC command line that is running to over the current post SQLDOCs.

<#  
=====================
Delete Current SQLDOC
=====================
#>
$path = '\\server\d$\PVP\PVP21\webcontent\Powershell\Database_Documentation\DBPrime1\'
Get-ChildItem $path |
Sort-Object { $_.Name -as [Version] } |
Select-Object -Last 1 |
Remove-Item -recurse
<#
===============
Create SQLDOC
===============
#>
$CMD = 'C:\Program Files (x86)\Red Gate\SQL Doc 2\sqldoc.exe'
$arg1 = '/project:H:\My Documents\SQL Server Management Studio\Red-Gate\DBPrime1.sqldoc'
$arg2 = '/filetype:html-frames'
$arg3 = '/force'
$arg4 = '/outputfolder:"\\server\d$\PVP\PVP21\webcontent\Powershell\Database_Documentation\DBPrime1"'

& $CMD $arg1 $arg2 $arg3 $arg4

<#
"C:\Program Files (x86)\Red Gate\SQL Doc 2\sqldoc.exe" /project:"H:\My Documents\SQL Server Management Studio\Red-Gate\DBPrime1.sqldoc" /filetype:html-frames /force /outputfolder:"\\rpt102\d$\PVP\PVP21\webcontent\Powershell\Database_Documentation\DBPrime1"
#>

<#
===============
Rename Directory
===============
#>
cd \\server\d$\PVP\PVP21\webcontent\Powershell\Database_Documentation\DBPrime1
dir | Where-Object {$_.Name} | Rename-Item -NewName "DBPrime1"


I have created a have also created a SSMSToolPack Custom Script. That we can run to create our custom extended properties when we are developing the tables, views, SP, and etc.


DECLARE @type varchar(50), @timestamp DATETIME, @user VARCHAR(100)

SELECT @type=CASE WHEN [type] = 'U'
THEN 'TABLE'
WHEN [type] = 'V'
THEN'VIEW'
WHEN [type] = 'P'
THEN 'PROCEDURE
WHEN [type] = 'PK'
THEN 'PRIMARY_KEY_CONSTRAINT'
WHEN [TYPE] = 'D'
THEN 'DEFAULT_CONSTRAINT'
WHEN [type] = 'FN'
THEN 'SQL_SCALAR_FUNCTION'
END
FROM sys.objects
WHERE [NAME] = '|ObjectName|'

SELECT @timestamp=GETDATE()

SELECT @user=SYSTEM_USER


EXEC sys.sp_addextendedproperty @name=N'Created by', @value=@user , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=@type,@level1name=N'|ObjectName|'

EXEC sys.sp_addextendedproperty @name=N'Purpose', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=@type,@level1name=N'|ObjectName|'

EXEC sys.sp_addextendedproperty @name=N'Created on', @value=@timestamp, @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=@type,@level1name=N'|ObjectName|'
GO

My new thought was this morning is it even possible to have default extended properties when the tables or etc. are created? Default extended properties. Standards for all users to fill in.
Post #1407842
Posted Wednesday, January 16, 2013 2:18 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, August 18, 2014 2:07 PM
Points: 18, Visits: 151
Answered my own question today.

I setup a new Database trigger:

CREATE TRIGGER [DB_Add_extended_propeties]

ON DATABASE

FOR CREATE_TABLE,CREATE_VIEW, CREATE_PROCEDURE
AS

SET NOCOUNT ON
DECLARE @eventInfo XML
SET @eventInfo = EVENTDATA()

DECLARE @timestamp DATETIME, @user VARCHAR(100), @ObjectType VARCHAR(50), @ObjectName VARCHAR(50)


SELECT @timestamp=GETDATE()
SELECT @user=SYSTEM_USER
SET @ObjectType=CONVERT(VARCHAR(50),@eventInfo.query('data(/EVENT_INSTANCE/ObjectType)'))
SET @ObjectName=CONVERT(VARCHAR(50),@eventInfo.query('data(/EVENT_INSTANCE/ObjectName)'))

EXEC sys.sp_addextendedproperty @name=N'Created by', @value=@user , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=@ObjectType ,@level1name=@ObjectName

EXEC sys.sp_addextendedproperty @name=N'Purpose', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=@ObjectType ,@level1name=@ObjectName

EXEC sys.sp_addextendedproperty @name=N'Created on', @value=@timestamp, @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=@ObjectType ,@level1name=@ObjectName
GO

Post #1408079
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse