SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


orphaned extended properties in SQL


orphaned extended properties in SQL

Author
Message
jasonclements32304
jasonclements32304
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 230
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?
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29477 Visits: 39986
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

ScottPletcher
ScottPletcher
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8362 Visits: 7163
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)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7703 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
jasonclements32304
jasonclements32304
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 230
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.
jasonclements32304
jasonclements32304
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 230
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search