|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 1:10 PM
Points: 16,
Visits: 99
|
|
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?
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 11:35 AM
Points: 11,646,
Visits: 27,750
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 3:56 PM
Points: 1,324,
Visits: 1,778
|
|
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) One man with courage makes a majority. Andrew Jackson
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 6:03 AM
Points: 2,345,
Visits: 3,191
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 1:10 PM
Points: 16,
Visits: 99
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 1:10 PM
Points: 16,
Visits: 99
|
|
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
|
|
|
|