Thursday, 22 March 2018

Having clause

Having clause
Having clause is similar to the where clause but it is used in the conjuction with group by clause in select statement
Group by clause with having clause used to filter the data or recordset.

Having clause folloed by group by and preced order by clause if used.

Following is the position of having clause

Select
From
Where
Group by
Having
Order by

Eg.
select TitleOfCourtesy from Employees
group by TitleOfCourtesy,BirthDate
having BirthDate >= CONVERT(datetime,'1950-10-12',101)


select TitleOfCourtesy from Employees
group by TitleOfCourtesy,BirthDate

having BirthDate >= '1960-10-12'

Monday, 19 March 2018

SQL Joins

SQL Join

SQL join is used to get the data from two or more table in the database.
A relation between the table is maintained by using the PK and FK reference.
 We can retrieve the data from two or more table using joins which containg some common values in both the table.

SQL join types

Inner join -  Retrieve the data from two or more table when there is matching records in both the table.


select a.FirstName,a.LastName, b.OrderID,b.ShipName,b.CustomerID
from
Employees a
inner join
Orders b
on
a.EmployeeID = b.EmployeeID
order by a.FirstName

Left join -  Retrive all the data from left table with matching records on right table


select a.FirstName,a.LastName, b.OrderID,b.ShipName,b.CustomerID
from
Employees a
left join
Orders b
on
a.EmployeeID = b.EmployeeID
order by a.FirstName

Right join – Retrive all the data from right table with matching records on left table.


select a.FirstName,a.LastName, b.OrderID,b.ShipName,b.CustomerID
from
Employees a
right join
Orders b
on
a.EmployeeID = b.EmployeeID
order by a.FirstName

Full join -  retrive data when there is matching records in one of the table.


select a.FirstName,a.LastName, b.OrderID,b.ShipName,b.CustomerID
from
Employees a
full join
Orders b
on
a.EmployeeID = b.EmployeeID
order by a.FirstName

Self join - is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.

Cartesian join - returns the Cartesian product of the sets of records from the two or more joined tables.

Friday, 16 March 2018

Cursor

Cursor

A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it.
This temporary work area is used to store the data retrieved from the database, and manipulate this data. A cursor can hold more than one row, but can process only one row at a time. The set of rows the cursor holds is called the active set.

There are two types of cursors in PL/SQL:

Implicit cursors
These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are executed. They are also created when a SELECT statement that returns just one row is executed. 

Explicit cursors
They must be created when you are executing a SELECT statement that returns more than one row. Even though the cursor stores multiple records, only one record can be processed at a time, which is called as current row. When you fetch a row the current row position moves to next row.

The cursor attributes available are
 %FOUND
 %NOTFOUND
 %ROWCOUNT
 %ISOPEN.
The status of the cursor for each of these attributes are defined in the below table. 
Attributes
Return Value
Example
%FOUND
The return value is TRUE, if the DML statements like INSERT, DELETE and UPDATE affect at least one row and if SELECT ….INTO statement return at least one row.
SQL%FOUND
The return value is FALSE, if DML statements like INSERT, DELETE and UPDATE do not affect row and if SELECT….INTO statement do not return a row.
%NOTFOUND
The return value is FALSE, if DML statements like INSERT, DELETE and UPDATE at least one row and if SELECT ….INTO statement return at least one row.
SQL%NOTFOUND
The return value is TRUE, if a DML statement like INSERT, DELETE and UPDATE do not affect even one row and if SELECT ….INTO statement does not return a row.
%ROWCOUNT
Return the number of rows affected by the DML operations INSERT, DELETE, UPDATE, SELECT
SQL%ROWCOUNT


For Example: Consider the PL/SQL Block that uses implicit cursor attributes as shown below:
DECLARE  var_rows number(5);
BEGIN
  UPDATE employee
  SET salary = salary + 1000;
  IF SQL%NOTFOUND THEN
    dbms_output.put_line('None of the salaries where updated');
  ELSIF SQL%FOUND THEN
    var_rows := SQL%ROWCOUNT;
    dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');
  END IF;
END;
In the above PL/SQL Block, the salaries of all the employees in the ‘employee’ table are updated. If none of the employee’s salary are updated we get a message 'None of the salaries where updated'. Else we get a message like for example, 'Salaries for 1000 employees are updated' if there are 1000 rows in ‘employee’ table. 

Friday, 9 March 2018

Use of caching in Asp.net (GidView Demo)

Post Content
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.IO;
using System.Web.Caching;
public partial class _Default : System.Web.UI.Page
{
    string strXmlAllDetails = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getData();
        }
    }
    public DataSet getData()
    {
        DataSet dsAllDetailsOutput = (DataSet)Cache["mydata"];
        dsAllDetailsOutput = new DataSet();
        WebServiceDemoExample objWebServiceDemoExample = new WebServiceDemoExample();
        string strXmlOutput = objWebServiceDemoExample.FetchAllDetailsAdmin();
        XmlReader objXmlReader = System.Xml.XmlReader.Create(new StringReader(strXmlOutput));
        dsAllDetailsOutput.ReadXml(objXmlReader);
        Cache.Insert("mydata", dsAllDetailsOutput, new CacheDependency(Server.MapPath("")), DateTime.Now.AddHours(12), Cache.NoSlidingExpiration);

        if (dsAllDetailsOutput.Tables.Count > 0)
        {
            if (dsAllDetailsOutput.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = dsAllDetailsOutput;
                GridView1.DataBind();

            }
        }
        return dsAllDetailsOutput;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataSet ds = (DataSet)Cache["mydata"];
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}