An alternative to triggers in SQL Server

To fix potential trigger issues, what are some of the alternatives you can use to get the same response functionality for an event triggered by an INSERT action?

I have a database that must have additional values ​​added to insert. INSERT is controlled by compiled code and cannot be changed.

EXAMPLE: a program inserts a string, and from this string I need to specify an integer in a new field that points to a lookup table.

If there is an alternative to a trigger, please let me know some of the pros and cons of any alternative. The main reason for this is that triggers are not allowed in our database standards.

SQL Server 2008 Enterprise

+3
source share
8 answers

How do you determine your integer based on an inserted string?

Alternatively, you may need computed columns in SQL Server . If this match is fairly simple (for example, extracting a character from 10 to 14 from a string) or something like that, you can create a calculated column to do this automatically - no trigger is needed.

You can even save these calculated columns (physically saved as part of your tables) and create indexes in these fields!

Computed columns are available from SQL Server 2000 on, SQL Server 2005 columns are saved.

+3
source

, . insert, . , - .

, , . , , .

- . (, , ), .

+6

(, , ) SQL; , , , - . , , , , , , - , , . , , . , , INSERT, .

+1

SQL Server 2005 -, OUTPUT, INSERT ( ). . , INSERT, - ...

INSERT INTO Contact
(FirstName, MiddleName, LastName)

OUTPUT INSERTED.ContactID, INSERTED.FirstName, INSERTED.MiddleName, INSERTED.LastName
INTO Contact_Audit    
VALUES
(@@SCOPE_IDENTITY, 'Joe', 'D.', 'Schmoe')

.

+1

, . SQL Server 2008 " " MSDN. , 2008 R2 - " " . ( ), , "" .

+1

?

BEGIN
 -- DO YOUR INSERT CODE

  IF @@ERROR = 0
    -- DO THE OTHER STUFF?

. , , .

-1

Your options here are limited. I think your only alternative is to make your inserts through a call to a stored procedure and put additional code in the stored procedure.

-2
source

I think we can implement a trigger with the hibernate event system now, despite the impact of performance. I have not done this before. But I think it works

-2
source

Source: https://habr.com/ru/post/1728680/


All Articles