<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8948206673859997041</id><updated>2011-04-21T11:21:51.286-07:00</updated><category term='Final Solution'/><title type='text'>proc_ar_BumpCacheInvalidationCounter</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://vladco.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8948206673859997041/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://vladco.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Vladco</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8948206673859997041.post-1029146279082060403</id><published>2008-06-23T09:05:00.000-07:00</published><updated>2008-06-23T09:12:27.033-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Final Solution'/><title type='text'>proc_ar_BumpCacheInvalidationCounter - Final Solution.</title><content type='html'>This Script will restore all missing stored procedures:&lt;br /&gt;&lt;br /&gt;1. Please copy the entire script in New Query on your SharedServices_DB.&lt;br /&gt;2. Change USE [SharedServices_DB] to your Database name!!&lt;br /&gt;3. Run the script!&lt;br /&gt;&lt;br /&gt;NOTE:&lt;br /&gt;Grant the following rights in shared services:&lt;br /&gt;Default Content Access Account:Manage Analytics, Manage Audiences, Manage User Profiles, Personal Features, Personal Site, Set Permissions&lt;br /&gt;Search Service Account: Manage Analytics, Manage Audiences, Manage User Profiles, Personal Features, Personal Site, Set Permission&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;USE [SharedServices_DB]&lt;br /&gt;&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_AddOrInsertLocalizedNameForMetadataObjectId] Script Date: 06/23/2008 08:03:59 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_AddOrInsertLocalizedNameForMetadataObjectId] ( @MetadataObjectId int, @LocalizedName nvarchar (255), @LCID int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Check for MaxDECLARE @LocNamesForObject intSET @LocNamesForObject = (SELECT COUNT (Id) FROM AR_LocalizedName WHERE MetadataObjectId = @MetadataObjectId)IF (@LocNamesForObject &gt;= 100)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Add or insertDELETE FROM AR_LocalizedNameWITH (HOLDLOCK)WHERE MetadataObjectId = @MetadataObjectId AND LCID = @LCID&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;INSERT INTO AR_LocalizedNameWITH (HOLDLOCK) (MetadataObjectId, LocalizedName, LCID) VALUES (@MetadataObjectId, @LocalizedName, @LCID)&lt;br /&gt;IF (@@ROWCOUNT != 1)BEGIN SET @ErrorCode = -1 GOTO ON_ERROREND&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_AddOrInsertPropertyForMetadataObjectId] Script Date: 06/23/2008 08:04:00 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_AddOrInsertPropertyForMetadataObjectId] ( @MetadataObjectId int, @Name nvarchar (255), @Value sql_variant, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;-- check object existsIF NOT EXISTS (SELECT Id FROM AR_MetadataObject WHERE &lt;a href="mailto:Id=@MetadataObjectId"&gt;Id=@MetadataObjectId&lt;/a&gt;)BEGIN SET @ErrorCode = -2 GOTO ON_ERROREND&lt;br /&gt;-- check for MaxDECLARE @PropertiesForObject intSET @PropertiesForObject = (SELECT COUNT (Id) FROM AR_Property WHERE MetadataObjectId = @MetadataObjectId)IF( @PropertiesForObject &gt;= 50)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Add or insertBEGIN TRANSACTIONDELETE FROM AR_PropertyWITH (HOLDLOCK)WHERE AR_Property.MetadataObjectId = @MetadataObjectId AND AR_Property.Name = @Name&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;INSERT INTO AR_PropertyWITH (HOLDLOCK) (MetadataObjectId, Name, Value) VALUES (@MetadataObjectId, @Name, @Value)&lt;br /&gt;IF (@@ROWCOUNT != 1)BEGIN SET @ErrorCode = -1 GOTO ON_ERROREND&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_BumpCacheInvalidationCounter] Script Date: 06/23/2008 08:04:00 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_BumpCacheInvalidationCounter] ( @MetadataObjectType [nvarchar] (250), @ObjectCache [bit])AS&lt;br /&gt;BEGIN TRANSACTIONDECLARE @count [int]IF @ObjectCache = 1BEGIN SELECT @count = ObjectCacheCounter FROM AR_CacheCounters WHERE MetadataObjectType = @MetadataObjectType IF @count &gt;= 2147483646 SET @count = 0 UPDATE AR_CacheCounters SET ObjectCacheCounter = (@count+1) WHERE MetadataObjectType = @MetadataObjectTypeENDELSEBEGIN SELECT @count = RelationshipCacheCounter FROM AR_CacheCounters WHERE MetadataObjectType = @MetadataObjectType IF @count &gt;= 2147483646 SET @count = 0 UPDATE AR_CacheCounters SET RelationshipCacheCounter = (@count+1) WHERE MetadataObjectType = @MetadataObjectTypeEND&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_BumpMetadataObjectVersionCounter] Script Date: 06/23/2008 08:04:00 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_BumpMetadataObjectVersionCounter] ( @MetadataObjectId int)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;DECLARE @count [int]&lt;br /&gt;SELECT @count = VersionFROM AR_MetadataObjectWITH (HOLDLOCK)WHERE Id = @MetadataObjectId&lt;br /&gt;IF @count &gt;= 2147483646 SET @count = 0&lt;br /&gt;UPDATE AR_MetadataObjectSET Version = (@count+1) WHERE Id = @MetadataObjectId&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_ClearAccessControlEntriesForMetadataObject] Script Date: 06/23/2008 08:04:01 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_ClearAccessControlEntriesForMetadataObject] ( @MetadataObjectId [int])AS&lt;br /&gt;DELETE FROM AR_MetadataObjectSecurity WHERE MetadataObjectId = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CopyAccessControlEntriesForMetadataObjectId] Script Date: 06/23/2008 08:04:01 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CopyAccessControlEntriesForMetadataObjectId] ( @SourceMetadataObjectId int, @DestinationMetadataObjectId int)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;IF( @SourceMetadataObjectId != @DestinationMetadataObjectId)BEGIN&lt;br /&gt;EXEC proc_ar_ClearAccessControlEntriesForMetadataObject @DestinationMetadataObjectId&lt;br /&gt;INSERT INTO AR_MetadataObjectSecurity (MetadataObjectId, IdentityName, DisplayName, RawSid, Rights) SELECT @DestinationMetadataObjectId, IdentityName, DisplayName, RawSid, Rights FROM AR_MetadataObjectSecurity WHERE MetadataObjectId = @SourceMetadataObjectIdEND&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateAction] Script Date: 06/23/2008 08:04:01 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateAction] ( @Name nvarchar(50), @IsCached bit, @EntityId int, @Position tinyint, @IsDisplayed bit, @IsOpenedInNewWindow bit, @Icon nvarchar(255), @Url nvarchar(255), @CreatedId int OUTPUT, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;DECLARE @MaxActions intDECLARE @NoOfActions int&lt;br /&gt;BEGIN TRAN&lt;br /&gt;SET @ErrorCode = 0SET @MaxActions = 20&lt;br /&gt;-- Lock Action table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 7 -- action table&lt;br /&gt;-- Check for max limitSET @NoOfActions = (SELECT count(*) FROM AR_Action WHERE AR_Action.EntityId = @EntityId)&lt;br /&gt;IF (@NoOfActions = @MaxActions)BEGIN SET @ErrorCode = -3 --Max no of actions exceeded GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_MetadataObject.IdFROM AR_ActionINNER JOIN AR_MetadataObjectON AR_Action.Id = AR_MetadataObject.IdWHERE AR_Action.EntityId = @EntityId AND AR_MetadataObject.Name = @Name&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;--For creating an Action we need to create two rows--one in the metadataobject table--and other in the actions tableINSERT INTO AR_MetadataObject(Name, IsCached) VALUES (@Name, @IsCached)SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @CreatedId = @@IDENTITYINSERT INTO AR_Action(Id, EntityId, Position, IsDisplayed, IsOpenedInNewWindow, Icon, Url) VALUES (@CreatedId, @EntityId, @Position, @IsDisplayed, @IsOpenedInNewWindow, @Icon, @Url)SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACL -- not individually securable --&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateActionParameter] Script Date: 06/23/2008 08:04:02 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateActionParameter] ( @Name nvarchar(50), @IsCached bit, @ActionId int, @Index tinyint, @CreatedId int OUTPUT, @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRAN&lt;br /&gt;-- Lock ActionParameter table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 13 -- action parameter table&lt;br /&gt;-- validate inputDECLARE @MaxActionParameters intDECLARE @NoOfActionParameters int&lt;br /&gt;SET @ErrorCode = 0SET @MaxActionParameters = 10&lt;br /&gt;SET @NoOfActionParameters = (SELECT count(*) FROM AR_ActionParameter WHERE AR_ActionParameter.ActionId = @ActionId)&lt;br /&gt;IF (@NoOfActionParameters = @MaxActionParameters)BEGIN SET @ErrorCode = -3 --Max no of actionparameters exceeded GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_MetadataObject.IdFROM AR_ActionParameterINNER JOIN AR_MetadataObjectON AR_ActionParameter.Id = AR_MetadataObject.IdWHERE AR_ActionParameter.ActionId = @ActionId AND AR_MetadataObject.Name = @Name&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;--For creating an ActionParamter we need to create two rows--one in the metadataobject table--and other in the actionparameters table&lt;br /&gt;INSERT INTO AR_MetadataObject(Name, IsCached) VALUES (@Name, @IsCached)SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @CreatedId = @@IDENTITYINSERT INTO AR_ActionParameterVALUES (@CreatedId, @ActionId, @Index)SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateAssociation] Script Date: 06/23/2008 08:04:02 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateAssociation] ( @Name nvarchar(255), @IsCached bit, @ReturnTypeDescriptorId int, @SourceEntityIds nvarchar(255), @DestinationEntityId int, @CreatedId int OUTPUT, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;DECLARE @MethodId intSELECT @MethodId=MethodId FROM AR_Parameter WHERE Id IN (SELECT ParameterId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@ReturnTypeDescriptorId"&gt;Id=@ReturnTypeDescriptorId&lt;/a&gt;)&lt;br /&gt;DECLARE @EntityId intSELECT @EntityId=ClassId FROM AR_Method WHERE &lt;a href="mailto:Id=@MethodId"&gt;Id=@MethodId&lt;/a&gt;&lt;br /&gt;DECLARE @SystemId int SELECT @SystemId=SystemId FROM AR_Class WHERE &lt;a href="mailto:Id=@EntityId"&gt;Id=@EntityId&lt;/a&gt;&lt;br /&gt;BEGIN TRANSACTION-- Lock Association table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 8 -- association table&lt;br /&gt;-- Check for duplicates; an association must be uniquely named within a SystemSELECT AR_Association.IdFROM AR_Association INNER JOIN AR_MethodInstanceON AR_Association.Id = AR_MethodInstance.IdINNER JOIN AR_MetadataObjectON AR_MethodInstance.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_MethodInstance.MethodId IN (SELECT Id FROM AR_Method WHERE ClassId IN (SELECT Id FROM AR_Class WHERE &lt;a href="mailto:SystemId=@SystemId"&gt;SystemId=@SystemId&lt;/a&gt;))&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraintsDECLARE @AssociationsAsDestinationForEntity intSET @AssociationsAsDestinationForEntity = (SELECT DISTINCT COUNT (AssociationId) FROM AR_AssociationMember WHERE EntityId = @DestinationEntityId AND EntityRole = 1)IF(@AssociationsAsDestinationForEntity &gt;= 1000)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND--IF(@EntityId != @DestinationEntityId)--BEGIN -- destination entity must be the one containing the method that provides the association functionality-- SET @ErrorCode = -400 -- GOTO ON_ERROR--END&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into MethodInstanceINSERT INTO AR_MethodInstance (Id, MethodId, Type, ReturnTypeDescriptorId)VALUES (@CreatedId, @MethodId, 4, @ReturnTypeDescriptorId)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Insert into AssociationINSERT INTO AR_Association (Id)VALUES (@CreatedId)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Insert Destination into AssociationMemberINSERT INTO AR_AssociationMember (AssociationId, EntityId, EntityRole)VALUES (@CreatedId, @DestinationEntityId, 1)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Insert Sources into AssociationMemberDECLARE @currentId nvarchar (255)DECLARE @index intDECLARE @oldIndex intSET @oldIndex = 0SET @index=CHARINDEX(',',@SourceEntityIds, @oldIndex)WHILE(@oldIndex &lt; currentid =" SUBSTRING(@SourceEntityIds," oldindex =" @index+1" associationsassourceforentity =" (SELECT" entityid =" @DestinationEntityId" entityrole =" 0)"&gt;= 1000) BEGIN SET @ErrorCode = -3 GOTO ON_ERROR END&lt;br /&gt;INSERT INTO AR_AssociationMember (AssociationId, EntityId, EntityRole) VALUES (@CreatedId, CAST(@currentId AS int), 0)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @index=CHARINDEX(',',@SourceEntityIds, @oldIndex)END&lt;br /&gt;-- Copy ACLEXEC proc_ar_CopyAccessControlEntriesForMetadataObjectId @EntityId, @CreatedId&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateEntity] Script Date: 06/23/2008 08:04:02 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateEntity] ( @Name nvarchar(255), @IsCached bit, @SystemId int, @EstimatedInstanceCount int, @CreatedId int OUTPUT, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock System table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 4 -- entity table&lt;br /&gt;-- Check for duplicatesSELECT AR_Entity.IdFROM AR_EntityINNER JOIN AR_ClassON AR_Entity.Id = AR_Class.IdINNER JOIN AR_MetadataObjectON AR_Class.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Class.SystemId = @SystemId&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @EntitiesForSystem intSET @EntitiesForSystem = (SELECT COUNT (AR_Entity.Id) FROM AR_Entity INNER JOIN AR_Class ON AR_Entity.Id = AR_Class.Id WHERE AR_Class.SystemId = @SystemId)IF(@EntitiesForSystem &gt;= 200)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERRORSET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into ClassINSERT INTO AR_Class (Id, SystemId) VALUES (@CreatedId, @SystemId)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Insert into EntityINSERT INTO AR_Entity (Id,EstimatedInstanceCount)VALUES (@CreatedId,@EstimatedInstanceCount)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACLEXEC proc_ar_CopyAccessControlEntriesForMetadataObjectId @SystemId, @CreatedId&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateFilterDescriptor] Script Date: 06/23/2008 08:04:03 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateFilterDescriptor] ( @Name nvarchar(255), @IsCached bit, @MethodId int, @TypeName nvarchar(255), @CreatedId int OUTPUT, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock FilterDescriptor table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 9 -- filterdescriptor table&lt;br /&gt;-- Check for duplicatesSELECT AR_FilterDescriptor.IdFROM AR_FilterDescriptorINNER JOIN AR_MetadataObjectON AR_FilterDescriptor.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_FilterDescriptor.MethodId = @MethodId&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @FilterDescriptorsForMethod intSET @FilterDescriptorsForMethod = (SELECT COUNT (Id) FROM AR_FilterDescriptor WHERE MethodId = @MethodId)IF( @FilterDescriptorsForMethod &gt;= 200)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERRORSET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into FilterDescriptorINSERT INTO AR_FilterDescriptor (Id, TypeName, MethodId)VALUES (@CreatedId, @TypeName, @MethodId)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACL -- not individually securable --&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateIdentifier] Script Date: 06/23/2008 08:04:03 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateIdentifier] ( @Name nvarchar(255), @IsCached bit, @EntityId int, @TypeName nvarchar(255), @CreatedId int OUTPUT, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock Identifier table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 5 -- identifier table&lt;br /&gt;-- Check for duplicatesSELECT AR_Identifier.IdFROM AR_IdentifierINNER JOIN AR_MetadataObjectON AR_Identifier.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Identifier.EntityId = @EntityId&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @IdentifiersForEntity intSET @IdentifiersForEntity = (SELECT COUNT(Id) FROM AR_Identifier WHERE EntityId = @EntityId)IF( @IdentifiersForEntity &gt;= 20)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @CreatedId = @@IDENTITY&lt;br /&gt;DECLARE @OrdinalNumber intIF EXISTS (SELECT TOP 1 * FROM AR_Identifier WHERE EntityId = @EntityId)BEGIN SELECT TOP 1 @OrdinalNumber = OrdinalNumber+1 FROM AR_Identifier WHERE EntityId = @EntityId ORDER BY OrdinalNumber DESCENDELSEBEGIN SET @OrdinalNumber = 0END&lt;br /&gt;-- Insert into IdentifierINSERT INTO AR_Identifier (Id, TypeName, EntityId, OrdinalNumber) VALUES (@CreatedId, @TypeName, @EntityId, @OrdinalNumber)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACL -- not individually securable --&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateMethod] Script Date: 06/23/2008 08:04:04 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateMethod] ( @Name nvarchar(255), @IsCached bit, @ClassId int, @IsStatic bit, @CreatedId int OUTPUT, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock System table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 6 -- method table&lt;br /&gt;-- Check for duplicatesSELECT AR_Method.IdFROM AR_MethodINNER JOIN AR_MetadataObjectON AR_Method.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Method.ClassId = @ClassId&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @MethodsForEntity intSET @MethodsForEntity = (SELECT COUNT (Id) FROM AR_Method WHERE ClassId = @ClassId)IF (@MethodsForEntity &gt;= 50)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into MethodINSERT INTO AR_Method (Id, ClassId, IsStatic) VALUES (@CreatedId, @ClassId, @IsStatic)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACLEXEC proc_ar_CopyAccessControlEntriesForMetadataObjectId @ClassId, @CreatedId&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateMethodInstance] Script Date: 06/23/2008 08:04:04 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateMethodInstance] ( @Name nvarchar(255), @IsCached bit, @MethodId int, @ReturnTypeDescriptorId int, @Type tinyint, @CreatedId int OUTPUT, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock MethodInstance table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 10 -- methodinstance table&lt;br /&gt;-- find the class/entity this methodinstance needs to belong toDECLARE @ClassId intSELECT @ClassId=ClassId FROM AR_Method WHERE Id = @MethodId&lt;br /&gt;-- Check for duplicate names in all MethodInstances in the entity/class that the given Method belongs toSELECT AR_MethodInstance.IdFROM AR_MethodInstanceINNER JOIN AR_MetadataObjectON AR_MethodInstance.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_MethodInstance.MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @MethodInstancesForMethod intSET @MethodInstancesForMethod = (SELECT COUNT (Id) FROM AR_MethodInstance WHERE MethodId = @MethodId)IF (@MethodInstancesForMethod &gt;= 15)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraintsIF(@Type = 1) -- 'Finder' BEGIN IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE Type=1 AND MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)) BEGIN -- object specific errors start at -100, -200 etc. SET @ErrorCode = -200 --More than 1 'Finder' method instance in entity GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@Type = 2) -- 'SpecificFinder' BEGIN IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE Type=2 AND MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)) BEGIN SET @ErrorCode = -201 --More than 1 'SpecificFinder' method instance in entity GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@Type = 5) -- 'IdEnumerator' BEGIN IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE Type=5 AND MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)) BEGIN SET @ErrorCode = -202 --More than 1 'IdEnumerator' method instance in entity GOTO ON_ERROR ENDEND&lt;br /&gt;DECLARE @ReturnTypeDescriptorMethodId intDECLARE @Direction intSELECT @ReturnTypeDescriptorMethodId=MethodId, @Direction=Direction FROM AR_Parameter WHERE Id IN (SELECT ParameterId FROM AR_TypeDescriptor WHERE Id = @ReturnTypeDescriptorId)IF(@ReturnTypeDescriptorMethodId != @MethodId)BEGIN SET @ErrorCode = -203 -- Return typedescriptor's parameter's method and this method instance's method are not the same GOTO ON_ERROREND&lt;br /&gt;IF(@Direction = 1) -- 1 = DirectionType.InBEGIN SET @ErrorCode = -204 -- Return typedescriptor's parameter's direction is DirectionType.In GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERRORSET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into MethodInstanceINSERT INTO AR_MethodInstance (Id, MethodId, ReturnTypeDescriptorId, Type)VALUES (@CreatedId, @MethodId, @ReturnTypeDescriptorId, @Type)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACL (from Class/Entity, not Method)-- The security ownership relationship is slightly different from the structural ownership relationshipEXEC proc_ar_CopyAccessControlEntriesForMetadataObjectId @ClassId, @CreatedId&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateParameter] Script Date: 06/23/2008 08:04:05 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateParameter] ( @Name nvarchar(255), @IsCached bit, @MethodId int, @Direction tinyint, @TypeReflectorTypeName nvarchar(255), @CreatedId int OUTPUT, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock Parameter table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 11 -- parameter table&lt;br /&gt;-- Check for duplicatesSELECT AR_Parameter.IdFROM AR_ParameterINNER JOIN AR_MetadataObjectON AR_Parameter.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Parameter.MethodId = @MethodId&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @ParametersForMethod intSET @ParametersForMethod = (SELECT COUNT (Id) FROM AR_Parameter WHERE MethodId = @MethodId)IF (@ParametersForMethod &gt;= 30)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraintsIF(@Direction = 4) --DirectionType.ReturnBEGIN IF EXISTS (SELECT Id FROM AR_Parameter WHERE MethodId = @MethodId AND Direction=4) BEGIN -- object specific errors start at -100, -200 etc. SET @ErrorCode = -100 --More than 1 'Return' parameter GOTO ON_ERROR ENDEND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @CreatedId = @@IDENTITY&lt;br /&gt;DECLARE @OrdinalNumber intIF EXISTS (SELECT TOP 1 * FROM AR_Parameter WHERE MethodId = @MethodId)BEGIN SELECT TOP 1 @OrdinalNumber = OrdinalNumber+1 FROM AR_Parameter WHERE MethodId = @MethodId ORDER BY OrdinalNumber DESCENDELSEBEGIN SET @OrdinalNumber = 0END&lt;br /&gt;-- Insert into FilterDescriptorINSERT INTO AR_Parameter (Id, MethodId, TypeReflectorTypeName, Direction, OrdinalNumber)VALUES (@CreatedId, @MethodId, @TypeReflectorTypeName, @Direction, @OrdinalNumber)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACL -- not individually securable --&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateSystem] Script Date: 06/23/2008 08:04:05 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateSystem] ( @Name nvarchar(255), @IsCached bit, @SystemUtilityTypeName nvarchar(255), @ConnectionManagerTypeName nvarchar(255), @EntityInstanceTypeName nvarchar(255), @CreatedId int OUTPUT, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock System table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 2 -- system table&lt;br /&gt;-- Check for duplicatesSELECT AR_System.IdFROM AR_SystemINNER JOIN AR_MetadataObjectON AR_System.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERRORSET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into SystemINSERT INTO AR_System (Id, SystemUtilityTypeName, ConnectionFactoryTypeName, SystemEntityTypeName) VALUES (@CreatedId, @SystemUtilityTypeName, @ConnectionManagerTypeName, @EntityInstanceTypeName)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACLDECLARE @SourceMetadataObjectId intSELECT @SourceMetadataObjectId = Id FROM AR_ApplicationRegistryEXEC proc_ar_CopyAccessControlEntriesForMetadataObjectId @SourceMetadataObjectId, @CreatedId&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateSystemInstance] Script Date: 06/23/2008 08:04:05 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateSystemInstance] ( @Name nvarchar(255), @IsCached bit, @SystemId int, @CreatedId int OUTPUT, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock SystemInstance table for concurrent inserts or updatesSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 3 -- systeminstance table&lt;br /&gt;-- Check for duplicates globallySELECT AR_SystemInstance.IdFROM AR_SystemInstanceINNER JOIN AR_MetadataObjectON AR_SystemInstance.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @SystemInstancesForSystem intSET @SystemInstancesForSystem = (SELECT COUNT (Id) FROM AR_SystemInstance WHERE SystemId = @SystemId)IF( @SystemInstancesForSystem &gt;= 300)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;SET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into SystemInstanceINSERT INTO AR_SystemInstance (Id, SystemId) VALUES (@CreatedId, @SystemId)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Copy ACLEXEC proc_ar_CopyAccessControlEntriesForMetadataObjectId @SystemId, @CreatedId&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_CreateTypeDescriptor] Script Date: 06/23/2008 08:04:06 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_CreateTypeDescriptor] ( @Name nvarchar(255), @IsCached bit, @ParameterId int, @ParentTypeDescriptorId int, @TypeName nvarchar(255), @IdentifierId int, @FilterDescriptorId int, @IsCollection bit, @CreatedId int OUTPUT, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock TypeDescriptor table for concurrent insertsSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 12 -- typedescriptor table&lt;br /&gt;-- Check for duplicatesSELECT AR_TypeDescriptor.IdFROM AR_TypeDescriptorINNER JOIN AR_MetadataObjectON AR_TypeDescriptor.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_TypeDescriptor.ParentTypeDescriptorId = @ParentTypeDescriptorId AND AR_TypeDescriptor.ParameterId = @ParameterId&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @ChildTypeDescriptors intDECLARE @TypeDescriptorsRelatedToFilterDescriptor intSET @ChildTypeDescriptors = (SELECT COUNT (Id) FROM AR_TypeDescriptor WHERE ParentTypeDescriptorId = @ParentTypeDescriptorId AND ParameterId = @ParameterId AND @ParentTypeDescriptorId IS NOT NULL)SET @TypeDescriptorsRelatedToFilterDescriptor = (SELECT COUNT (Id) FROM AR_TypeDescriptor WHERE FilterDescriptorId = @FilterDescriptorId AND @FilterDescriptorId IS NOT NULL)IF (@ChildTypeDescriptors &gt;= 300 OR @TypeDescriptorsRelatedToFilterDescriptor &gt;= 10)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraintsDECLARE @ParentIsCollection bitIF(@IsCollection=1 AND @ParentTypeDescriptorId IS NOT NULL)BEGIN SELECT @ParentIsCollection=IsCollection FROM AR_TypeDescriptor WHERE Id = @ParentTypeDescriptorId IF (@ParentIsCollection = 1) BEGIN SET @ErrorCode = -305 -- cannot have parent-child typedescriptors with IsCollection set GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@ParentTypeDescriptorId IS NOT NULL AND EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE ParentTypeDescriptorId = @ParentTypeDescriptorId)) -- if there is a siblingBEGIN SELECT @ParentIsCollection=IsCollection FROM AR_TypeDescriptor WHERE Id = @ParentTypeDescriptorId IF(@ParentIsCollection = 1) BEGIN SET @ErrorCode = -306 -- a type desc with IsCollection can have only one child; do not create a typedesc if the parent has isCollection set and already has a child GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@ParentTypeDescriptorId IS NULL AND EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE &lt;a href="mailto:ParameterId=@ParameterId"&gt;ParameterId=@ParameterId&lt;/a&gt; AND ParentTypeDescriptorId IS NULL))BEGIN SET @ErrorCode = -302 -- cannot have multiple root type descriptors for a single parameter GOTO ON_ERROREND&lt;br /&gt;DECLARE @FilterDescriptorMethodId intDECLARE @TypeDescriptorMethodId intSELECT @FilterDescriptorMethodId=MethodId FROM AR_FilterDescriptor WHERE &lt;a href="mailto:Id=@FilterDescriptorId"&gt;Id=@FilterDescriptorId&lt;/a&gt;SELECT @TypeDescriptorMethodId=MethodId FROM AR_Parameter WHERE &lt;a href="mailto:Id=@ParameterId"&gt;Id=@ParameterId&lt;/a&gt;IF(@FilterDescriptorId IS NOT NULL AND @TypeDescriptorMethodId != @FilterDescriptorMethodId)BEGIN SET @ErrorCode = -303 -- filter doesn't belong to the same method as the typedescriptor GOTO ON_ERROREND&lt;br /&gt;-- Insert into MetadataObjectINSERT INTO AR_MetadataObject (Name, IsCached) VALUES (@Name, @IsCached)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERRORSET @CreatedId = @@IDENTITY&lt;br /&gt;-- Insert into TypeDescriptorINSERT INTO AR_TypeDescriptor (Id, ParentTypeDescriptorId, ParameterId, TypeName, InterpretedTypeName, ContainsIdentifier, IdentifierId, ContainsFilterDescriptor, FilterDescriptorId, IsCollection)VALUES (@CreatedId, @ParentTypeDescriptorId, @ParameterId, @TypeName, @TypeName, 0, @IdentifierId, 0, @FilterDescriptorId, @IsCollection)&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Propogate Contains* flagsEXEC proc_ar_TypeDescriptorContainsFlagsFixup @CreatedId, @ErrorCode OUTPUTIF(@ErrorCode != 0) GOTO ON_ERRORUPDATE AR_MetadataObject SET Version=0 WHERE &lt;a href="mailto:Id=@CreatedId"&gt;Id=@CreatedId&lt;/a&gt; -- reset any version increment caused by fixup&lt;br /&gt;-- Copy ACL -- not individually securable --&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteActionById] Script Date: 06/23/2008 08:04:06 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteActionById] ( @Id int, @Version int, @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRAN&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;-- Lock Action table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent deleteSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (7,13) -- action, action parameter&lt;br /&gt;--before deleting make sure that the action existsSELECT IdFROM AR_ActionWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE AR_Action.ID = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --action not found GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- Delete the action itself and cascade delete action parametersDELETE FROM AR_Metadataobject WHERE AR_Metadataobject.Id IN (SELECT Id FROM AR_ActionParameter WHERE ActionId = @Id)SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;DELETE FROM AR_Metadataobject WHERE AR_Metadataobject.id = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteActionParameterById] Script Date: 06/23/2008 08:04:07 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteActionParameterById] ( @Id int, @Version int, @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRAN&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;-- Lock ActionParameter table for concurrent deletesSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (13) -- action parameter&lt;br /&gt;-- before deleting make sure that the actionparameter existsSELECT IdFROM AR_ActionParameterWHERE AR_ActionParameter.ID = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --actionparameter not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objects -- there are no child objects&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_Metadataobject WHERE Id = @Id&lt;br /&gt;COMMIT TRANRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteAssociationById] Script Date: 06/23/2008 08:04:07 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteAssociationById] ( @Id int, @Version int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Lock Association table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent delete.SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (8) -- association&lt;br /&gt;-- before deleting make sure Association existsSELECT IdFROM AR_AssociationWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objects -- no child objects --&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_DefaultValueWHERE AR_DefaultValue.MethodInstanceId = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;DELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteDefaultValue] Script Date: 06/23/2008 08:04:07 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteDefaultValue] ( @TypeDescriptorId int, @MethodInstanceId int, @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;-- ensure the MethodInstance and TypeDescriptor existIF NOT EXISTS (SELECT Id FROM AR_MethodInstance WHERE Id = @MethodInstanceId)BEGIN SET @ErrorCode = -2 GOTO ON_ERRORENDIF NOT EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE Id = @TypeDescriptorId)BEGIN SET @ErrorCode = -2 GOTO ON_ERROREND&lt;br /&gt;-- do the workDELETE FROM AR_DefaultValue WHERE MethodInstanceId = @MethodInstanceId AND TypeDescriptorId = @TypeDescriptorId&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteEntityById] Script Date: 06/23/2008 08:04:08 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteEntityById] ( @Id int, @Version int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Lock Entity table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent deleteSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (4,5,6,7,8) -- entity, identifier, method, action, association&lt;br /&gt;-- before deleting make sure Entity existsSELECT IdFROM AR_EntityWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE AR_Entity.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objectsSELECT IdFROM AR_AssociationMemberWHERE EntityId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;SELECT IdFROM AR_ActionWHERE EntityId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;SELECT IdFROM AR_MethodWHERE ClassId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;SELECT IdFROM AR_IdentifierWHERE EntityId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- may be safe here to delete non-cascading children that are -- NOT individually securable by calling the appropriate child-- delete stored proc (which in turn must make sure it has no -- individuallysecurable children before succeeding), but we'll -- opt for simplicity where all delete cascades are handled in the OM&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteFilterDescriptorById] Script Date: 06/23/2008 08:04:08 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteFilterDescriptorById] ( @Id int, @Version int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Lock FilterDescriptor table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent deleteSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (9,12) -- filterdescriptor, typedescriptor&lt;br /&gt;-- before deleting make sure FilterDescriptor existsSELECT IdFROM AR_FilterDescriptorWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE AR_FilterDescriptor.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objects -- there are no child objects&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- update TypeDescriptor references DECLARE tdFdCursor CURSOR LOCALFORWARD_ONLYKEYSETFOR SELECT AR_TypeDescriptor.Id FROM AR_TypeDescriptor WHERE AR_TypeDescriptor.FilterDescriptorId = @Id&lt;br /&gt;OPEN tdFdCursor&lt;br /&gt;DECLARE @currentId int&lt;br /&gt;FETCH NEXT FROM tdFdCursor INTO @currentIdWHILE(@@FETCH_STATUS=0)BEGIN -- important to run the stored proc as opposed -- to doing the updates directly, as the proc can -- take care of incrementing the version counter -- (to invalidate TypeDesc objects that may -- already exist) and other details UPDATE AR_TypeDescriptor SET FilterDescriptorId = NULL WHERE &lt;a href="mailto:Id=@currentId"&gt;Id=@currentId&lt;/a&gt; EXEC proc_ar_TypeDescriptorContainsFlagsFixup @currentId, @ErrorCode OUTPUT IF(@ErrorCode != 0) GOTO ON_ERROR FETCH NEXT FROM tdFdCursor INTO @currentIdENDDEALLOCATE tdFdCursor&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteIdentifierById] Script Date: 06/23/2008 08:04:08 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteIdentifierById] ( @Id int, @Version int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Lock Identifier table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent delete. Also-- want to ensure ordinal reordering is serialized in case-- of concurrent delete requests to IdentifiersSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (5,12) -- type descriptor&lt;br /&gt;-- before deleting make sure Identifier existsSELECT IdFROM AR_IdentifierWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE AR_Identifier.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objects -- there are no child objects&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- update TypeDescriptor references DECLARE tdIdCursor CURSOR LOCALFORWARD_ONLYKEYSETFOR SELECT AR_TypeDescriptor.Id FROM AR_TypeDescriptor WHERE AR_TypeDescriptor.IdentifierId = @Id&lt;br /&gt;OPEN tdIdCursor&lt;br /&gt;DECLARE @currentId int&lt;br /&gt;FETCH NEXT FROM tdIdCursor INTO @currentIdWHILE(@@FETCH_STATUS=0)BEGIN -- important to run the stored proc as opposed -- to doing the updates directly, as the proc can -- take care of incrementing the version counter -- (to invalidate TypeDesc objects that may -- already exist) and other details UPDATE AR_TypeDescriptor SET IdentifierId = NULL WHERE &lt;a href="mailto:Id=@currentId"&gt;Id=@currentId&lt;/a&gt; EXEC proc_ar_TypeDescriptorContainsFlagsFixup @currentId, @ErrorCode OUTPUT IF(@ErrorCode != 0) GOTO ON_ERROR FETCH NEXT FROM tdIdCursor INTO @currentIdENDDEALLOCATE tdIdCursor&lt;br /&gt;-- save key fields for ordinal number reordering DECLARE @EntityId intSELECT @EntityId = EntityId FROM AR_Identifier WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- reorder OrdinalsDECLARE ordinalNumberCursor CURSOR LOCALFORWARD_ONLYKEYSETFOR SELECT OrdinalNumber FROM AR_Identifier WHERE EntityId = @EntityId ORDER BY OrdinalNumber ASCFOR UPDATE OF OrdinalNumber&lt;br /&gt;OPEN ordinalNumberCursor&lt;br /&gt;DECLARE @OrdinalNumberCounter tinyintSET @OrdinalNumberCounter = 0FETCH NEXT FROM ordinalNumberCursorWHILE(@@FETCH_STATUS=0)BEGIN UPDATE AR_Identifier SET &lt;a href="mailto:OrdinalNumber=@OrdinalNumberCounter"&gt;OrdinalNumber=@OrdinalNumberCounter&lt;/a&gt; WHERE CURRENT OF ordinalNumberCursor&lt;br /&gt;EXEC proc_ar_BumpMetadataObjectVersionCounter @Id&lt;br /&gt;SET @OrdinalNumberCounter = @OrdinalNumberCounter + 1 FETCH NEXT FROM ordinalNumberCursorENDDEALLOCATE ordinalNumberCursor&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteLocalizedNameForMetadataObjectByLCID] Script Date: 06/23/2008 08:04:08 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteLocalizedNameForMetadataObjectByLCID] ( @MetadataObjectId int, @LCID int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTIONDELETE FROM AR_LocalizedNameWITH (HOLDLOCK)WHERE MetadataObjectId = @MetadataObjectId AND LCID = @LCID&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 1)BEGIN SET @ErrorCode = -2 GOTO ON_ERROREND&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteLocalizedNamesByMetadataObjectId] Script Date: 06/23/2008 08:04:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteLocalizedNamesByMetadataObjectId] ( @MetadataObjectId int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTIONDELETE FROM AR_LocalizedNameWHERE MetadataObjectId = @MetadataObjectId&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteMethodById] Script Date: 06/23/2008 08:04:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteMethodById] ( @Id int, @Version int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Lock Method table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent deleteSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (6,9) -- method, filter descriptor&lt;br /&gt;-- before deleting make sure Method existsSELECT IdFROM AR_MethodWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE AR_Method.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objectsSELECT IdFROM AR_FilterDescriptorWHERE MethodId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;SELECT IdFROM AR_MethodInstanceWHERE MethodId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;SELECT IdFROM AR_ParameterWHERE MethodId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- may be safe here to delete non-cascading children that are -- NOT individually securable by calling the appropriate child-- delete stored proc (which in turn must make sure it has no -- individuallysecurable children before succeeding), but we'll -- opt for simplicity where all delete cascades are handled in the OM&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteMethodInstanceById] Script Date: 06/23/2008 08:04:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteMethodInstanceById] ( @Id int, @Version int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- lock method instanceSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 10 -- methodinstance table&lt;br /&gt;-- before deleting make sure MethodInstance existsSELECT IdFROM AR_MethodInstanceWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objects -- no child objects --&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- may be safe here to delete non-cascading children that are -- NOT individually securable by calling the appropriate child-- delete stored proc (which in turn must make sure it has no -- individuallysecurable children before succeeding), but we'll -- opt for simplicity where all delete cascades are handled in the OM&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_DefaultValueWHERE AR_DefaultValue.MethodInstanceId = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;DELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteParameterById] Script Date: 06/23/2008 08:04:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteParameterById] ( @Id int, @Version int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Lock Parameter table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent delete.-- We also don't want concurrent parameter deletes, because -- a parameter deletion causes an ordinal reordering -- which needs to be serializedSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (11,12) -- parameter, type descriptor&lt;br /&gt;-- before deleting make sure Parameter existsSELECT IdFROM AR_ParameterWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE AR_Parameter.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objectsSELECT IdFROM AR_TypeDescriptorWHERE ParameterId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- save key fields for ordinal number reordering DECLARE @MethodId intSELECT @MethodId = MethodId FROM AR_Parameter WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- reorder OrdinalsDECLARE ordinalNumberCursor CURSOR LOCALFORWARD_ONLYKEYSETFOR SELECT OrdinalNumber FROM AR_Parameter WHERE MethodId = @MethodId ORDER BY OrdinalNumber ASCFOR UPDATE OF OrdinalNumber&lt;br /&gt;OPEN ordinalNumberCursor&lt;br /&gt;DECLARE @OrdinalNumberCounter tinyintSET @OrdinalNumberCounter = 0FETCH NEXT FROM ordinalNumberCursorWHILE(@@FETCH_STATUS=0)BEGIN UPDATE AR_Parameter SET &lt;a href="mailto:OrdinalNumber=@OrdinalNumberCounter"&gt;OrdinalNumber=@OrdinalNumberCounter&lt;/a&gt; WHERE CURRENT OF ordinalNumberCursor&lt;br /&gt;EXEC proc_ar_BumpMetadataObjectVersionCounter @Id&lt;br /&gt;SET @OrdinalNumberCounter = @OrdinalNumberCounter + 1 FETCH NEXT FROM ordinalNumberCursorENDDEALLOCATE ordinalNumberCursor&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeletePropertiesById] Script Date: 06/23/2008 08:04:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeletePropertiesById] ( @MetadataObjectId int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTIONDELETE FROM AR_PropertyWHERE AR_Property.MetadataObjectId = @MetadataObjectId&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeletePropertyForMetadataObjectId] Script Date: 06/23/2008 08:04:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeletePropertyForMetadataObjectId] ( @MetadataObjectId int, @Name nvarchar (255), @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTIONDELETE FROM AR_PropertyWITH (HOLDLOCK)WHERE AR_Property.MetadataObjectId = @MetadataObjectId AND AR_Property.Name = @Name&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 1)BEGIN SET @ErrorCode = -2 GOTO ON_ERROREND&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteSystemById] Script Date: 06/23/2008 08:04:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteSystemById] ( @Id int, @Version int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;-- Lock System table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent deleteSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (2,3,4) -- system, systeminstance and entity tables&lt;br /&gt;-- before deleting make sure System existsSELECT IdFROM AR_SystemWITH (HOLDLOCK) -- guarantee no one else deletes the same System WHERE AR_System.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objectsSELECT IdFROM AR_SystemInstanceWHERE AR_SystemInstance.SystemId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;SELECT IdFROM AR_ClassWHERE AR_Class.SystemId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- delete non-cascading children -- there are none --&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteSystemInstanceById] Script Date: 06/23/2008 08:04:11 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteSystemInstanceById] ( @Id int, @Version int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock SystemInstance table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent deleteSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 3 -- systeminstance table&lt;br /&gt;-- before deleting make sure SystemInstance existsSELECT IdFROM AR_SystemInstanceWITH (HOLDLOCK) -- guarantee no one else deletes the same SystemInstanceWHERE AR_SystemInstance.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objects-- (SystemInstance has no child objects)&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- delete non-cascading children -- there are none --&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteTypeDescriptorById] Script Date: 06/23/2008 08:04:11 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteTypeDescriptorById] ( @Id int, @Version int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;-- BEGIN TRANSACION; allow nested invocationDECLARE @TranCount int SET @TranCount=@@TRANCOUNT IF(@@TRANCOUNT=0) BEGIN TRANSACTION&lt;br /&gt;-- Lock TypeDescriptor table for concurrent deletes; want to -- guarantee that no one will insert a child object-- between the time we check for child objects and -- decide to go ahead or abort the parent deleteSELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId IN (10, 12) -- methodinstance, typedescriptor&lt;br /&gt;-- before deleting make sure TypeDescriptor existsSELECT IdFROM AR_TypeDescriptorWITH (HOLDLOCK) -- guarantee no one else deletes the same objectWHERE AR_TypeDescriptor.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --object not found GOTO ON_ERROREND&lt;br /&gt;-- before deleting make sure there are no child objectsSELECT IdFROM AR_MethodInstanceWHERE ReturnTypeDescriptorId = @Id&lt;br /&gt;IF (@@ROWCOUNT &gt; 0)BEGIN SET @ErrorCode = -5 --child objects found GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWITH (HOLDLOCK) -- now no one can update this before we deleteWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version AND @Version != -1)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;-- the delete is *really* expensive because of all the cascading constraints. We want -- to do all deletes together as a result; we'll collate them in this temp tableIF NOT EXISTS (select * from tempdb..sysobjects where id = object_id(N'tempdb..##__DeletedTypeDescriptorList'))BEGIN CREATE TABLE ##__DeletedTypeDescriptorList ( [Id] [int] NOT NULL, [SessionId] [int] NOT NULL ) ON [PRIMARY]&lt;br /&gt;ALTER TABLE ##__DeletedTypeDescriptorList WITH NOCHECK ADD CONSTRAINT [PK] PRIMARY KEY CLUSTERED ( [Id] ) ON [PRIMARY] END&lt;br /&gt;-- delete all typedescriptors; use the root typedescriptor as the 'session'. -- The session allows the same tmp table to be used by concurrent delete operations -- (even though we don't allow concurrent deletes in general; this is more robust)EXEC proc_ar_DeleteTypeDescriptorByIdInternal @Id, @Id, -1, @ErrorCode OUTPUT -- -1 ==&gt; ignore version checkIF( @ErrorCode != 0) GOTO ON_ERROR&lt;br /&gt;-- save parent before deletingDECLARE @parentId intSELECT @parentId=ParentTypeDescriptorId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;&lt;br /&gt;-- Perform delete on collected TypeDescriptors DELETE FROM AR_MetadataObject WHERE AR_MetadataObject.Id IN (SELECT Id FROM ##__DeletedTypeDescriptorList WHERE SessionId = @Id)SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- fixup ParentTypeDescriptorEXEC proc_ar_TypeDescriptorContainsFlagsFixup @parentId, @ErrorCode OUTPUTIF(@ErrorCode != 0) GOTO ON_ERROR&lt;br /&gt;-- reset the DeletedTypeDescriptorListDELETE FROM ##__DeletedTypeDescriptorList WHERE SessionId = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;IF(@TranCount &lt; @@TRANCOUNT) COMMIT TRANSACTIONRETURN ON_ERROR:IF(@TranCount &lt; @@TRANCOUNT) ROLLBACK TRANSACTIONRETURN GO/****** Object: StoredProcedure [dbo].[proc_ar_DeleteTypeDescriptorByIdInternal] Script Date: 06/23/2008 08:04:11 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_DeleteTypeDescriptorByIdInternal] ( @Id int, @SessionId int, @Version int, @ErrorCode int OUTPUT)AS SET @ErrorCode = 0 -- before deleting delete child objects-- (ArshishK: I considered several hierarchical SQL representation models -- here (nested sets, enumerated lineage, etc.). I'm adopting the most -- intuitive and easy to write one here - admin OM perf is not our design-- point; the runtime OM will be fast, and that's all that matters)DECLARE @ParameterId intSELECT @ParameterId=ParameterId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;&lt;br /&gt;DECLARE childCursor CURSOR LOCALFORWARD_ONLYKEYSETFOR SELECT Id FROM AR_TypeDescriptor WHERE ParameterId = @ParameterId AND ParentTypeDescriptorId = @Id&lt;br /&gt;OPEN childCursorDECLARE @childId intFETCH NEXT FROM childCursor INTO @childIdWHILE(@@FETCH_STATUS=0)BEGIN EXEC proc_ar_DeleteTypeDescriptorByIdInternal @childId, @SessionId, -1, @ErrorCode OUTPUT -- -1 ==&gt; ignore version check IF( @ErrorCode != 0) BEGIN DEALLOCATE childCursor GOTO ON_ERROR END FETCH NEXT FROM childCursor INTO @childIdENDDEALLOCATE childCursor&lt;br /&gt;-- cascading deletes will take care of other tablesDELETE FROM AR_DefaultValueWHERE AR_DefaultValue.TypeDescriptorId = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- save this in the list of candidates to be deleted in one swoopINSERT INTO ##__DeletedTypeDescriptorList (Id, SessionId) VALUES (@Id, @SessionId)&lt;br /&gt;RETURN&lt;br /&gt;ON_ERROR:RETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_EnsureApplicationRegistryExists] Script Date: 06/23/2008 08:04:12 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_EnsureApplicationRegistryExists]AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;IF NOT EXISTS (SELECT * FROM AR_ApplicationRegistry)BEGIN DECLARE @StaticAppRegId int INSERT INTO AR_MetadataObject (Name,IsCached) VALUES ('ApplicationRegistry',1) SET @StaticAppRegId = @@IDENTITY INSERT INTO AR_ApplicationRegistry (Id) VALUES (@StaticAppRegId)&lt;br /&gt;INSERT INTO AR_MetadataObjectSecurity (MetadataObjectId, IdentityName, DisplayName, Rights) VALUES (@StaticAppRegId, 'domainUser', 'domainUser', 31)END&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAccessControlEntriesForMetadataObject] Script Date: 06/23/2008 08:04:12 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAccessControlEntriesForMetadataObject] ( @MetadataObjectId [int], @ErrorCode [int] OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;IF EXISTS (SELECT * FROM AR_MetadataObject WHERE Id = @MetadataObjectId)BEGIN&lt;br /&gt;SELECT MetadataObjectId, IdentityName, DisplayName, RawSid, Rights FROM AR_MetadataObjectSecurity WHERE MetadataObjectId = @MetadataObjectIdENDELSEBEGIN SET @ErrorCode = -2END&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetActionById] Script Date: 06/23/2008 08:04:12 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetActionById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_Action.[Id], AR_Action.EntityId, AR_Action.Position, AR_Action.IsDisplayed, AR_Action.IsOpenedInNewWindow, AR_Action.Icon, AR_Action.Url, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_ActionINNER JOIN AR_MetadataObjectON AR_Action.Id = AR_MetadataObject.IdWHERE AR_Action.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetActionParameterById] Script Date: 06/23/2008 08:04:12 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetActionParameterById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_ActionParameter.[Id], AR_ActionParameter.ActionId, AR_ActionParameter.[Index], AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_ActionParameterINNER JOIN AR_MetadataObjectON AR_ActionParameter.Id = AR_MetadataObject.IdWHERE AR_ActionParameter.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetActionParametersForActionWithCount] Script Date: 06/23/2008 08:04:12 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetActionParametersForActionWithCount] ( @ActionId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_ActionParameterINNER JOIN AR_MetadataObjectON AR_ActionParameter.Id = AR_MetadataObject.IdWHERE AR_ActionParameter.ActionId = @ActionId&lt;br /&gt;SELECT AR_ActionParameter.[Id], AR_ActionParameter.ActionId, AR_ActionParameter.[Index], AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_ActionParameterON AR_ActionParameter.Id = AR_MetadataObject.Id WHERE AR_ActionParameter.ActionId = @ActionIdORDER BY AR_ActionParameter.[Index]ASCCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetActionsForEntityWithCount] Script Date: 06/23/2008 08:04:13 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetActionsForEntityWithCount] ( @EntityId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_ActionINNER JOIN AR_MetadataObjectON AR_Action.Id = AR_MetadataObject.IdWHERE AR_Action.EntityId = @EntityId&lt;br /&gt;SELECT AR_Action.[Id], AR_Action.EntityId, AR_Action.Position, AR_Action.IsDisplayed, AR_Action.IsOpenedInNewWindow, AR_Action.Icon, AR_Action.Url, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_ActionON AR_Action.Id = AR_MetadataObject.Id WHERE AR_Action.EntityId = @EntityIdORDER BY AR_Action.PositionASCCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAllLocalizedNamesForMetadataObjectWithCount] Script Date: 06/23/2008 08:04:13 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAllLocalizedNamesForMetadataObjectWithCount] ( @MetadataObjectId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT( * ) FROM AR_LocalizedNameWHERE AR_LocalizedName.MetadataObjectId = @MetadataObjectId&lt;br /&gt;SELECT AR_LocalizedName.[Id], AR_LocalizedName.LCID, AR_LocalizedName.LocalizedName, AR_LocalizedName.MetadataObjectId FROM AR_LocalizedNameWHERE AR_LocalizedName.MetadataObjectId = @MetadataObjectIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAllSystemInstancesLikeNameWithCount] Script Date: 06/23/2008 08:04:13 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAllSystemInstancesLikeNameWithCount] ( @SystemInstanceName nvarchar(255), @LCID int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT DISTINCT COUNT( AR_SystemInstance.[Id] ) FROM AR_SystemInstanceINNER JOIN AR_MetadataObjectON AR_SystemInstance.Id = AR_MetadataObject.IdLEFT JOIN AR_LocalizedNameON AR_MetadataObject.Id = AR_LocalizedName.MetadataObjectIdWHERE UPPER(AR_MetadataObject.Name) LIKE @SystemInstanceName OR (UPPER(AR_LocalizedName.LocalizedName) LIKE @SystemInstanceName AND (AR_LocalizedName.LCID = @LCID OR AR_LocalizedName.LCID = 0))&lt;br /&gt;SELECT DISTINCT AR_SystemInstance.[Id], AR_SystemInstance.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.Version FROM AR_SystemInstanceINNER JOIN AR_MetadataObject ON AR_SystemInstance.Id = AR_MetadataObject.IdLEFT JOIN AR_LocalizedNameON AR_MetadataObject.Id = AR_LocalizedName.MetadataObjectIdWHERE UPPER(AR_MetadataObject.Name) LIKE @SystemInstanceName OR (UPPER(AR_LocalizedName.LocalizedName) LIKE @SystemInstanceName AND (AR_LocalizedName.LCID = @LCID OR AR_LocalizedName.LCID = 0))&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAllSystemInstancesWithCount] Script Date: 06/23/2008 08:04:13 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAllSystemInstancesWithCount]AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT( * ) FROM AR_SystemInstance&lt;br /&gt;SELECT AR_SystemInstance.[Id], AR_SystemInstance.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.Version FROM AR_SystemInstanceINNER JOIN AR_MetadataObject ON AR_SystemInstance.Id = AR_MetadataObject.IdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAllSystemsWithCount] Script Date: 06/23/2008 08:04:14 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAllSystemsWithCount]AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT( * ) FROM AR_System&lt;br /&gt;SELECT AR_System.[Id], AR_System.SystemUtilityTypeName, AR_System.ConnectionFactoryTypeName, AR_System.SystemEntityTypeName, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_SystemINNER JOIN AR_MetadataObject ON AR_System.Id = AR_MetadataObject.IdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAssociationById] Script Date: 06/23/2008 08:04:14 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAssociationById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_Association.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_Association.Id = AR_MethodInstance.IdINNER JOIN AR_MetadataObject ON AR_MethodInstance.Id = AR_MetadataObject.IdWHERE AR_Association.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAssociationByName] Script Date: 06/23/2008 08:04:14 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAssociationByName] ( @AssociationName [nvarchar] (255), @SystemId int) AS&lt;br /&gt;SELECT AR_Association.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_MethodInstance.Id = AR_Association.IdINNER JOIN AR_MetadataObjectON AR_MetadataObject.Id = AR_MethodInstance.IdWHERE AR_MetadataObject.Id IN (SELECT DISTINCT AR_Association.ID FROM AR_Association INNER JOIN AR_AssociationMember ON AR_Association.Id = AR_AssociationMember.AssociationId INNER JOIN AR_Class ON AR_AssociationMember.EntityId = AR_Class.Id INNER JOIN AR_System ON AR_Class.SystemId = AR_System.Id WHERE AR_System.Id = @SystemId AND AR_Association.Id IN (SELECT AR_Association.Id FROM AR_MetadataObject, AR_Association WHERE &lt;a href="mailto:AR_MetadataObject.Name=@AssociationName"&gt;AR_MetadataObject.Name=@AssociationName&lt;/a&gt; AND AR_MetadataObject.Id = AR_Association.Id) )&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAssociationsForDataClassWithCount] Script Date: 06/23/2008 08:04:14 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAssociationsForDataClassWithCount] ( @ClassId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_Association.Id = AR_MethodInstance.Id WHERE AR_MethodInstance.MethodId IN (SELECT AR_Method.[Id] AS Method FROM AR_Method WHERE AR_Method.ClassId = @ClassId)&lt;br /&gt;SELECT AR_Association.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_Association.Id = AR_MethodInstance.Id INNER JOIN AR_MetadataObjectON AR_MethodInstance.Id = AR_MetadataObject.IdWHERE AR_MethodInstance.MethodId IN (SELECT AR_Method.[Id] AS Method FROM AR_Method WHERE AR_Method.ClassId = @ClassId)&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAssociationsForEntityAndRoleWithCount] Script Date: 06/23/2008 08:04:14 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAssociationsForEntityAndRoleWithCount] ( @EntityId int, @EntityRole bit) AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT( * )FROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_MethodInstance.Id = AR_Association.IdINNER JOIN AR_MetadataObjectON AR_MetadataObject.Id = AR_MethodInstance.IdWHERE AR_Association.Id IN ( SELECT AR_AssociationMember.AssociationId FROM AR_AssociationMember WHERE AR_AssociationMember.EntityId = @EntityId AND AR_AssociationMember.EntityRole = @EntityRole)&lt;br /&gt;SELECT AR_Association.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_MethodInstance.Id = AR_Association.IdINNER JOIN AR_MetadataObjectON AR_MetadataObject.Id = AR_MethodInstance.IdWHERE AR_Association.Id IN ( SELECT AR_AssociationMember.AssociationId FROM AR_AssociationMember WHERE AR_AssociationMember.EntityId = @EntityId AND AR_AssociationMember.EntityRole = @EntityRole)&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetAssociationsForMethodWithCount] Script Date: 06/23/2008 08:04:15 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetAssociationsForMethodWithCount] ( @MethodId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_Association.Id = AR_MethodInstance.Id WHERE AR_MethodInstance.MethodId = @MethodId&lt;br /&gt;SELECT AR_Association.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_AssociationINNER JOIN AR_MethodInstanceON AR_Association.Id = AR_MethodInstance.Id INNER JOIN AR_MetadataObjectON AR_MethodInstance.Id = AR_MetadataObject.IdWHERE AR_MethodInstance.MethodId = @MethodId&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetCacheInvalidationCountersWithCount] Script Date: 06/23/2008 08:04:15 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetCacheInvalidationCountersWithCount] AS&lt;br /&gt;SELECT COUNT( * ) FROM AR_CacheCountersWITH (NOLOCK) -- number of rows will never change; each row contains independent data; dirty reads ok; no need to encase in xact&lt;br /&gt;SELECT MetadataObjectType, ObjectCacheCounter, RelationshipCacheCounterFROM AR_CacheCountersWITH (NOLOCK)ORDER BY MetadataObjectType&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetChildTypeDescriptorsForTypeDescriptorWithCount] Script Date: 06/23/2008 08:04:15 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetChildTypeDescriptorsForTypeDescriptorWithCount] ( @ParentTypeDescriptorId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT (*)FROM AR_MetadataObjectINNER JOIN AR_TypeDescriptorON AR_TypeDescriptor.Id = AR_MetadataObject.Id WHERE AR_TypeDescriptor.ParentTypeDescriptorId = @ParentTypeDescriptorId&lt;br /&gt;SELECT AR_TypeDescriptor.[Id], AR_TypeDescriptor.ParameterId, AR_TypeDescriptor.ParentTypeDescriptorId, AR_TypeDescriptor.TypeName, AR_TypeDescriptor.InterpretedTypeName, AR_TypeDescriptor.ContainsIdentifier, AR_TypeDescriptor.IdentifierId, AR_TypeDescriptor.ContainsFilterDescriptor, AR_TypeDescriptor.FilterDescriptorId, AR_TypeDescriptor.IsCollection, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_TypeDescriptorON AR_TypeDescriptor.Id = AR_MetadataObject.Id WHERE AR_TypeDescriptor.ParentTypeDescriptorId = @ParentTypeDescriptorIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetDataClassById] Script Date: 06/23/2008 08:04:15 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetDataClassById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_Class.[Id], AR_Class.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_ClassINNER JOIN AR_MetadataObject ON AR_Class.Id = AR_MetadataObject.IdLEFT JOIN AR_Entity ON AR_Class.Id = AR_Entity.IdWHERE AR_Class.Id = @MetadataObjectId AND AR_Entity.Id IS NULL&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetDataClassesForSystemWithCount] Script Date: 06/23/2008 08:04:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetDataClassesForSystemWithCount] ( @SystemId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_ClassLEFT JOIN AR_Entity ON AR_Class.Id = AR_Entity.IdWHERE AR_Class.SystemId = @SystemId AND AR_Entity.Id IS NULL&lt;br /&gt;SELECT AR_Class.[Id], AR_Class.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_ClassON AR_Class.Id = AR_MetadataObject.Id LEFT JOIN AR_EntityON AR_Class.Id = AR_Entity.IdWHERE AR_Class.SystemId = @SystemId AND AR_Entity.Id IS NULLCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetDefaultValuesForTypeDescriptor] Script Date: 06/23/2008 08:04:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetDefaultValuesForTypeDescriptor] ( @TypeDescriptorId int, @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;IF NOT EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE Id = @TypeDescriptorId)BEGIN SET @ErrorCode = -2 -- objectNotFoundEND&lt;br /&gt;SELECT AR_DefaultValue.[Id], AR_DefaultValue.[Value], AR_DefaultValue.[TypeDescriptorId], AR_DefaultValue.[MethodInstanceId], AR_MetadataObject.[Name] AS MethodInstanceNameFROM AR_DefaultValue, AR_MetadataObjectWHERE AR_DefaultValue.TypeDescriptorId = @TypeDescriptorId AND AR_MetadataObject.Id = AR_DefaultValue.MethodInstanceId&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetDependentEntitiesForEntity] Script Date: 06/23/2008 08:04:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetDependentEntitiesForEntity] ( @EntityId int)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SELECT AR_Entity.[Id] AS EntityId-- AR_Entity.EstimatedInstanceCount,-- AR_Class.SystemId,-- AR_MetadataObject.[Name], -- AR_MetadataObject.IsCached,-- AR_MetadataObject.VersionFROM AR_Entity--INNER JOIN-- AR_Class--ON-- AR_Entity.Id = AR_Class.Id--INNER JOIN -- AR_MetadataObject --ON-- AR_Class.Id = AR_MetadataObject.IdWHERE AR_Entity.Id != @EntityId AND AR_Entity.Id IN (SELECT EntityId FROM AR_Identifier WHERE Id IN (SELECT IdentifierId FROM AR_TypeDescriptor WHERE IdentifierId IS NOT NULL AND ParameterId IN (SELECT Id FROM AR_Parameter WHERE MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @EntityId))))&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetEntitiesForAssociationAndRoleWithCount] Script Date: 06/23/2008 08:04:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetEntitiesForAssociationAndRoleWithCount] ( @AssociationId int, @EntityRole bit) AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT( * )FROM AR_EntityINNER JOIN AR_ClassON AR_Class.Id = AR_Entity.IdINNER JOIN AR_MetadataObjectON AR_MetadataObject.Id = AR_Class.IdWHERE AR_Entity.IdIN ( SELECT AR_AssociationMember.EntityId FROM AR_AssociationMember WHERE AR_AssociationMember.AssociationId = @AssociationId AND AR_AssociationMember.EntityRole = @EntityRole)&lt;br /&gt;SELECT AR_Entity.Id, AR_Entity.EstimatedInstanceCount, AR_Class.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_EntityINNER JOIN AR_ClassON AR_Class.Id = AR_Entity.IdINNER JOIN AR_MetadataObjectON AR_MetadataObject.Id = AR_Class.IdWHERE AR_Entity.IdIN ( SELECT AR_AssociationMember.EntityId FROM AR_AssociationMember WHERE AR_AssociationMember.AssociationId = @AssociationId AND AR_AssociationMember.EntityRole = @EntityRole)&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetEntitiesForSystemLikeNameWithCount] Script Date: 06/23/2008 08:04:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetEntitiesForSystemLikeNameWithCount] ( @MetadataObjectName nvarchar (255), @SystemId int)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SELECT COUNT (AR_MetadataObject.Id)FROM AR_EntityINNER JOIN AR_ClassON AR_Entity.Id = AR_Class.IdINNER JOIN AR_MetadataObject ON AR_Class.Id = AR_MetadataObject.IdWHERE UPPER(AR_MetadataObject.Name) LIKE @MetadataObjectName AND AR_Class.SystemId = @SystemId&lt;br /&gt;SELECT AR_Entity.[Id], AR_Entity.EstimatedInstanceCount, AR_Class.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_EntityINNER JOIN AR_ClassON AR_Entity.Id = AR_Class.IdINNER JOIN AR_MetadataObject ON AR_Class.Id = AR_MetadataObject.IdWHERE UPPER(AR_MetadataObject.Name) LIKE @MetadataObjectName AND AR_Class.SystemId = @SystemId&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetEntitiesForSystemWithCount] Script Date: 06/23/2008 08:04:17 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetEntitiesForSystemWithCount] ( @SystemId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_EntityINNER JOIN AR_ClassON AR_Class.Id = AR_Entity.IdWHERE AR_Class.SystemId = @SystemId&lt;br /&gt;SELECT AR_Entity.[Id], AR_Entity.EstimatedInstanceCount, AR_Class.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_ClassON AR_Class.Id = AR_MetadataObject.Id INNER JOIN AR_EntityON AR_Class.Id = AR_Entity.IdWHERE AR_Class.SystemId = @SystemIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetEntityById] Script Date: 06/23/2008 08:04:17 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetEntityById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_Entity.[Id], AR_Entity.EstimatedInstanceCount, AR_Class.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_EntityINNER JOIN AR_ClassON AR_Entity.Id = AR_Class.IdINNER JOIN AR_MetadataObject ON AR_Class.Id = AR_MetadataObject.IdWHERE AR_Entity.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetFilterDescriptorById] Script Date: 06/23/2008 08:04:17 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetFilterDescriptorById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_FilterDescriptor.[Id], AR_FilterDescriptor.[TypeName], AR_FilterDescriptor.[MethodId], AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_FilterDescriptorINNER JOIN AR_MetadataObjectON AR_FilterDescriptor.Id = AR_MetadataObject.IdWHERE AR_FilterDescriptor.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetFilterDescriptorsForMethodWithCount] Script Date: 06/23/2008 08:04:17 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetFilterDescriptorsForMethodWithCount] ( @MethodId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_FilterDescriptorINNER JOIN AR_MetadataObjectON AR_FilterDescriptor.Id = AR_MetadataObject.IdWHERE AR_FilterDescriptor.MethodId = @MethodId&lt;br /&gt;SELECT AR_FilterDescriptor.[Id], AR_FilterDescriptor.[TypeName], AR_FilterDescriptor.[MethodId], AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_FilterDescriptorON AR_FilterDescriptor.Id = AR_MetadataObject.Id WHERE AR_FilterDescriptor.MethodId = @MethodIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetIdentifierById] Script Date: 06/23/2008 08:04:18 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetIdentifierById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_Identifier.[Id], AR_Identifier.[TypeName], AR_Identifier.[EntityId], AR_Identifier.[OrdinalNumber], AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_IdentifierINNER JOIN AR_MetadataObjectON AR_Identifier.Id = AR_MetadataObject.IdWHERE AR_Identifier.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetIdentifiersForEntityWithCount] Script Date: 06/23/2008 08:04:18 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetIdentifiersForEntityWithCount] ( @EntityId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_IdentifierINNER JOIN AR_MetadataObjectON AR_Identifier.Id = AR_MetadataObject.IdWHERE AR_Identifier.EntityId = @EntityId&lt;br /&gt;SELECT AR_Identifier.[Id], AR_Identifier.[TypeName], AR_Identifier.[EntityId], AR_Identifier.[OrdinalNumber], AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_IdentifierINNER JOIN AR_MetadataObjectON AR_Identifier.Id = AR_MetadataObject.IdWHERE AR_Identifier.EntityId = @EntityId ORDER BY AR_Identifier.[OrdinalNumber] ASCCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetMethodById] Script Date: 06/23/2008 08:04:18 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetMethodById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_Method.[Id], AR_Method.ClassId, AR_Method.IsStatic, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MethodINNER JOIN AR_MetadataObjectON AR_Method.Id = AR_MetadataObject.IdWHERE AR_Method.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetMethodInstanceById] Script Date: 06/23/2008 08:04:18 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetMethodInstanceById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_MethodInstance.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MethodInstanceINNER JOIN AR_MetadataObjectON AR_MethodInstance.Id = AR_MetadataObject.Id-- exclude out those method instances that are associations-- as they will be materialized and treated separately LEFT JOIN AR_AssociationON AR_MethodInstance.Id = AR_Association.IdWHERE AR_MethodInstance.Id = @MetadataObjectId AND AR_Association.Id IS NULL&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetMethodInstancesForDataClassWithCount] Script Date: 06/23/2008 08:04:18 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetMethodInstancesForDataClassWithCount] ( @ClassId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_MetadataObjectINNER JOIN AR_MethodInstanceON AR_MethodInstance.Id = AR_MetadataObject.Id -- exclude out those method instances that are associations-- as they will be materialized and treated separately LEFT JOIN AR_AssociationON AR_MethodInstance.Id = AR_Association.IdWHERE AR_MethodInstance.MethodId IN (SELECT AR_Method.[Id] AS Method FROM AR_Method WHERE AR_Method.ClassId = @ClassId) AND AR_Association.Id IS NULL&lt;br /&gt;SELECT AR_MethodInstance.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_MethodInstanceON AR_MethodInstance.Id = AR_MetadataObject.Id -- exclude out those method instances that are associations-- as they will be materialized and treated separately LEFT JOIN AR_AssociationON AR_MethodInstance.Id = AR_Association.IdWHERE AR_MethodInstance.MethodId IN (SELECT AR_Method.[Id] AS Method FROM AR_Method WHERE AR_Method.ClassId = @ClassId) AND AR_Association.Id IS NULL&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetMethodInstancesForMethodWithCount] Script Date: 06/23/2008 08:04:19 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetMethodInstancesForMethodWithCount] ( @MethodId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_MetadataObjectINNER JOIN AR_MethodInstanceON AR_MethodInstance.Id = AR_MetadataObject.Id -- exclude out those method instances that are associations-- as they will be materialized and treated separately LEFT JOIN AR_AssociationON AR_MethodInstance.Id = AR_Association.IdWHERE AR_MethodInstance.MethodId = @MethodId AND AR_Association.Id IS NULL&lt;br /&gt;SELECT AR_MethodInstance.[Id], AR_MethodInstance.MethodId, AR_MethodInstance.ReturnTypeDescriptorId, AR_MethodInstance.Type, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_MethodInstanceON AR_MethodInstance.Id = AR_MetadataObject.Id -- exclude out those method instances that are associations-- as they will be materialized and treated separately LEFT JOIN AR_AssociationON AR_MethodInstance.Id = AR_Association.IdWHERE AR_MethodInstance.MethodId = @MethodId AND AR_Association.Id IS NULL&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetMethodsForDataClassWithCount] Script Date: 06/23/2008 08:04:19 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetMethodsForDataClassWithCount] ( @ClassId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_MethodINNER JOIN AR_MetadataObjectON AR_Method.Id = AR_MetadataObject.IdWHERE AR_Method.ClassId = @ClassId&lt;br /&gt;SELECT AR_Method.[Id], AR_Method.ClassId, AR_Method.IsStatic, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_MethodON AR_Method.Id = AR_MetadataObject.Id WHERE AR_Method.ClassId = @ClassIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetParameterById] Script Date: 06/23/2008 08:04:19 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetParameterById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_Parameter.[Id], AR_Parameter.MethodId, AR_Parameter.Direction, AR_Parameter.OrdinalNumber, AR_Parameter.TypeReflectorTypeName, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_ParameterINNER JOIN AR_MetadataObjectON AR_Parameter.Id = AR_MetadataObject.IdWHERE AR_Parameter.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetParametersForMethodWithCount] Script Date: 06/23/2008 08:04:19 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetParametersForMethodWithCount] ( @MethodId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_ParameterINNER JOIN AR_MetadataObjectON AR_Parameter.Id = AR_MetadataObject.IdWHERE AR_Parameter.MethodId = @MethodId&lt;br /&gt;SELECT AR_Parameter.[Id], AR_Parameter.MethodId, AR_Parameter.Direction, AR_Parameter.OrdinalNumber, AR_Parameter.TypeReflectorTypeName, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_ParameterON AR_Parameter.Id = AR_MetadataObject.Id WHERE AR_Parameter.MethodId = @MethodIdORDER BY AR_Parameter.OrdinalNumberASCCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetPropertiesForMetadataObject] Script Date: 06/23/2008 08:04:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetPropertiesForMetadataObject] ( @MetadataObjectId int, @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;IF NOT EXISTS (SELECT Id FROM AR_MetadataObject WHERE Id = @MetadataObjectId)BEGIN SET @ErrorCode = -2 -- objectNotFoundEND&lt;br /&gt;SELECT AR_Property.[Name], AR_Property.[Value]FROM AR_PropertyWHERE AR_Property.MetadataObjectId = @MetadataObjectId&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetRootTypeDescriptorForParameter] Script Date: 06/23/2008 08:04:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetRootTypeDescriptorForParameter] ( @ParameterId int)AS&lt;br /&gt;SELECT AR_TypeDescriptor.[Id], AR_TypeDescriptor.ParameterId, AR_TypeDescriptor.ParentTypeDescriptorId, AR_TypeDescriptor.TypeName, AR_TypeDescriptor.InterpretedTypeName, AR_TypeDescriptor.ContainsIdentifier, AR_TypeDescriptor.IdentifierId, AR_TypeDescriptor.ContainsFilterDescriptor, AR_TypeDescriptor.FilterDescriptorId, AR_TypeDescriptor.IsCollection, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_TypeDescriptorON AR_TypeDescriptor.Id = AR_MetadataObject.Id WHERE AR_TypeDescriptor.ParameterId = @ParameterId AND AR_TypeDescriptor.ParentTypeDescriptorId IS NULL&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetSystemById] Script Date: 06/23/2008 08:04:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetSystemById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_System.[Id], AR_System.ConnectionFactoryTypeName, AR_System.SystemUtilityTypeName, AR_System.SystemEntityTypeName, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_SystemINNER JOIN AR_MetadataObject ON AR_System.Id = AR_MetadataObject.IdWHERE AR_System.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetSystemDataBySystemName] Script Date: 06/23/2008 08:04:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetSystemDataBySystemName] ( @SystemName nvarchar (255))AS&lt;br /&gt;DECLARE @SystemId intSELECT @SystemId=AR_System.Id FROM AR_System,AR_MetadataObject WHERE AR_MetadataObject.Id = AR_System.Id AND AR_MetadataObject.Name = @SystemName&lt;br /&gt;BEGIN TRANSACTIONSELECT AR_SystemData.[Length], AR_SystemData.[Data]FROM AR_SystemDataWHERE AR_SystemData.[SystemId] = @SystemIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetSystemInstanceById] Script Date: 06/23/2008 08:04:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetSystemInstanceById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_SystemInstance.[Id], AR_SystemInstance.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_SystemInstanceINNER JOIN AR_MetadataObject ON AR_SystemInstance.Id = AR_MetadataObject.IdWHERE AR_SystemInstance.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetSystemInstanceByName] Script Date: 06/23/2008 08:04:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetSystemInstanceByName] ( @Name nvarchar(50))AS&lt;br /&gt;BEGIN TRANSACTIONSELECT AR_SystemInstance.[Id], AR_SystemInstance.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.Version FROM AR_SystemInstanceINNER JOIN AR_MetadataObject ON AR_SystemInstance.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @NameCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetSystemInstancesForSystemWithCount] Script Date: 06/23/2008 08:04:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetSystemInstancesForSystemWithCount] ( @SystemId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT(*)FROM AR_SystemInstanceWHERE AR_SystemInstance.SystemId = @SystemId&lt;br /&gt;SELECT AR_SystemInstance.[Id], AR_SystemInstance.SystemId, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.Version FROM AR_SystemInstanceINNER JOIN AR_MetadataObject ON AR_SystemInstance.Id = AR_MetadataObject.IdWHERE AR_SystemInstance.SystemId = @SystemIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetSystemsLikeNameWithCount] Script Date: 06/23/2008 08:04:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetSystemsLikeNameWithCount] ( @MetadataObjectName nvarchar (255), @LCID int)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SELECT DISTINCT COUNT (AR_System.Id)FROM AR_SystemINNER JOIN AR_MetadataObject ON AR_System.Id = AR_MetadataObject.IdLEFT JOIN AR_LocalizedNameON AR_MetadataObject.Id = AR_LocalizedName.MetadataObjectIdWHERE UPPER(AR_MetadataObject.Name) LIKE @MetadataObjectName OR (UPPER(AR_LocalizedName.LocalizedName) LIKE @MetadataObjectName AND (AR_LocalizedName.LCID = @LCID OR AR_LocalizedName.LCID = 0))&lt;br /&gt;SELECT DISTINCT AR_System.[Id], AR_System.ConnectionFactoryTypeName, AR_System.SystemUtilityTypeName, AR_System.SystemEntityTypeName, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_SystemINNER JOIN AR_MetadataObject ON AR_System.Id = AR_MetadataObject.IdLEFT JOIN AR_LocalizedNameON AR_MetadataObject.Id = AR_LocalizedName.MetadataObjectIdWHERE UPPER(AR_MetadataObject.Name) LIKE @MetadataObjectName OR (UPPER(AR_LocalizedName.LocalizedName) LIKE @MetadataObjectName AND (AR_LocalizedName.LCID = @LCID OR AR_LocalizedName.LCID = 0))&lt;br /&gt;COMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetTypeDescriptorById] Script Date: 06/23/2008 08:04:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetTypeDescriptorById] ( @MetadataObjectId int)AS&lt;br /&gt;SELECT AR_TypeDescriptor.[Id], AR_TypeDescriptor.ParameterId, AR_TypeDescriptor.ParentTypeDescriptorId, AR_TypeDescriptor.TypeName, AR_TypeDescriptor.InterpretedTypeName, AR_TypeDescriptor.ContainsIdentifier, AR_TypeDescriptor.IdentifierId, AR_TypeDescriptor.ContainsFilterDescriptor, AR_TypeDescriptor.FilterDescriptorId, AR_TypeDescriptor.IsCollection, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_TypeDescriptorINNER JOIN AR_MetadataObjectON AR_TypeDescriptor.Id = AR_MetadataObject.IdWHERE AR_TypeDescriptor.Id = @MetadataObjectId&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetTypeDescriptorsByNameAndParameter] Script Date: 06/23/2008 08:04:22 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetTypeDescriptorsByNameAndParameter] ( @ParameterId int, @Name nvarchar(255))AS&lt;br /&gt;SELECT AR_TypeDescriptor.[Id], AR_TypeDescriptor.ParameterId, AR_TypeDescriptor.ParentTypeDescriptorId, AR_TypeDescriptor.TypeName, AR_TypeDescriptor.InterpretedTypeName, AR_TypeDescriptor.ContainsIdentifier, AR_TypeDescriptor.IdentifierId, AR_TypeDescriptor.ContainsFilterDescriptor, AR_TypeDescriptor.FilterDescriptorId, AR_TypeDescriptor.IsCollection, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_TypeDescriptorINNER JOIN AR_MetadataObjectON AR_TypeDescriptor.Id = AR_MetadataObject.IdWHERE AR_TypeDescriptor.ParameterId = @ParameterId AND AR_MetadataObject.Name = @Name&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_GetTypeDescriptorsForFilterDescriptorWithCount] Script Date: 06/23/2008 08:04:22 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_GetTypeDescriptorsForFilterDescriptorWithCount] ( @FilterDescriptorId int)AS&lt;br /&gt;BEGIN TRANSACTIONSELECT COUNT (*)FROM AR_TypeDescriptorWHERE AR_TypeDescriptor.FilterDescriptorId = @FilterDescriptorId&lt;br /&gt;SELECT AR_TypeDescriptor.[Id], AR_TypeDescriptor.ParameterId, AR_TypeDescriptor.ParentTypeDescriptorId, AR_TypeDescriptor.TypeName, AR_TypeDescriptor.InterpretedTypeName, AR_TypeDescriptor.ContainsIdentifier, AR_TypeDescriptor.IdentifierId, AR_TypeDescriptor.ContainsFilterDescriptor, AR_TypeDescriptor.FilterDescriptorId, AR_TypeDescriptor.IsCollection, AR_MetadataObject.[Name], AR_MetadataObject.IsCached, AR_MetadataObject.VersionFROM AR_MetadataObjectINNER JOIN AR_TypeDescriptorON AR_TypeDescriptor.Id = AR_MetadataObject.Id WHERE AR_TypeDescriptor.FilterDescriptorId = @FilterDescriptorIdCOMMIT TRANSACTION&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_SetAccessControlEntryForMetadataObject] Script Date: 06/23/2008 08:04:22 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_SetAccessControlEntryForMetadataObject] ( @MetadataObjectId [int], @IdentityName [nvarchar] (250), @DisplayName [nvarchar] (250), @RawSid [varbinary] (512), @Rights [bigint])AS&lt;br /&gt;DELETE FROM AR_MetadataObjectSecurityWHERE IdentityName = @IdentityName AND MetadataObjectId = @MetadataObjectID&lt;br /&gt;INSERT INTO AR_MetadataObjectSecurity (MetadataObjectId, IdentityName, DisplayName, RawSid, Rights)VALUES (@MetadataObjectId, @IdentityName, @DisplayName, @RawSid, @Rights)&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_SetDefaultAction] Script Date: 06/23/2008 08:04:22 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_SetDefaultAction] ( @EntityId int, @ActionName nvarchar(255), @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;IF(@ActionName IS NOT NULL)BEGIN -- See if the action/entity pair exists SELECT AR_MetadataObject.Id FROM AR_Action INNER JOIN AR_MetadataObject ON AR_Action.Id = AR_MetadataObject.Id WHERE AR_Action.EntityId = @EntityId AND AR_MetadataObject.Name = @ActionName&lt;br /&gt;IF (@@ROWCOUNT = 0) BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROR ENDEND&lt;br /&gt;-- TODO: Make this a proper field on the entity table-- See if property already existsSELECT ValueFROM AR_PropertyWHERE MetadataObjectId = @EntityId AND Name = 'DefaultAction'&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN IF(@ActionName IS NOT NULL) BEGIN -- Need to create the property INSERT INTO AR_Property (Name,Value,MetadataObjectId) VALUES ('DefaultAction', @ActionName, @EntityId)&lt;br /&gt;SELECT @ErrorCode = @@Error IF @ErrorCode &lt;&gt; 0 GOTO ON_ERROR ENDENDELSEBEGIN IF(@ActionName IS NOT NULL) BEGIN -- Update old property UPDATE AR_Property SET Value = @ActionName WHERE MetadataObjectId = @EntityId AND Name = 'DefaultAction' SELECT @ErrorCode = @@Error IF @ErrorCode &lt;&gt; 0 GOTO ON_ERROR END ELSE BEGIN -- delete property DELETE AR_Property WHERE MetadataObjectId = @EntityId AND Name = 'DefaultAction' SELECT @ErrorCode = @@Error IF @ErrorCode &lt;&gt; 0 GOTO ON_ERROR ENDEND&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_SetDefaultValuesForTypeDescriptor] Script Date: 06/23/2008 08:04:23 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_SetDefaultValuesForTypeDescriptor] ( @TypeDescriptorId int, @MethodInstanceId int, @Value sql_variant, @ErrorCode int OUTPUT)AS&lt;br /&gt;BEGIN TRANSACTION&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;-- ensure the MethodInstance and TypeDescriptor existIF NOT EXISTS (SELECT Id FROM AR_MethodInstance WHERE Id = @MethodInstanceId)BEGIN SET @ErrorCode = -2 GOTO ON_ERRORENDIF NOT EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE Id = @TypeDescriptorId)BEGIN SET @ErrorCode = -2 GOTO ON_ERROREND&lt;br /&gt;-- ensure the MethodInstance and TypeDescriptor belong to same MethodDECLARE @TypeDescriptorMethodId intSELECT @TypeDescriptorMethodId=Id FROM AR_Method WHERE Id IN (SELECT MethodId FROM AR_Parameter WHERE Id IN (SELECT ParameterId FROM AR_TypeDescriptor WHERE Id = @TypeDescriptorId))DECLARE @MethodInstanceMethodId intSELECT @MethodInstanceMethodId=MethodId FROM AR_MethodInstance WHERE &lt;a href="mailto:Id=@MethodInstanceId"&gt;Id=@MethodInstanceId&lt;/a&gt;&lt;br /&gt;IF(@MethodInstanceMethodId != @TypeDescriptorMethodId)BEGIN SET @ErrorCode = -600 GOTO ON_ERROREND&lt;br /&gt;-- ensure MaxDECLARE @DefaultValuesForTypeDescriptor intSET @DefaultValuesForTypeDescriptor = (SELECT COUNT (Id) FROM AR_DefaultValue WHERE TypeDescriptorId = @TypeDescriptorId)IF(@DefaultValuesForTypeDescriptor &gt;= 15)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- insert/updateIF EXISTS (SELECT * FROM AR_DefaultValue WHERE TypeDescriptorId = @TypeDescriptorId AND MethodInstanceId = @MethodInstanceId)BEGIN UPDATE AR_DefaultValue SET &lt;a href="mailto:Value=@Value"&gt;Value=@Value&lt;/a&gt; WHERE TypeDescriptorId = @TypeDescriptorId AND MethodInstanceId = @MethodInstanceIdENDELSEBEGIN INSERT INTO AR_DefaultValue (TypeDescriptorId, MethodInstanceId, Value) VALUES (@TypeDescriptorId, @MethodInstanceId, @Value)END&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_SetSystemDataBySystemName] Script Date: 06/23/2008 08:04:23 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_SetSystemDataBySystemName] ( @SystemName nvarchar(255), @AssemblyName nvarchar (255), @Length int, @Data Image)AS&lt;br /&gt;BEGIN TRANSACTIONDECLARE @Result intSET @Result = 0;&lt;br /&gt;DECLARE @SystemId intSELECT @SystemId=AR_System.Id FROM AR_System,AR_MetadataObject WHERE AR_MetadataObject.Id = AR_System.Id AND AR_MetadataObject.Name = @SystemName&lt;br /&gt;IF NOT EXISTS ( SELECT * FROM AR_SystemData WHERE SystemId = @SystemId)BEGIN INSERT INTO AR_SystemData ( [SystemId], [Name], [Length], [Data] ) VALUES ( @SystemId, @AssemblyName, @Length, @Data )&lt;br /&gt;SET @Result = @@ROWCOUNTENDELSEBEGIN UPDATE AR_SystemData SET AR_SystemData.[Name] = @AssemblyName, AR_SystemData.[Length] = @Length, AR_SystemData.[Data] = @Data WHERE AR_SystemData.[SystemId] = @SystemId&lt;br /&gt;SET @Result = @@ROWCOUNTEND&lt;br /&gt;COMMIT TRANSACTIONRETURN @Result&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_TypeDescriptorContainsFlagsFixup] Script Date: 06/23/2008 08:04:23 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_TypeDescriptorContainsFlagsFixup] ( @TypeDescriptorId int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0IF(@TypeDescriptorId IS NULL OR @TypeDescriptorId=0) RETURN&lt;br /&gt;-- BEGIN TRANSACION; allow nested invocation. SQL doesn't like if a stored proc rolls back a txn that it didn't start. -- Another way to look at it is @@TRANCOUNT must be same at the start and end of a stored procDECLARE @TranCount int SET @TranCount=@@TRANCOUNT IF(@@TRANCOUNT=0) BEGIN TRANSACTION&lt;br /&gt;DECLARE @currentParameter int&lt;br /&gt;-- logical OR of currentTypeDescriptor's children and whether or not the currentTypeDescriptor has an IdentifierId or FilterDescriptorId respectivelyDECLARE @currentContainsIdentifier bitDECLARE @currentContainsFilterDescriptor bit&lt;br /&gt;-- setting the initial condition of the contains* vars based on the presence of Identifier/FilterDesc Ids in the current rowDECLARE @containsIdentifier bit -- the cumulative result of the OR operationDECLARE @currentIdentifierId intDECLARE @containsFilterDescriptor bit -- the cumulative result of the OR operationDECLARE @currentFilterDescriptorId intSELECT @currentParameter=ParameterId, @currentIdentifierId=IdentifierId, @currentFilterDescriptorId=FilterDescriptorId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@TypeDescriptorId"&gt;Id=@TypeDescriptorId&lt;/a&gt;IF (@currentIdentifierId != 0) SET @containsIdentifier = 1 ELSE SET @containsIdentifier = 0IF (@currentFilterDescriptorId != 0) SET @containsFilterDescriptor = 1 ELSE SET @containsFilterDescriptor = 0&lt;br /&gt;-- setting the outer loop to loop from current to root DECLARE @current intSET @current = @TypeDescriptorIdDECLARE @maxNestDepth tinyintSET @maxNestDepth = 10WHILE(@current != 0 AND @maxNestDepth &gt; 0)BEGIN -- perform logical OR for Identifiers -- IF EXISTS(SELECT Id FROM AR_TypeDescriptor WHERE ParameterId = @currentParameter AND ParentTypeDescriptorId = @current AND ContainsIdentifier = 1) BEGIN SET @containsIdentifier = 1 END&lt;br /&gt;-- perform logical OR for FilterDescriptors -- IF EXISTS(SELECT Id FROM AR_TypeDescriptor WHERE ParameterId = @currentParameter AND ParentTypeDescriptorId = @current AND ContainsFilterDescriptor = 1) BEGIN SET @containsFilterDescriptor = 1 END&lt;br /&gt;-- now before updating, make sure we're actually changing something ... DECLARE @changedContainsIdentifier bit SET @changedContainsIdentifier = 0 DECLARE @changedContainsFilterDescriptor bit SET @changedContainsFilterDescriptor = 0 SELECT @currentContainsIdentifier=ContainsIdentifier, @currentContainsFilterDescriptor=ContainsFilterDescriptor FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@current"&gt;Id=@current&lt;/a&gt; IF (@currentContainsIdentifier != @containsIdentifier) SET @changedContainsIdentifier = 1 IF (@currentContainsFilterDescriptor != @containsFilterDescriptor) SET @changedContainsFilterDescriptor = 1 IF (@changedContainsIdentifier = 1 OR @changedContainsFilterDescriptor = 1) BEGIN UPDATE AR_TypeDescriptor SET ContainsIdentifier = @containsIdentifier, ContainsFilterDescriptor = @containsFilterDescriptor WHERE Id = @current SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;EXEC proc_ar_BumpMetadataObjectVersionCounter @current END&lt;br /&gt;-- update current for next iteration SELECT @current=ParentTypeDescriptorId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@current"&gt;Id=@current&lt;/a&gt;&lt;br /&gt;-- control max typeDescriptor nest level SET @maxNestDepth = @maxNestDepth - 1END&lt;br /&gt;IF(@maxNestDepth = 0)BEGIN SET @ErrorCode = -300 GOTO ON_ERROREND&lt;br /&gt;IF(@TranCount &lt; @@TRANCOUNT) COMMIT TRANSACTIONRETURN ON_ERROR:IF(@TranCount &lt; @@TRANCOUNT) ROLLBACK TRANSACTIONRETURN GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateActionById] Script Date: 06/23/2008 08:04:24 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateActionById] ( @Id int, @Name nvarchar(50), @IsCached bit, @Version int OUTPUT, @Position int, @IsDisplayed bit, @IsOpenedInNewWindow bit, @Icon nvarchar(255), @Url nvarchar(255), @ErrorCode int OUTPUT )AS BEGIN TRAN SET @ErrorCode = 0 -- Lock Action table so no one can insert a Action with the same-- name as the one we may update the existing Action to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 7 --before updating make sure that the action existsSELECT IdFROM AR_ActionWHERE AR_Action.ID = @Id IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 --action not found GOTO ON_ERROREND -- check for duplicatesSELECT AR_Action.IdFROM AR_ActionINNER JOIN AR_MetadataObjectON AR_Action.Id = AR_MetadataObject.IdWHERE AR_Action.EntityId IN (SELECT EntityId FROM AR_Action WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;) AND AR_MetadataObject.Name = @Name AND AR_Action.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- UpdateUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;UPDATE AR_ACTIONSET Position = @Position, IsDisplayed = @IsDisplayed, IsOpenedInNewWindow = @IsOpenedInNewWindow, Icon = @Icon, Url = @UrlWHERE Id = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateActionParameterById] Script Date: 06/23/2008 08:04:24 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateActionParameterById] ( @Id int, @IsCached bit, @Version int OUTPUT, @Name nvarchar(50), @Index tinyint, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;BEGIN TRAN&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;-- Lock ActionParameter table so no one can insert a ActionParameter with the same-- name as the one we may update the existing ActionParameter to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 13&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_ActionParameterWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_ActionParameter.IdFROM AR_ActionParameterINNER JOIN AR_MetadataObjectON AR_ActionParameter.Id = AR_MetadataObject.IdWHERE AR_ActionParameter.ActionId IN (SELECT ActionId FROM AR_ActionParameter WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;) AND AR_MetadataObject.Name = @Name AND AR_MetadataObject.Id != @Id -- don't consider yourself a duplicate&lt;br /&gt;IF (@@ROWCOUNT != 0)BEGIN SET @ErrorCode = -1 -- Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- UpdateUPDATE AR_METADATAOBJECTSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;UPDATE AR_ACTIONPARAMETERSET [Index] = @IndexWHERE Id = @IdSELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANRETURN&lt;br /&gt;ON_ERROR:ROLLBACK TRANRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateAssociationById] Script Date: 06/23/2008 08:04:25 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateAssociationById] ( @Id int, @Name nvarchar(255), @IsCached bit, @ReturnTypeDescriptorId int, @Type tinyint, @Version int OUTPUT, -- input/output param @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock Association table so no one can insert a Association with the same-- name as the one we may update the existing Association to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 8&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_AssociationWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesDECLARE @MethodId intSELECT @MethodId=MethodId FROM AR_MethodInstance WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;&lt;br /&gt;DECLARE @EntityId intSELECT @EntityId=ClassId FROM AR_Method WHERE &lt;a href="mailto:Id=@MethodId"&gt;Id=@MethodId&lt;/a&gt;&lt;br /&gt;DECLARE @SystemId int SELECT @SystemId=SystemId FROM AR_Class WHERE &lt;a href="mailto:Id=@EntityId"&gt;Id=@EntityId&lt;/a&gt;&lt;br /&gt;SELECT AR_Association.IdFROM AR_Association INNER JOIN AR_MethodInstanceON AR_Association.Id = AR_MethodInstance.IdINNER JOIN AR_MetadataObjectON AR_MethodInstance.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_MethodInstance.MethodId IN (SELECT Id FROM AR_Method WHERE ClassId IN (SELECT Id FROM AR_Class WHERE &lt;a href="mailto:SystemId=@SystemId"&gt;SystemId=@SystemId&lt;/a&gt;)) AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraints-- do not allow this sproc to update ReturnTypeDescriptorId or MethodInstanceType; the parameters are only passed in to support Associations subclassing MethodInstanceDECLARE @oldReturnTypeDescriptorId intDECLARE @oldType tinyintSELECT @oldReturnTypeDescriptorId=ReturnTypeDescriptorId, @oldType=Type FROM AR_MethodInstance WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;IF( @oldReturnTypeDescriptorId != @ReturnTypeDescriptorId OR @oldType != @Type )BEGIN SET @ErrorCode = -500 GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;ALL_WELL:COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateEntityById] Script Date: 06/23/2008 08:04:25 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateEntityById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param @SystemId int, @EstimatedInstanceCount int, @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock Entity table so no one can insert an Entity with the same-- name as the one we may update the existing Entity to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 4&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_EntityWHERE AR_Entity.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_Entity.IdFROM AR_EntityINNER JOIN AR_ClassON AR_Entity.Id = AR_Class.IdINNER JOIN AR_MetadataObjectON AR_Class.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Class.SystemId = @SystemId AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @EntitiesForSystem intSET @EntitiesForSystem = (SELECT COUNT (AR_Entity.Id) FROM AR_Entity INNER JOIN AR_Class ON AR_Entity.Id = AR_Class.Id WHERE AR_Class.SystemId = @SystemId)IF(@EntitiesForSystem &gt;= 200)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update ClassUPDATE AR_ClassSET SystemId = @SystemIdWHERE Id = @Id&lt;br /&gt;-- Update EntityUPDATE AR_EntitySET EstimatedInstanceCount = @EstimatedInstanceCountWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateFilterDescriptorById] Script Date: 06/23/2008 08:04:25 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateFilterDescriptorById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param-- @MethodId int, -- forbid updating this @TypeName nvarchar(255), @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock FilterDescriptor table so no one can insert a FilterDescriptor with the same-- name as the one we may update the existing FilterDescriptor to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 9&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_FilterDescriptorWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_FilterDescriptor.IdFROM AR_FilterDescriptorINNER JOIN AR_MetadataObjectON AR_FilterDescriptor.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_FilterDescriptor.MethodId IN (SELECT MethodId FROM AR_FilterDescriptor WHERE Id = @Id) AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update FilterDescriptorUPDATE AR_FilterDescriptorSET TypeName = @TypeNameWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateIdentifierById] Script Date: 06/23/2008 08:04:26 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateIdentifierById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param-- @EntityId int, -- forbid updating this-- @OrdinalNumber int, - no need; this is automaintained @TypeName nvarchar(255), @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock Identifier table so no one can insert an Identifier with the same-- name as the one we may update the existing Identifier to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 5&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_IdentifierWHERE AR_Identifier.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_Identifier.IdFROM AR_IdentifierINNER JOIN AR_MetadataObjectON AR_Identifier.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Identifier.EntityId IN (SELECT EntityId FROM AR_Identifier WHERE Id = @Id) AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update IdentifierUPDATE AR_IdentifierSET TypeName = @TypeNameWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateMethodById] Script Date: 06/23/2008 08:04:26 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateMethodById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param-- @ClassId int, -- do not allow update @IsStatic bit, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock Method table so no one can insert a Method with the same-- name as the one we may update the existing Method to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 6&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_MethodWHERE AR_Method.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_Method.IdFROM AR_MethodINNER JOIN AR_MetadataObjectON AR_Method.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Method.ClassId IN (SELECT ClassId FROM AR_Method WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;) AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update MethodUPDATE AR_MethodSET IsStatic = @IsStaticWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateMethodInstanceById] Script Date: 06/23/2008 08:04:27 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateMethodInstanceById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param-- @MethodId int, -- do not allow update @ReturnTypeDescriptorId int, @Type tinyint, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock MethodInstance table so no one can insert a MethodInstance with the same-- name as the one we may update the existing MethodInstance to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 10&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_MethodInstanceWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- find the class/entity this methodinstance needs to belong toDECLARE @MethodId intSELECT @MethodId=MethodId FROM AR_MethodInstance WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;DECLARE @ClassId intSELECT @ClassId=ClassId FROM AR_Method WHERE Id = @MethodId&lt;br /&gt;-- Check for duplicate names in all MethodInstances in the entity/class that the given Method belongs toSELECT AR_MethodInstance.IdFROM AR_MethodInstanceINNER JOIN AR_MetadataObjectON AR_MethodInstance.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_MethodInstance.MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId) AND AR_MetadataObject.Id != @Id&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate metadata name GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraintsIF(@Type = 1) -- 'Finder' BEGIN IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE Id != @Id AND Type=1 AND MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)) BEGIN -- object specific errors start at -100, -200 etc. SET @ErrorCode = -200 --More than 1 'Finder' method instance in entity GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@Type = 2) -- 'SpecificFinder' BEGIN IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE Id != @Id AND Type=2 AND MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)) BEGIN SET @ErrorCode = -201 --More than 1 'SpecificFinder' method instance in entity GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@Type = 5) -- 'IdEnumerator' BEGIN IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE Id != @Id AND Type=5 AND MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)) BEGIN SET @ErrorCode = -202 --More than 1 'IdEnumerator' method instance in entity GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@Type = 7) -- 'AccessChecker' BEGIN IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE Id != @Id AND Type=7 AND MethodId IN (SELECT Id FROM AR_Method WHERE ClassId = @ClassId)) BEGIN SET @ErrorCode = -205 --More than 1 'IdEnumerator' method instance in entity GOTO ON_ERROR ENDEND&lt;br /&gt;DECLARE @ReturnTypeDescriptorMethodId intDECLARE @Direction intSELECT @ReturnTypeDescriptorMethodId=MethodId, @Direction=Direction FROM AR_Parameter WHERE Id IN (SELECT ParameterId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@ReturnTypeDescriptorId"&gt;Id=@ReturnTypeDescriptorId&lt;/a&gt;)IF(@ReturnTypeDescriptorMethodId != @MethodId)BEGIN SET @ErrorCode = -203 -- Return typeDescriptor's parameter's method and this method instance's method are not the same GOTO ON_ERROREND&lt;br /&gt;IF(@Direction = 1) -- 1 = DirectionType.InBEGIN SET @ErrorCode = -204 -- Return type descriptor's parameter's direction is DirectionType.In GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update MethodUPDATE AR_MethodInstanceSET Type = @Type, ReturnTypeDescriptorId = @ReturnTypeDescriptorIdWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateParameterById] Script Date: 06/23/2008 08:04:27 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateParameterById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param-- @MethodId int, -- forbid updating this @OrdinalNumber tinyint OUTPUT, -- input/output param @Direction tinyint, @TypeReflectorTypeName nvarchar(255), @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock Parameter table so no one can insert a Parameter with the same-- name as the one we may update the existing Parameter to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 11&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_ParameterWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesDECLARE @MethodId intSELECT @MethodId = MethodId FROM AR_Parameter WHERE Id = @IdSELECT AR_Parameter.IdFROM AR_ParameterINNER JOIN AR_MetadataObjectON AR_Parameter.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_Parameter.MethodId = @MethodId AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraintsIF(@Direction = 4) -- DirectionType.Return directionBEGIN IF EXISTS (SELECT Id FROM AR_Parameter WHERE MethodId = @MethodId AND Direction=4 AND &lt;a href="mailto:Id!=@Id"&gt;Id!=@Id&lt;/a&gt;) BEGIN -- object specific errors start at -100, -200 etc. SET @ErrorCode = -100 --More than 1 'Return' parameter GOTO ON_ERROR ENDEND&lt;br /&gt;IF(@Direction = 1) -- DirectionType.In BEGIN -- If there exists a MethodInstance such that its ReturnTypeDescriptor belongs to the set of TypeDescriptors used by this Parameter IF EXISTS (SELECT Id FROM AR_MethodInstance WHERE ReturnTypeDescriptorId IN (SELECT Id FROM AR_TypeDescriptor WHERE ParameterId = @Id)) BEGIN SET @ErrorCode = -102 --Can't have Parameter DirectionType.In if it's used by a ReturnTypeDescriptor in some other MethodInstance GOTO ON_ERROR ENDEND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- save old ordinalDECLARE @oldOrdinalNumber intSELECT @oldOrdinalNumber=OrdinalNumber FROM AR_Parameter WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;&lt;br /&gt;-- Update ParameterDECLARE @NumParametersInMethod intSELECT @NumParametersInMethod = COUNT(*) FROM AR_Parameter WHERE MethodId = @MethodIdIF (@OrdinalNumber &lt; ordinalnumber =" 0IF"&gt; @NumParametersInMethod) SET @OrdinalNumber=@NumParametersInMethod - 1&lt;br /&gt;UPDATE AR_ParameterSET TypeReflectorTypeName = @TypeReflectorTypeName, Direction = @Direction, OrdinalNumber = @OrdinalNumberWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- figure out if ordinal update caused upward or downward movement of parameterIF(@oldOrdinalNumber != @OrdinalNumber)BEGIN DECLARE @startIndex tinyint DECLARE @endIndex tinyint DECLARE @OrdinalNumberCounter tinyint&lt;br /&gt;IF( @oldOrdinalNumber &gt; @OrdinalNumber ) BEGIN -- move up case SET @startIndex = @OrdinalNumber+1 SET @endIndex = @NumParametersInMethod&lt;br /&gt;-- now reorder ordinals between @startIndex and @endIndex DECLARE ordinalNumberCursor CURSOR LOCAL FORWARD_ONLY KEYSET FOR -- issue query for all params excluding the one to be updated SELECT Id, OrdinalNumber FROM AR_Parameter WHERE MethodId = @MethodId AND Id != @Id ORDER BY OrdinalNumber DESC FOR UPDATE OF OrdinalNumber&lt;br /&gt;OPEN ordinalNumberCursor&lt;br /&gt;SET @OrdinalNumberCounter = @NumParametersInMethod-1 FETCH NEXT FROM ordinalNumberCursor WHILE(@@FETCH_STATUS=0) BEGIN IF( @OrdinalNumberCounter &gt;= @startIndex AND @OrdinalNumberCounter &lt; @endIndex) BEGIN UPDATE AR_Parameter SET &lt;a href="mailto:OrdinalNumber=@OrdinalNumberCounter"&gt;OrdinalNumber=@OrdinalNumberCounter&lt;/a&gt; WHERE CURRENT OF ordinalNumberCursor EXEC proc_ar_BumpMetadataObjectVersionCounter @Id END SET @OrdinalNumberCounter = @OrdinalNumberCounter - 1 FETCH NEXT FROM ordinalNumberCursor END DEALLOCATE ordinalNumberCursor END -- end move up case ELSE BEGIN -- start move down case SET @startIndex = 0 SET @endIndex = @OrdinalNumber&lt;br /&gt;-- now reorder ordinals between @startIndex and @endIndex DECLARE ordinalNumberCursor CURSOR LOCAL FORWARD_ONLY KEYSET FOR -- issue query for all params excluding the one to be updated SELECT Id, OrdinalNumber FROM AR_Parameter WHERE MethodId = @MethodId AND Id != @Id ORDER BY OrdinalNumber ASC FOR UPDATE OF OrdinalNumber&lt;br /&gt;OPEN ordinalNumberCursor&lt;br /&gt;SET @OrdinalNumberCounter = 0 FETCH NEXT FROM ordinalNumberCursor WHILE(@@FETCH_STATUS=0) BEGIN IF( @OrdinalNumberCounter &gt;= @startIndex AND @OrdinalNumberCounter &lt; @endIndex) BEGIN UPDATE AR_Parameter SET &lt;a href="mailto:OrdinalNumber=@OrdinalNumberCounter"&gt;OrdinalNumber=@OrdinalNumberCounter&lt;/a&gt; WHERE CURRENT OF ordinalNumberCursor EXEC proc_ar_BumpMetadataObjectVersionCounter @Id END SET @OrdinalNumberCounter = @OrdinalNumberCounter + 1 FETCH NEXT FROM ordinalNumberCursor END DEALLOCATE ordinalNumberCursor END -- end move down case&lt;br /&gt;END -- end @oldOrdinalNumber != @OrdinalNumber Test&lt;br /&gt;ALL_WELL:COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateSystemById] Script Date: 06/23/2008 08:04:28 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateSystemById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param @SystemUtilityTypeName nvarchar(255), @ConnectionManagerTypeName nvarchar(255), @EntityInstanceTypeName nvarchar(255), @ErrorCode int OUTPUT&lt;br /&gt;)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock System table so no one can insert a System with the same-- name as the one we may update the existing System to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 2&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_SystemWHERE AR_System.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_System.IdFROM AR_SystemINNER JOIN AR_MetadataObjectON AR_System.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update SystemUPDATE AR_SystemSET SystemUtilityTypeName = @SystemUtilityTypeName, ConnectionFactoryTypeName = @ConnectionManagerTypeName, SystemEntityTypeName = @EntityInstanceTypeNameWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateSystemInstanceById] Script Date: 06/23/2008 08:04:28 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateSystemInstanceById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param @SystemId int, @ErrorCode int OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock SystemInst table so no one can insert a SystemInst with the same-- name as the one we may change the existing SystemInst to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 3 -- system instance table&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_SystemInstanceWHERE AR_SystemInstance.Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesSELECT AR_SystemInstance.IdFROM AR_SystemInstanceINNER JOIN AR_MetadataObjectON AR_SystemInstance.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_SystemInstance.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @SystemInstancesForSystem intSET @SystemInstancesForSystem = (SELECT COUNT (Id) FROM AR_SystemInstance WHERE SystemId = @SystemId)IF( @SystemInstancesForSystem &gt;= 300)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update SystemInstanceUPDATE AR_SystemInstanceSET SystemId = @SystemId WHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;br /&gt;GO/****** Object: StoredProcedure [dbo].[proc_ar_UpdateTypeDescriptorById] Script Date: 06/23/2008 08:04:28 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE Procedure [dbo].[proc_ar_UpdateTypeDescriptorById] ( @Id int, @Name nvarchar(255), @IsCached bit, @Version int OUTPUT, -- input/output param-- @ParameterId int, -- forbid updating this @ParentTypeDescriptorId int, @TypeName nvarchar(255), @IdentifierId int, @FilterDescriptorId int, @IsCollection bit, @ErrorCode int OUTPUT, @ContainsIdentifier bit OUTPUT, @ContainsFilterDescriptor bit OUTPUT)AS&lt;br /&gt;SET @ErrorCode = 0&lt;br /&gt;BEGIN TRANSACTION-- Lock TypeDescriptor table so no one can insert a TypeDescriptor with the same-- name as the one we may update the existing TypeDescriptor to ...SELECT LockIdFROM AR_AdminLocksWITH (HOLDLOCK)WHERE LockId = 12&lt;br /&gt;--before updating make sure that the object existsSELECT IdFROM AR_TypeDescriptorWHERE Id = @Id&lt;br /&gt;IF (@@ROWCOUNT = 0)BEGIN SET @ErrorCode = -2 -- not found GOTO ON_ERROREND&lt;br /&gt;-- Check for duplicatesDECLARE @ParameterId intDECLARE @OldParentTypeDescriptorId intSELECT @OldParentTypeDescriptorId=ParentTypeDescriptorId, @ParameterId=ParameterId FROM AR_TypeDescriptor WHERE Id = @Id&lt;br /&gt;SELECT AR_TypeDescriptor.IdFROM AR_TypeDescriptorINNER JOIN AR_MetadataObjectON AR_TypeDescriptor.Id = AR_MetadataObject.IdWHERE AR_MetadataObject.Name = @Name AND AR_TypeDescriptor.ParameterId = @ParameterId AND AR_TypeDescriptor.ParentTypeDescriptorId IN (SELECT ParentTypeDescriptorId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;) AND AR_MetadataObject.Id != @Id -- don't consider yourself as a duplicate&lt;br /&gt;IF (@@ROWCOUNT &lt;&gt; 0)BEGIN SET @ErrorCode = -1 --Duplicate name GOTO ON_ERROREND&lt;br /&gt;-- Check for MaxDECLARE @ChildTypeDescriptors intDECLARE @TypeDescriptorsRelatedToFilterDescriptor intSET @ChildTypeDescriptors = (SELECT COUNT (Id) FROM AR_TypeDescriptor WHERE ParentTypeDescriptorId = @ParentTypeDescriptorId AND ParameterId = @ParameterId AND @ParentTypeDescriptorId IS NOT NULL)SET @TypeDescriptorsRelatedToFilterDescriptor = (SELECT COUNT (Id) FROM AR_TypeDescriptor WHERE FilterDescriptorId = @FilterDescriptorId AND @FilterDescriptorId IS NOT NULL)IF (@ChildTypeDescriptors &gt;= 300 OR @TypeDescriptorsRelatedToFilterDescriptor &gt;= 10)BEGIN SET @ErrorCode = -3 GOTO ON_ERROREND&lt;br /&gt;-- Check object specific constraintsIF(@ParentTypeDescriptorId IS NULL AND EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE &lt;a href="mailto:ParameterId=@ParameterId"&gt;ParameterId=@ParameterId&lt;/a&gt; AND ParentTypeDescriptorId IS NULL AND Id != @Id))BEGIN SET @ErrorCode = -302 -- cannot have multiple root type descriptors for a single parameter GOTO ON_ERROREND&lt;br /&gt;DECLARE @FilterDescriptorMethodId intDECLARE @TypeDescriptorMethodId intSELECT @FilterDescriptorMethodId=MethodId FROM AR_FilterDescriptor WHERE &lt;a href="mailto:Id=@FilterDescriptorId"&gt;Id=@FilterDescriptorId&lt;/a&gt;SELECT @TypeDescriptorMethodId=MethodId FROM AR_Parameter WHERE Id IN (SELECT ParameterId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;)IF(@FilterDescriptorId IS NOT NULL AND @TypeDescriptorMethodId != @FilterDescriptorMethodId)BEGIN SET @ErrorCode = -303 -- filter doesn't belong to the same method as the typedescriptor GOTO ON_ERROREND&lt;br /&gt;DECLARE @ParentsParameterId intSELECT @ParentsParameterId=ParameterId FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@ParentTypeDescriptorId"&gt;Id=@ParentTypeDescriptorId&lt;/a&gt;IF(@ParentTypeDescriptorId IS NOT NULL AND @ParameterId != @ParentsParameterId)BEGIN SET @ErrorCode = -304 GOTO ON_ERROREND&lt;br /&gt;IF(@IsCollection=1 AND EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE Id = @ParentTypeDescriptorId AND IsCollection = 1))BEGIN SET @ErrorCode = -305 -- cannot have parent-child typedescriptors with IsCollection set GOTO ON_ERROREND&lt;br /&gt;IF(@IsCollection=1 AND EXISTS (SELECT Id FROM AR_TypeDescriptor WHERE ParentTypeDescriptorId = @Id AND IsCollection = 1))BEGIN SET @ErrorCode = -305 -- cannot have parent-child typedescriptors with IsCollection set GOTO ON_ERROREND&lt;br /&gt;IF(@IsCollection=1)BEGIN DECLARE @ChildCount int SELECT @ChildCount=COUNT(Id) FROM AR_TypeDescriptor WHERE ParentTypeDescriptorId = @Id IF(@ChildCount &gt; 1) BEGIN SET @ErrorCode = -306 -- a type desc with IsCollection can have only one child GOTO ON_ERROR ENDEND&lt;br /&gt;-- Optimistic concurrency checkDECLARE @DBVersion intSELECT @DBVersion = VersionFROM AR_MetadataObjectWHERE AR_MetadataObject.Id = @Id&lt;br /&gt;IF (@DBVersion != @Version)BEGIN SET @ErrorCode = -6 GOTO ON_ERROREND&lt;br /&gt;SET @Version = @Version + 1IF @Version &gt;= 2147483646 SET @Version = 0&lt;br /&gt;-- Update MetadataObjectUPDATE AR_MetadataObjectSET Name = @Name, IsCached = @IsCached, Version = @VersionWHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- Update TypeDescriptorUPDATE AR_TypeDescriptorSET ParentTypeDescriptorId = @ParentTypeDescriptorId, TypeName = @TypeName, InterpretedTypeName = @TypeName, ContainsIdentifier = 0, IdentifierId = @IdentifierId, ContainsFilterDescriptor = 0, FilterDescriptorId = @FilterDescriptorId, IsCollection = @IsCollection WHERE Id = @Id&lt;br /&gt;SELECT @ErrorCode=@@Error IF(@ErrorCode &lt;&gt; 0) GOTO ON_ERROR&lt;br /&gt;-- fixup&lt;br /&gt;-- if the parent changes, we may need to fix up the old parentIF( @OldParentTypeDescriptorId != @ParentTypeDescriptorId AND @OldParentTypeDescriptorId IS NOT NULL)BEGIN EXEC proc_ar_TypeDescriptorContainsFlagsFixup @OldParentTypeDescriptorId, @ErrorCode OUTPUT IF(@ErrorCode != 0) GOTO ON_ERROREND&lt;br /&gt;-- now fix up the current node; we could optimize this away by checking if the contains* flags changed in the futureEXEC proc_ar_TypeDescriptorContainsFlagsFixup @Id, @ErrorCode OUTPUTIF(@ErrorCode != 0) GOTO ON_ERROR&lt;br /&gt;-- set up return params; version is included because the fixups may change itSELECT @ContainsIdentifier=ContainsIdentifier, @ContainsFilterDescriptor=ContainsFilterDescriptor FROM AR_TypeDescriptor WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;SELECT @Version=Version FROM AR_MetadataObject WHERE &lt;a href="mailto:Id=@Id"&gt;Id=@Id&lt;/a&gt;&lt;br /&gt;COMMIT TRANSACTIONRETURN&lt;br /&gt;ON_ERROR:IF(@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTIONRETURN&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8948206673859997041-1029146279082060403?l=vladco.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladco.blogspot.com/feeds/1029146279082060403/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8948206673859997041&amp;postID=1029146279082060403' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8948206673859997041/posts/default/1029146279082060403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8948206673859997041/posts/default/1029146279082060403'/><link rel='alternate' type='text/html' href='http://vladco.blogspot.com/2008/06/procarbumpcacheinvalidationcounter.html' title='proc_ar_BumpCacheInvalidationCounter - Final Solution.'/><author><name>Vladco</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>
