Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Dumps
Starting Member
10 Posts |
Posted - 2006-01-26 : 18:20:57
|
| Go easy on me people, I'm a new to this!I have the following Delete trigger;CREATE TRIGGER dbo.MT_ProjType_CheckProjects ON ProjectTypesFOR DELETEASSET NOCOUNT ON --get id of deleted project type from DELETEDDECLARE @DeletedID intSET @DeletedID = (SELECT projecttypeid FROM DELETED)--see if any projects are of the deleted type DECLARE @CountType int --number of projects of deleted typeSELECT @CountType = count(projecttype)FROM projectWHERE projecttype = @DeletedID --if 0 then no projects of deleted type so returnIF @CountType = 0BEGINRETURNENDELSEBEGIN --see if Unknown already exists as an option in projecttypesDECLARE @UnknownExists int --not 0 if unknown doesn't existSELECT @UnknownExists = count(projecttype)FROM projecttypesWHERE projecttype = 'Unknown'END--if 0 then insert Unknown into projecttypes and give all projects--of deleted type Unknown's corresponding idIF @UnknownExists = 0 BEGIN--insert Unknown and get relevant idDECLARE @IDofUnknown int INSERT INTO projecttypes (projecttype)VALUES ('Unknown')--get ID of unknownSET @IDofUnknown = SCOPE_IDENTITY()--PROBLEM--update all projects of old type with new Unknown IDUPDATE projectSET projecttype = @IDofUnknownWHERE projecttype = @DeletedIDEND--PROBLEMELSEBEGIN--get id of Unknown from projecttypesDECLARE @UnknownID intSELECT @UnknownID = projecttypeidFROM projecttypesWHERE projecttype = 'Unknown'--PROBLEM--update all projects of old type with UnknownIDUPDATE projectSET projecttype = @UnknownIDWHERE projecttype = @DeletedID END--PROBLEMSET NOCOUNT OFFGOEssentially, I'm trying to update all existing rows in table Project where the project type is the same as that being deleted from the table Project types. So if the project type has been used for another project, it is replaced with Unknown (which is inserted if it doesn't already exist in the Project type table).I understand that @DeletedID has to be one value and having checked what is returned it seems to be correct - ie. @DeletedID is the ID of the project type deleted. The trigger works fine when only one existing project is being updated but not for multiple updates. As far as I can tell I'm only using one value for @DeletedID so the update should work fine - shouldn't it????Please help!! |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-01-26 : 18:40:40
|
You have a serious design error in this trigger. A trigger has to be able to handle multiple rows, and you have coded it with the assumption that only one row at a time will be updated.This will generate an error when the deleted table contains more than one row.SET @DeletedID = (SELECT projecttypeid FROM DELETED) Besides this problem, I don't understand why you are doing this. Why allow the deletion of a project type when it is being referenced? It would be better to use a foreign key reference to keep this from happening. Then, if you want to do this, create a stored procedure to update the referencing rows before deleting a project type.CODO ERGO SUM |
 |
|
|
Dumps
Starting Member
10 Posts |
Posted - 2006-01-27 : 14:02:07
|
| Thanks Michael, and I see what you mean. I think I'll have another go at this and do as you suggest. |
 |
|
|
|
|
|