Posts

Showing posts from February, 2015

SQL HAVING

The  HAVING  clause is used in combination with the  GROUP BY  clause and the SQL aggregate functions. HAVING  clause allows us to call the data conditionally on the column that return from using the SQL aggregate functions. The SQL  HAVING  syntax is simple and looks like this: SELECT  [COLUMN NAME 1] ,  AGGREGATE FUNCTION   (  [COLUMN NAME 2]  ) FROM  [TABLE NAME] GROUP BY [COLUMN NAME 1] HAVING   AGGREGATE FUNCTION   (  [COLUMN NAME 2]  )  = [CONDITION] EXAMPLE : Let’s say, we want to get the departments from  GameScores  that total scores is more than 4000. Table  GameScores PlayerName Department Scores Jason IT 3000 Irene IT 1500 Jane Marketing 1000 David Marketing 2500 Paul HR 2000 James HR 2000 SQL statement : SELECT  Department,  SUM ( Scores ) FROM  GameScores GROUP BY  Department HAVING   SUM ( Scores ) >4000 Result: ...

Why use triggers in Microsoft SQL Server?

What are triggers: Triggers are a special type of stored procedure which are executed automatically based on the occurrence of a database event. These events can be categorized as Data Manipulation Language (DML) and Data Definition Language (DDL) events. The benefits derived from triggers is based in their events driven nature. Once created, the trigger automatically fires without user intervention based on an event in the database. A) Using DML Triggers: DML triggers are invoked when any DML commands like INSERT, DELETE, and UPDATE happen on the data of a table and or view. Points to remember: DML triggers are powerful objects for maintaining database integrity and consistency. DML triggers evaluate data before it has been committed to the database. During this evaluation following actions are performed. Compare before and after versions of data Roll back invalid modification Read from other tables ,those in other database Modify other tables, including ...