Update Inserted Record with Trigger

There are some cases that you can not change your code if you have an ancient project. If it causes to insert wrong values to db, you may want to validate the value is ok and if not, correct it. We can use db trigger for such cases.


CREATE TRIGGER [dbo].[triggerName]
ON [dbo].[TableName]
FOR INSERT
AS
BEGIN
DECLARE @Name AS varchar(5000)
DECLARE @Category AS varchar(50)
DECLARE @ID AS int
SET @FirstName=(select TestID from inserted)
SET @ID = (select ResultID from inserted)

IF @Category = 'CARS'
BEGIN
SET @Name=(select name from inserted)
IF LEN(@Name) != 50 -- Our condition is the length of the name must be 50 if not, that means it has 0s and we need to replace 0s.
BEGIN
-- 'Lentgth is incorrect:' + Cast(LEN(@Name) as varchar(50))
IF CHARINDEX('0',@Name)>0 -- If name contains any 0s, update those 0s with 'X'  - here we update the table record
BEGIN
PRINT 'Includes Zero'
UPDATE TableName
SET Items = Replace(@Name,'0','X')
WHERE ID = @ID
END
END

END
-- 'IT IS NOT CATEGORY = CARS
END

Important thing here is the type of trigger FOR INSERT

This type runs after each insert operation.

Learn more here:  https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql

Multiple arguments in SQL IF:

DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME

SET @StartDate = NULL
SET @EndDate = NULL

IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL) 
    BEGIN
        Select 'This works just fine' as Msg
    END
Else
    BEGIN
    Select 'No Lol' as Msg
    END

#sql # mssql #db-trigger, #insert-trigger, #sql-trigger

Advertisements

Why Brackets in SQL queries with SQL Server

It’s been long time since I worked with SQL Server last time. I have been working with Oracle and there are differences for sure. This one is simple.

  1. The brackets are required if you use keywords or special chars in the column names or identifiers. You could name a column [First Name] (with a space)–but then you’d need to use brackets every time you referred to that column.
  2. They’re handy if your columns have the same names as SQL keywords, or have spaces in them.Example:create table test ( id int, user varchar(20) )
    Oh no! Incorrect syntax near the keyword ‘user’. But this:

    create table test ( id int, [user] varchar(20) )
    Works fine.

  3. Regardless of following a naming convention that avoids using reserved words, Microsoft does add new reserved words. Using brackets allows your code to be upgraded to a new SQL Server version, without first needing to edit Microsoft’s newly reserved words out of your client code. That editing can be a significant concern. It may cause your project to be prematurely retired….Brackets can also be useful when you want to Replace All in a script. If your batch contains a variable named @String and a column named [String], you can rename the column to [NewString], without renaming @String to @NewString.

Source: http://stackoverflow.com/questions/52898/what-is-the-use-of-the-square-brackets-in-sql-statements

#sqlserver