| Author |
Topic |
|
andros30
Yak Posting Veteran
80 Posts |
Posted - 2008-01-14 : 09:42:49
|
| These are my tables:Table A: ColumnID, ColumnName, ColumnOffice, ColumnComment,...Table B: ColumnID, ColumnNameWhat needs to happen: If ColumnID in Table A is edited, update ColumnName in Table A with what is in Table B.What I have so far:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER trAUpdate ON [Table A] FOR UPDATEAS IF EXISTS (SELECT * FROM inserted i INNER JOIN deleted d ON d.[Office] = i.[Office] WHERE i.ColumnID <> d.ColumnID)BEGIN SET NOCOUNT ON; UPDATE [Table A] SET [Table A].ColumnName = b.ColumnName FROM [Table A] a INNER JOIN [Table B] b ON b.ColumnID = a.ColumnID WHERE a.ColumnID = (SELECT i.ColumnID FROM inserted i)ENDGOError I'm getting:"This row was successfully committed to the database.However, a problem occurred when attempting to retrieve the data back after the commit.Because of this, the displayed data in this row is read-only.To fix the problem, please re-run the query." |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-14 : 09:56:29
|
| do like this:-SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER trAUpdateON [Table A] FOR UPDATEAS SET NOCOUNT ON;IF UPDATE(ColumnID)BEGINUPDATE [Table A]SET [Table A].ColumnName = b.ColumnNameFROM [Table A] aINNER JOIN [Table B] bON b.ColumnID = a.ColumnIDINNER JOIN inserted iON i.ColumnID = b.ColumnIDENDGO |
 |
|
|
andros30
Yak Posting Veteran
80 Posts |
Posted - 2008-01-14 : 11:28:11
|
| That did not work. I still get the same error. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-14 : 11:34:46
|
| did you check whether update happened? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-14 : 11:52:31
|
| Also try after executing this command :-ALTER DATABASE yourDB SET RECURSIVE_TRIGGERS OFFthis will prevent recursive trigger actions |
 |
|
|
andros30
Yak Posting Veteran
80 Posts |
Posted - 2008-01-14 : 11:52:34
|
quote: Originally posted by visakh16 did you check whether update happened?
Table ARow ID Name Comment1 4 YellowTeam Notes2 5 RedTeam Notes5 6 BlueTeam NotesTable B4 YellowTeam5 RedTeam6 BlueTeamWhat's happening is in Table A when I change the ID of 4 to 5, the YellowTeam is not changed to RedTeam. The error comes up, the row turns grey and then when you exit and go back into the table, the row is no longer there.In my original trigger while I was testing, I had it working partially. When I changed the ID, it changed the name but it changed ALL the names in all rows regardless of ID. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-14 : 12:07:11
|
quote: Originally posted by andros30
quote: Originally posted by visakh16 did you check whether update happened?
Table ARow ID Name Comment1 4 YellowTeam Notes2 5 RedTeam Notes5 6 BlueTeam NotesTable B4 YellowTeam5 RedTeam6 BlueTeamWhat's happening is in Table A when I change the ID of 4 to 5, the YellowTeam is not changed to RedTeam. The error comes up, the row turns grey and then when you exit and go back into the table, the row is no longer there.In my original trigger while I was testing, I had it working partially. When I changed the ID, it changed the name but it changed ALL the names in all rows regardless of ID.
Guess it may be due to recursive action . Can you try setting it to off and then executing the update?ALTER DATABASE dbnameSET RECURSIVE_TRIGGERS OFF |
 |
|
|
andros30
Yak Posting Veteran
80 Posts |
Posted - 2008-01-15 : 15:24:28
|
quote: ALTER DATABASE dbnameSET RECURSIVE_TRIGGERS OFF
When I add this into the trigger, it doesn't allow me to execute. The error is "RECURSIVE_TRIGGERS is not a recognized option.".Let me clarify also that in Table A there may be multiple rows with the same name and ID. The trigger is supposed to only change the name in the row that the ID is edited. |
 |
|
|
andros30
Yak Posting Veteran
80 Posts |
Posted - 2008-01-17 : 09:31:01
|
quote: Originally posted by visakh16 do like this:-SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER trAUpdateON [Table A] FOR UPDATEAS SET NOCOUNT ON;IF UPDATE(ColumnID)BEGINUPDATE [Table A]SET [Table A].ColumnName = b.ColumnNameFROM [Table A] aINNER JOIN [Table B] bON b.ColumnID = a.ColumnIDINNER JOIN inserted iON i.ColumnID = b.ColumnIDENDGO
For some reason this trigger is working but only through an interface. However, if you attempt to change the values directly from the database then you get the error that I mentioned. Any clues as to why that is? |
 |
|
|
|