Tuesday, October 1, 2019

What is Cursor in SQL Server with Example

Here I will explain what is cursor in sql server with example or simple cursor example in sql server and how to declare and use cursor in sql server. Cursor in sql server is used to retrieve set of data from table, loop through each record row by row, and modify the values based on requirements.
Description:
In previous articles I explained While loop example in sql server, nested while loop example in sql server, get only month and year from date in sql server, substring function in SQL server and many articles relating to SQL server. Now I will explain cursor in sql server with example.

To use cursor in sql server that syntax will be like as shown below

Syntax to Create Cursor


DECLARE cursorname CURSOR
FOR selectstatement -- like SELECT OrderID,CustomerID FROM Orders
OPEN cursor
FETCH tablerow FROM cursor
Process Fetched Tablerow
CLOSE cursor
DEALLOCATE cursor
If you observe above syntax we have different steps in cursor

Step 1: Declare Cursor

First we need to declare cursor name that will be used in cursor execution

Step 2: Select Statement

Select statement is used to get data from table to process with cursor

Step 3: Open Cursor

This statement is used to open the cursor to process select statement result set


Step 4: Fetch Rows

Once cursor is opened, rows can be fetched from the cursor one by one

Step 5: Close Cursor

Once our process finished we need to close the cursor

Step 6: Deallocate Cursor

We need to deallocate cursor to delete cursor definition and release resources associated with the cursor

We will check this with example for that first create one table UserDetails in your database and insert some 100 or 200 records or install northwind database in your server and use orders table it contains more than 500 records

Example

Write cursor script like as shown below and run it.


DECLARE ex_cursor CURSOR
FOR SELECT OrderID,CustomerID FROM Orders
DECLARE @oid INT
DECLARE @cname NVARCHAR(50)
OPEN ex_cursor
FETCH NEXT FROM ex_cursor INTO @oid,@cname
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT  (CAST(@oid AS VARCHAR(5)) + '-' + @cname)
FETCH NEXT FROM ex_cursor INTO @oid,@cname
END
CLOSE ex_cursor
DEALLOCATE ex_cursor
Output:

When we run above query our results like as shown below

Demo

what are the sealed classes in c# | uses of sealed keyword in c# |Sealed classes example in c# | Object-Oriented Programming: Sealed Classes

Here I will explain what are the sealed classes in c# and uses of sealed classes in c#.

Description:

In previous article I explained clearly about OOPS concepts now I will explain what is the use of sealed classes in c#. Generally if we create classes we can inherit the properties of that created class in any class without having any restrictions. In some situation we will get requirement like we don’t want to give permission for the users to derive the classes from it or don’t allow users to inherit the properties from particular class in that situations what we can do? 

For that purpose we have keyword called “Sealed” in OOPS. When we defined class with keyword “Sealed” then we don’t have a chance to derive that particular class and we don’t have permission to inherit the properties from that particular class.


Example to declare class as sealed 

sealed class Test
{
public int Number;
public string Name;
}
If the class declared with an access modifier, the Sealed keyword can appear after or before the public keyword. 

Example

public sealed class Test
{
public int Number;
public string Name;
}

Here we can declare a class that is derived from another class can also be sealed. Check below example

public sealed class Test
{
public int Number;
public string Name;
}

public sealed class child1:Test
{
public string Name;
}









Example to use Sealed Keyword First create console application in C# and write the following code in Program.cs file and test it once.
  
using System;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Irregular tri = new Irregular(42.73, 35.05);
tri.Describe("Irregular");
}
}
public abstract class Triangle
{
private double bs;
private double hgt;
















public Triangle(double length , double height)

{
bs = length;
hgt = height;
}

public virtual double Area()
{
return bs * hgt / 2;
}

public void Describe(string type)
{
Console.WriteLine("Triangle - {0}", type);
Console.WriteLine("Base:   {0}", bs);
Console.WriteLine("Height: {0}", hgt);
Console.WriteLine("Area:   {0}", Area());
}
}

sealed public class Irregular : Triangle
{
public Irregular(double Base, double Height): base(Base, Height)
{
}
}
}








































Output of above sample will be like this














From above example we can easily say that sealed classes can’t inherit or acquire properties from parent class. 

Another Important information is if we create a static class, it becomes automatically sealed. This means that you cannot derive a class from a static class. So, the sealed and the static class have in common that both are sealed. The difference is that you can declared a variable of a sealed class to access its members but you use the name of a static class to access its members.

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...