Create a “Last Modified” Column in SQL Server

Some database tables include a “last modified” column, which stores the date and time that the row was last updated. Each time the row is updated, the date is updated to reflect the date and time of that update.

In SQL Server, you can use a trigger to perform this update.

A trigger is a special type of stored procedure that automatically runs when an event occurs in the database server.

You can use the CREATE TRIGGER statement to create a trigger when using T-SQL. This statement can be used to create a DML, DDL, or logon trigger.

Continue reading

SQL Server IF vs IIF(): What’s the Difference?

Students learning a new programming language will often start their first lesson with an IF statement, where their statement will return a value only if the expression is true.

They might then progress to an IF... ELSE statement, where they can determine another value to return if the expression is false. So therefore, return one value if the expression is true, and another value if it’s false.

SQL Server certainly includes the IF... ELSE statement in its T-SQL toolbox.

SQL Server also includes the IIF() function, which does a similar thing, but with a more concise syntax.

But there are some subtle differences.

Continue reading