Microsoft SQL Server

CURSOR in SQL Server

Cursor is a database object used by applications to manipulate data in a set on a row-by- row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.

In order to work with a cursor we need to perform some steps in the following order:
Declare cursor
Open cursor
Fetch row from the cursor
Process fetched row
Close cursor
Deallocate cursor

 DECLARE @UserId int -- column name    
DECLARE db_cursor CURSOR FOR
SELECT UserId FROM dbo.Users
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @UserId
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE dbo.Users SET name='Himen' where UserId=@UserId
FETCH NEXT FROM db_cursor INTO @UserId
END
CLOSE db_cursor
DEALLOCATE db_cursor
Microsoft SQL Server

JOINS in SQL Server

Cross Join A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. The common example is when company wants to combine each product with a pricing table to analyze each product at each price.

Inner Join A join that displays only the rows that have a match in both joined tables is known as inner Join. This is the default type of join in the Query and View Designer.

Outer Join A join that includes rows even if they do not have related rows in the joined table is an Outer Join. You can create three different outer join to specify the unmatched rows to be included:

Left Outer Join
: In Left Outer Join all rows in the first-named table i.e. “left” table, which appears leftmost in the JOIN clause are included. Unmatched rows in the right table do not appear.

Right Outer Join
: In Right Outer Join all rows in the second-named table i.e. “right” table, which appears rightmost in the JOIN clause are included. Unmatched rows in the left table are not included.

Full Outer Join
: In Full Outer Join all rows in all joined tables are included, whether they are matched or not.

Self Join
This is a particular case when one table joins to itself, with one or two aliases to avoid confusion. A self join can be of any type, as long as the joined tables are the same. A self join is rather unique in that it involves a relationship with only one table.

The common example is when company has a hierarchal reporting structure whereby one member of staff reports to another. Self Join can be Outer Join or Inner Join.

Microsoft SQL Server

INDEX in SQL Server

An index is a physical structure containing pointers to the data. Indices are created in an existing table to locate rowsmore quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name.

A clustered index is a special type of index that reorders the way records in the table are physically stored. Thereforetable can have only one clustered index. The leaf nodes of a clustered index contain the data pages.
A non clustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a non clustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.

 //Clustered INDEX  
CREATE CLUSTERED INDEX index_name ON table_name(column_name);

//Non Clustered INDEX
CREATE NONCLUSTERED INDEX index_name ON table_name(column_name);

//DROP INDEX Syntax
DROP INDEX table_name.index_name
Microsoft SQL Server

USER-DEFINED FUNCTIONS in SQL Server

Scalar  Value Function
A Scalar-valued function in SQL Server 2012 is used to return a single value of any T-SQL data type.

 CREATE FUNCTION dbo.Calculate(@val1 int, @val2 int)  
RETURNS int
AS
BEGIN
DECLARE @val3 int;
SELECT @val3 = @val1 + @val2;
RETURN @val3;
END


Multi Table Value Function

1. Declare a table variable that is to be returned.
2. Must have a BEGIN/END block.
3. Inside the BEGIN/END block you need code that populates the table variable.
4. You return from the function.

 CREATE FUNCTION Util.MyMTVFunction (@Parameters INT)  
RETURNS @FunctionResultTableVariable TABLE (N INT)
AS
BEGIN
INSERT INTO @FunctionResultTableVariable (N)
SELECT column_name1,column_name2,...
FROM table_name
ORDER BY column_name ase|desc;
RETURN;
END
GO

In-line Table Value Function
1. Returns a select statement – there is no table variable to mess around with, no inserts, no code blocks.

 CREATE FUNCTION Util.MyITVFunction (@Parameters INT)  
RETURNS TABLE
AS
RETURN
SELECT column_name1,column_name2,...
FROM table_name
ORDER BY column_name ase|desc;
GO