Microsoft SQL Server

Temporary and Global Temporary Table in SQL Server

A local temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.

 CREATE TABLE #TEMPTABLE  
(
Column1 INT,
Column2 VARCHAR(30),
Column3 DATETIME DEFAULT GETDATE()
)


A global temporary
table remains in the database permanently, but the rows exist only within a given connection. When connection is closed, the data in the global temporary table disappears. However, the table definition remains with the database for access when database is opened next time.

 CREATE TABLE ##GLOBALTEMPTABLE  
(
Column1 INT,
Column2 VARCHAR(30),
Column3 DATETIME DEFAULT GETDATE()
)
Microsoft SQL Server

Common Table Expression (CTE) in SQL Server

CTE improves the readability and makes maintenance of complex queries easy.
The query can be divided into separate, simple, logical building blocks which can be then used to build more complex CTEs until final result set is generated.

CTE can be defined in functions, stored procedures, triggers or even views.
After a CTE is defined, it can be used as a Table or a View and can SELECT, INSERT, UPDATE or DELETE Data.

 WITH CTE_name  
AS
(
SELECT column_name1,column_name2 from table_name
where column_name='Himen'
)
FROM CTE_name;
GO
Microsoft SQL Server

Logical Interview Questions for SQL Server

SELECT statements covering all the options.

 SELECT select_list  
 [INTO new_table_] 
 FROM table_source 
 [WHERE search_condition] 
 [GROUP BY group_by__expression] 
 [HAVING search_condition] 
 [ORDER BY order__expression [ASC | DESC] ] 

Identity Insert Syntax for SQL Server

 SET IDENTITY_INSERT IdentityTable ON  
 INSERT IdentityTable(TheIdentity, TheValue) VALUES (3, 'First Row') 
 SET IDENTITY_INSERT IdentityTable OFF 


Deleting duplicate records from table using CTE (Common Table Expression)

 WITH [CTE DUPLICATE] AS   
(
SELECT
RN = ROW_NUMBER() OVER (PARTITION BY CompanyTitle ORDER BY LastContactDate DESC),
Id, CompanyTitle, ContactName, LastContactDate
FROM Suppliers
)
DELETE FROM [CTE DUPLICATE] WHERE RN > 1

Microsoft SQL Server

TRIGGER in SQL Server

A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updating of data. It is a database object which is bound to a table and is executed automatically. You can’t explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.

Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them.

After Triggers
These triggers run after an insert, update or delete on a table. They are not supported for views.
AFTER TRIGGERS can be classified further into three types as:
(a) AFTER INSERT Trigger.  : This trigger is fired after an INSERT on the table.
(b) AFTER UPDATE Trigger. : This trigger is fired after an update on the table.
(c) AFTER DELETE Trigger. : This trigger is fired after a delete on the table.

Instead Of Triggers
These can be used as an interceptor for anything that anyone tried to do on our table or view. If you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless you issue another delete instruction from within the trigger)
INSTEAD OF TRIGGERS can be classified further into three types as:-
(a) INSTEAD OF INSERT Trigger.
(b) INSTEAD OF UPDATE Trigger.
(c) INSTEAD OF DELETE Trigger.