Delete trigger and getting field from another table

Tom S

I have this delete trigger on an SQL database. The record deletes currently and gets written to an audit table. I have been asked to include in this history table a field from another table that is related to the record being deleted based on SurveyID. I thought I could do something like

select @Status = Status from table where Survey = deleted.Survey

But this is incorrect syntax.

ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]

after delete
as
Begin
Set nocount on;

Declare @SurveyId int
Declare @StudentUIC varchar(10)
Declare @Status varchar(10)

select @SurveyId = deleted.SurveyID,
        @StudentUIC = deleted.StudentUIC
from deleted

select @Status = Status from tbly when SurveyID = deleted.SurveyID

insert into fupSurveyAudit
    values(@SurveyId,@StudentUIC,@Status)


End    
Gordon Linoff

Arrgh. I think you want this insert in your trigger (and nothing else):

insert into fupSurveyAudit(SurveyId, StudentUIC, status)
    select d.SurveyId, d.StudentUIC, y.status
    from deleted d left join
         tbly y
         on d.SurveyId = y.SurveyId;

Notes:

  • deleted could contain more than one row, so assuming that it has one row can lead to a run-time error or incorrect results.
  • A left join is needed in case there is no matching row for the status.
  • You should always include the columns in an insert
  • Your archive table should have additional columns, such as an identity column and the date of the insert, which are set automatically (and hence not explicitly part of the insert).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

FileMaker - Getting Data From Another Table with Multiple Field Restrictions

From Dev

Trigger to not delete Data from a table

From Dev

SQLite select average from one table and update average field from another table using trigger

From Dev

Trigger with lookup from another table

From Dev

mySQL DELETE from one table using field from natural joining with another table?

From Dev

mySQL DELETE from one table using field from natural joining with another table?

From Dev

inserting from a table to another table and activeting trigger

From Dev

Sql Trigger to "move" a field value to another field on the same table

From Dev

select field from table in another field from another table

From Dev

Not getting ID from another table

From Dev

Trigger to update sum from another table MySQL

From Dev

Trigger to update sum from another table MySQL

From Dev

Trigger that updates the average from another table

From Dev

Trigger Segue from a table cell into another page

From Dev

MYSQL - Trigger - Update field and add the data to another table

From Dev

Getting Additional Field From Different Table

From Dev

Trigger to insert data from source table and another table

From Dev

Mysql Trigger, Copy All Rows From One Table Into Another Table

From Dev

Trigger update the same table with data from another table after insert

From Dev

delete rows from a table that are not present in another

From Dev

Getting values from another table on Ruby on Rails

From Dev

Getting information from another table in same query

From Dev

LinqtoSQL Getting a value from another table

From Dev

Getting id from another table in a relationship laravel

From Dev

Getting a value from another table for multiple columns

From Dev

Field involving multiple rows from another table

From Dev

Adding a field from another table to results

From Dev

Pull field from another table in MVC

From Dev

Pulling a field from another table in Hibernate

Related Related

  1. 1

    FileMaker - Getting Data From Another Table with Multiple Field Restrictions

  2. 2

    Trigger to not delete Data from a table

  3. 3

    SQLite select average from one table and update average field from another table using trigger

  4. 4

    Trigger with lookup from another table

  5. 5

    mySQL DELETE from one table using field from natural joining with another table?

  6. 6

    mySQL DELETE from one table using field from natural joining with another table?

  7. 7

    inserting from a table to another table and activeting trigger

  8. 8

    Sql Trigger to "move" a field value to another field on the same table

  9. 9

    select field from table in another field from another table

  10. 10

    Not getting ID from another table

  11. 11

    Trigger to update sum from another table MySQL

  12. 12

    Trigger to update sum from another table MySQL

  13. 13

    Trigger that updates the average from another table

  14. 14

    Trigger Segue from a table cell into another page

  15. 15

    MYSQL - Trigger - Update field and add the data to another table

  16. 16

    Getting Additional Field From Different Table

  17. 17

    Trigger to insert data from source table and another table

  18. 18

    Mysql Trigger, Copy All Rows From One Table Into Another Table

  19. 19

    Trigger update the same table with data from another table after insert

  20. 20

    delete rows from a table that are not present in another

  21. 21

    Getting values from another table on Ruby on Rails

  22. 22

    Getting information from another table in same query

  23. 23

    LinqtoSQL Getting a value from another table

  24. 24

    Getting id from another table in a relationship laravel

  25. 25

    Getting a value from another table for multiple columns

  26. 26

    Field involving multiple rows from another table

  27. 27

    Adding a field from another table to results

  28. 28

    Pull field from another table in MVC

  29. 29

    Pulling a field from another table in Hibernate

HotTag

Archive