|
All, I am working on a procedure and I am kind of stuck at a point. I have pasted the code below. I understand the code can be improved a lot.. but, I need to see that it is covering all the requirements. The issue I am facing is.. after I wrote this procedure I understood that I have to disable a constraint before I run this procedure and enable it after this procedure is executed. But I want to incorporate that alter table commands in this procedure itself. I wonder where do I put it in the code below. Another idea I had was to write the drop and creation of the constraints in a separate procedures and call them in this procedure.. but I am not sure how I can do it. These are novice ideas, that I have. I would appreciate if experts help me out. I also wanted to rollback the whole procedure, if at all any error occurred in any of the Begin- end blocks. any comments or suggestions on the code I would take as if I learnt something new :). I have not figured out yet how this code would affect the performance yet. The code that I have written below, propagates same id across different tables, which are connected through constraints. the effort was to minimize writing 6 insert statements explicitly into 6 different tables. Any help is appreciated.
(comments are locked)
|
|
OK, requirements are requirements, and if the circular reference must stay then here is how your procedure might look. The only things I needed to tweak are:
Here is the abridged script: Oleg
(comments are locked)
|
|
i kind of modified the code and now i am able to rollback the inserted records if anyerrors occur in the following way. But i still wonder how to tweak the constraints in the same procedures. create PROCEDURE [dbo].[CREATEASSET_MODIFIED] ) AS END declare @id int exec dbo.CREATEASSET_MODIFIED_Auyu ASSET,UNIT,823,@id output,182,182,0,0,0,0,'12-1-2010','12-1-2010',1 print @id
(comments are locked)
|


@Katie Could you please elaborate why do you need to disable a constraint and then re-enable it within the body of the procedure? Generally speaking, this is not a good option, specifically when your tables grow in size enough so re-enabling a constraint becomes a very time-consumming process. Maybe there are other ways to tackle your problem, but it would be nice if you provided more information. Thank you.
Oleg, there is this table called asset container,which has only one column called asset container id . Something like this.
ALTER TABLE [cim].[AssetContainer] WITH CHECK ADD CONSTRAINT [FK_AssetContainer_Asset] FOREIGN KEY([assetContainerID]) REFERENCES [dbo].Asset GO
and again
ALTER TABLE [cim].[Asset] WITH CHECK ADD CONSTRAINT [FK_AssetContainer] FOREIGN KEY([AssetContainer]) REFERENCES [dbo].AssetContainer GO
@Katie So, you have cim.Asset references dbo.AssetContainer and cim.AssetContaier references dbo.Asset (4 tables), right?
You query the last IdentifiedObjectId in IdentifiedObject table, increment it by 1 and then insert incremented value back into IdentifiedObject. The problem now is that when you try to insert that incremented @id value as a value of assetContainerID column of the cim.AssetContainer table, this value has to exist as a valid AssetID of existing record in dbo.Asset table, and this will always cause the trouble if such record does not yet exist. Even if you disable FK_AssetContainer constraint at this point, it is not going to solve your problem because in the end you will have to re-enable it and this will fail if there is no record with assetID = @id in the dbo.Asset table. Since your procedure does not do anything to dbo.Asset and dbo.AssetContainer tables, please give a little bit more information about the role of the IdentifiedObject table and the purpose of the cim schema.
@oleg, Firstly, thank you for being soo patient with me to understand my problem. Sorry for the confusion. there are only two tables cim.asset and cim.assetcontainer.
cim.asset table has (assetid, assetcontianerid) cim.assetcontainer has (assetcontainerID)
ALTER TABLE [cim].[AssetContainer] WITH CHECK ADD CONSTRAINT [FK_AssetContainer_Asset] FOREIGN KEY([assetContainerID]) REFERENCES [cim].Asset GO
ALTER TABLE [cim].[Asset] WITH CHECK ADD CONSTRAINT [FK_AssetContainer] FOREIGN KEY([AssetContainer]) REFERENCES [cim].AssetContainer GO
for now to get my procedure working, i have to asssume that the objectidentifierid = assetid = assetcontainerid
for this to happend unless i drop the constraint below, i cannot insert the data. ALTER TABLE [cim].[AssetContainer] WITH CHECK ADD CONSTRAINT [FK_AssetContainer_Asset] FOREIGN KEY([assetContainerID]) REFERENCES [cim].Asset GO
i am not worrying about the performance because.. any at given time there are only few hundreds of records in these tables. and only once few months or a year one record is inserted.
Thank you so much !
@Katie But if you only have 2 tables then your constraints cause circular reference, and thus, there is no way to insert data in either of these 2 tables. If you try to insert into Asset first then you cannot because the assetcontianerid value is not valid in AssetContainer table yet, but if you instead try to insert into AssetContainer then you cannot because the AssetContainerID is not valid existing AssetID column of the Asset table. This makes it look as what you really need is to ensure that whenever the record is inserted into one of these tables, the other needs to have a record inserted too. If I am correct, please let me know which of the 2 tables you would consider as a parent table, and then we can figure out which constraints to toggle so your procedure starts working, and after that we can toggle the proc itself to ensure that it behaves like you want (either inserts all records or inserts none and rolls back).