Saturday, 28 July 2012

asp.net fastest store procedure class data insert and pic by fast way


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class RegistrationUpdate : System.Web.UI.Page
{
    dataUtility appReg = new dataUtility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            regMstData();
        }

    }
    private DataTable OpenTable(string cExpr, string cWhere)
    {
        string cExcute = "";
        DataTable _tConfE = new DataTable();
        switch (cExpr)
        {
            case "Varify"://---varify email 0->1 taken reg mst data
                cExcute = "Exec sp_register 2, '" + cWhere + "'";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(cExcute))
        {
            _tConfE = appReg.executeStoteProc(cExcute);
        }
        return _tConfE;
    }

    private void regMstData()
    {
        string cConReg = Request.QueryString["confirm"].ToString();
        DataTable _tRegMst = OpenTable("Varify", cConReg.Trim());//--after do check already confirm
        RegMstlbl(_tRegMst);//--label text fill
    }
    private void RegMstlbl(DataTable tRegMst)
    {//-- label value data souce value from table
        foreach (DataRow _dr in tRegMst.Rows)
        {
            lblUserName.Text=(_dr["first_name"]+ " "+_dr["last_name"]).ToString();
            lblDbo.Text = (_dr["date_of_birth"]).ToString();
            lblGender.Text = (_dr["gender"]).ToString();
            lblEmail.Text = (_dr["email"]).ToString();
            lblVarify.Text = (_dr["varify"]).ToString();
            lblrow_id.Text = (_dr["row_id"]).ToString();
        }
    }
}
//----------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net.Mail;

public partial class registration : System.Web.UI.Page
{
    dataUtility appReg = new dataUtility();
    protected void Page_Load(object sender, EventArgs e)
    {
       

    }
 
    private void SaveRegistration()
    {
        SqlParameter[] par = new SqlParameter[6];
        par[0] = appReg.addPara("@first", SqlDbType.VarChar, txtfName.Text.Trim().ToString());
        par[1] = appReg.addPara("@last", SqlDbType.VarChar, txtlName.Text.Trim().ToString());
        par[2] = appReg.addPara("@date_of_birth", SqlDbType.Date, txtDob.Text.Trim().ToString());
        par[3] = appReg.addPara("@gender", SqlDbType.NChar, txtGender.Text.Trim().ToString());
        par[4] = appReg.addPara("@email", SqlDbType.VarChar, txtEmail.Text.Trim().ToString());
        par[5] = appReg.addPara("@password", SqlDbType.VarChar, txtPassword.Text.Trim().ToString());

        appReg.insStoreProc("sp_registration_save", par);//---store proc save data in registrationMst/ login
        SendMail(txtEmail.Text.Trim().ToString());

        lblMsg.Text = "Registration Successfully Create. Go to Mail and Confirm your account Information";
    }
    private void SendMail(string email)
    {//--send email and confirm mgs 
        string _conf = confirmEmail();//=== confirm code
        string strMsgBody = "<table style='width: 702px; border-raduis:5px; background-color:#b6776; color:Gray; height: 200px;'><tbody>" +
                    "<tr> <td width='100'>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     :</td><td>" + txtfName.Text.Trim().ToString() + " " + txtlName.Text.Trim().ToString() + "</td></tr>" +
                   "<tr> <td>Email id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :</td><td>" + txtEmail.Text.Trim() + "</td></tr>" +
                    "<tr> <td>password&nbsp;&nbsp; :</td><td>" + txtPassword.Text.Trim() + "</td></tr>" +
                    "<tr> <td>&nbsp;</td><td>please link blow for confirmation of your account</td></tr>" +
                    "<tr> <td>&nbsp;</td><td>" + _conf + "</td></tr>" +
                    "<tr> <td>&nbsp;</td><td>&nbsp;</td></tr>"+
                    "<tr> <td>&nbsp;</td><td>more information go to <a href='http://zerodegreeentertainment.in/' target='_blank'>http://zerodegreeentertainment.in </a> </td></tr>"+
                    "</tbody></table>";
            MailAddress SendFrom = new MailAddress("********");
            MailAddress SendTo = new MailAddress(email);
            MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
            MyMessage.IsBodyHtml = true;
            MyMessage.Subject = "confirm Registration of zerodegree";
            MyMessage.Body = strMsgBody;
            System.Net.NetworkCredential authentication = new System.Net.NetworkCredential("*******", "*********");
            SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
            client.EnableSsl = true;
            client.UseDefaultCredentials = true;
            client.Credentials = authentication;
            client.Send(MyMessage);
             
    }
    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {
        //-- registration submit
        SaveRegistration();

    }
    private DataTable OpenTable(string cExpr, string cWhere)
    {
        string cExcute = "";
        DataTable _tConfE = new DataTable();
        switch (cExpr)
        {
            case "ConfirmRow":
                cExcute = "Exec sp_register 1, '" + cWhere + "'";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(cExcute))
        {
           _tConfE= appReg.executeStoteProc(cExcute);
        }
        return _tConfE;
    }
    private string confirmEmail()
    {
        string cConfirmE = null;
        DataTable tConfE = OpenTable("ConfirmRow",txtEmail.Text.Trim().ToString());
        if (tConfE.Rows.Count>0)
        {
            cConfirmE = "http://zerodegreeentertainment.in/RegistrationUpdate.aspx?confirm=";
            cConfirmE += tConfE.Rows[0][0].ToString();
        }
        return cConfirmE;
    }
}

sql connection configuration class


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
/// Summary description for dataUtility
/// </summary>
public class dataUtility
{
    # region All Module level variables
    SqlDataAdapter Da;
    SqlConnection Con;
    SqlCommand Cmd;
    DataSet ds;

    # endregion
public dataUtility()
{
//
// TODO: Add constructor logic here
//
}
     private void OpenConnection()
    {
        if (Con == null)
        {
            Con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["zeroCon"].ToString());
        }
        // lets open the connection

        if (Con.State == ConnectionState.Closed)
        {
            Con.Open();
        }

        Cmd = new SqlCommand();

        //Set active connection with the command object

        Cmd.Connection = Con;

    }



    private void CloseConnection()
    {
        if (Con.State == ConnectionState.Open)
        {
            Con.Close();
        }
    }
    /// <summary>
    /// This is to release the memory from the connection object
    /// </summary>
    private void DisposeConnection()
    {
        if (Con != null)
        {
            Con.Dispose();
            Con = null;
        }
    }
    public int ExecuteSql(String Sql)
    {
        OpenConnection();
        //int Result;
        //Set command object properties

        Cmd.CommandType = CommandType.Text;
        Cmd.CommandText = Sql;
        Cmd.CommandTimeout = 3000;

        // Now call the method

        int Result = Cmd.ExecuteNonQuery();

        CloseConnection();
        DisposeConnection();

        return Result;
    }
    public bool IsExist(string sql)
    {

        OpenConnection();
        //now set command object properties
        Cmd.CommandType = CommandType.Text;
        Cmd.CommandText = sql;
        Cmd.CommandTimeout = 2000;

        //Execute Command object method
        int Result = (int)Cmd.ExecuteScalar();


        CloseConnection();
        DisposeConnection();

        if (Result > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public SqlDataReader GetDataReader(String Sql)
    {
        OpenConnection();
        SqlDataReader dReader;
        Cmd.CommandType = CommandType.Text;
        Cmd.CommandText = Sql;
        Cmd.CommandTimeout = 1000;

        dReader = Cmd.ExecuteReader();
        //CommandBehavior.CloseConnection
        return dReader;
    }
    public DataTable GetDataTable(String sql)
    {
        DataTable dt = new DataTable();
        try
        {
            OpenConnection();
         

            Da = new SqlDataAdapter();
            Cmd.CommandType = CommandType.Text;
            Cmd.CommandText = sql;
            Cmd.CommandTimeout = 2000;

            Da.SelectCommand = Cmd;
            //ds = new DataSet();
            Da.Fill(dt);
            //dt = ds.Tables["table"];
            CloseConnection();
            return dt;
        }
        catch
        {
            return dt;
        }
    }
    public DataTable returnDataTable(string sql)
    {
        //Con.Open();
        ds = new DataSet();
        Da = new SqlDataAdapter(sql, Con);
        Da.Fill(ds, "datatable");
        DataTable dt = new DataTable();
        dt = ds.Tables["datatable"];
        Con.Close();
        return (dt);
    }
    public DataTable executeStoteProc(string cExp)
    {//---dataTaken
        DataTable dt = new DataTable();
     
            OpenConnection();


            Da = new SqlDataAdapter();
            Cmd.CommandType = CommandType.Text;
            Cmd.CommandText = cExp;
            Cmd.CommandTimeout = 2000;

            Da.SelectCommand = Cmd;
            //ds = new DataSet();
            Da.Fill(dt);
            //dt = ds.Tables["table"];
            CloseConnection();
            return dt;
     
    }
    public void insStoreProc(string cExp, SqlParameter [] para)
    {
        OpenConnection();
        Cmd.CommandTimeout = 2000;

        Cmd = new SqlCommand(cExp,Con);
        Cmd.CommandType = CommandType.StoredProcedure;
        foreach (SqlParameter par in para)
        {
            Cmd.Parameters.Add(par);
        }

        Cmd.ExecuteNonQuery();
        CloseConnection();
        //SqlParameter[] par = new SqlParameter[2];
        //par[0] = appReg.addPara("@name", SqlDbType.VarChar, "mahesh");
        //par[1] = appReg.addPara("@address", SqlDbType.VarChar, "agra");

    }
    public SqlParameter addPara(string cAtVAl,SqlDbType sType ,string cVal)
    {
        SqlParameter par = new SqlParameter();
        par.ParameterName = cAtVAl;
        par.SqlDbType = sType;
        par.SqlValue = cVal;

        return par;
    }


}

Tuesday, 24 July 2012

cursor in sql server

http://sqlusa.com/bestpractices2005/doublecursor/
http://zlika.org/index.php/2010/03/06/sql-cursor-example/


 SQL Server cursor example - row-by-row operation - DECLARE CURSOR
DECLARE @dbName sysname
DECLARE AllDBCursor CURSOR  STATIC LOCAL FOR
  SELECT   name FROM     MASTER.dbo.sysdatabases
  WHERE    name NOT IN ('master','tempdb','model','msdb') ORDER BY name
OPEN AllDBCursor; FETCH  AllDBCursor INTO @dbName;
WHILE (@@FETCH_STATUS = 0) -- loop through all db-s 
  BEGIN
/***** PROCESSING (like BACKUP) db by db goes here - record-by-record process  *****/ 
    PRINT @dbName
    FETCH  AllDBCursor   INTO @dbName
  END -- while 
CLOSE AllDBCursor; DEALLOCATE AllDBCursor;
/* Messages
AdventureWorks
AdventureWorks2008
AdventureWorksDW
AdventureWorksDW2008
..... */
------------
-- T-SQL Cursor declaration and usage example - cursor loop syntax - using t-sql cursor
------------
USE AdventureWorks2008;
DECLARE curSubcategory CURSOR STATIC LOCAL               -- sql declare cursor
FOR SELECT ProductSubcategoryID, Subcategory=Name
FROM Production.ProductSubcategory ORDER BY Subcategory
DECLARE @Subcategory varchar(40), @PSID int
OPEN curSubcategory
FETCH NEXT FROM curSubcategory INTO @PSID, @Subcategory  -- sql fetch cursor
WHILE (@@fetch_status = 0)                    -- sql cursor fetch_status
BEGIN -- begin cursor loop
/***** USER DEFINED CODE HERE - POSSIBLY NESTED CURSOR *****/
            DECLARE @Msg varchar(128)
            SELECT @Msg = 'ProductSubcategory info: ' + @Subcategory + ' '+
                   CONVERT(varchar,@PSID)
            PRINT @Msg
FETCH NEXT FROM curSubcategory INTO @PSID, @Subcategory   -- sql fetch cursor
END -- end cursor loop
CLOSE curSubcategory
DEALLOCATE curSubcategory
GO
/* Partial output in Messages

ProductSubcategory info: Bib-Shorts 18
ProductSubcategory info: Bike Racks 26
ProductSubcategory info: Bike Stands 27
ProductSubcategory info: Bottles and Cages 28
ProductSubcategory info: Bottom Brackets 5
ProductSubcategory info: Brakes 6
*/
------------

------------
-- T SQL Search All Text & XML Columns in All Tables
------------
-- SQL nested cursors - sql server nested cursor - transact sql nested cursor
USE AdventureWorks;
GO
-- SQL Server create stored procedure with nested cursors
CREATE PROC sprocSearchKeywordInAllTables  @Keyword NVARCHAR(64)
AS
  BEGIN
    SET NOCOUNT  ON
    DECLARE  @OutputLength VARCHAR(4),
             @NolockOption CHAR(8)
         SET @OutputLength = '256'
         SET @NolockOption = ''
         -- SET @NolockOption =  '(NOLOCK)'
    DECLARE  @DynamicSQL   NVARCHAR(MAX),
             @SchemaTableName   NVARCHAR(256),
             @SchemaTableColumn NVARCHAR(128),
             @SearchWildcard    NVARCHAR(128)
         SET @SearchWildcard = QUOTENAME('%' + @Keyword + '%',CHAR(39)+CHAR(39))
         PRINT @SearchWildcard
    DECLARE  @SearchResults  TABLE(
                                   SchemaTableColumn NVARCHAR(384),
                                   TextWithKeyword   NVARCHAR(MAX)
                                   )
 
    DECLARE curAllTables CURSOR  STATIC LOCAL FOR
    SELECT   QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) AS ST
    FROM     INFORMATION_SCHEMA.TABLES
    WHERE    TABLE_TYPE = 'BASE TABLE'
             AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.'
             + QUOTENAME(TABLE_NAME)), 'IsMSShipped') != 1
    ORDER BY ST
    OPEN curAllTables
    FETCH NEXT FROM curAllTables
    INTO @SchemaTableName
    
    WHILE (@@FETCH_STATUS = 0) -- Outer cursor loop
      BEGIN
        PRINT @SchemaTableName
        SET @SchemaTableColumn = ''
        DECLARE curAllColumns CURSOR  FOR -- Nested cursor
        SELECT   QUOTENAME(COLUMN_NAME)
        FROM     INFORMATION_SCHEMA.COLUMNS
        WHERE    TABLE_NAME = PARSENAME(@SchemaTableName,1)
                 AND TABLE_SCHEMA = PARSENAME(@SchemaTableName,2)
                 AND DATA_TYPE IN ('varchar','nvarchar','char','nchar','xml')
        ORDER BY ORDINAL_POSITION
        OPEN curAllColumns
        FETCH NEXT FROM curAllColumns
        INTO @SchemaTableColumn
        WHILE (@@FETCH_STATUS = 0) -- Inner cursor loop (nested cursor while)
          BEGIN
            PRINT '  ' + @SchemaTableColumn
            SET @DynamicSQL = 'SELECT ''' + @SchemaTableName + '.' +
              @SchemaTableColumn + ''', LEFT(CONVERT(nvarchar(max),' +
              @SchemaTableColumn + '),' + @OutputLength + ')  FROM ' +
              @SchemaTableName + ' '+@NolockOption+
              ' WHERE CONVERT(nvarchar(max),' + @SchemaTableColumn +
              ') LIKE ' + @SearchWildcard
            INSERT INTO @SearchResults
            EXEC sp_executeSQL  @DynamicSQL
            FETCH NEXT FROM curAllColumns
            INTO @SchemaTableColumn
          END  -- Inner cursor loop
        CLOSE curAllColumns
        DEALLOCATE curAllColumns
        FETCH NEXT FROM curAllTables
        INTO @SchemaTableName
      END  -- Outer cursor loop
    CLOSE curAllTables
    DEALLOCATE curAllTables
   
    SELECT DISTINCT SchemaTableColumn, TextWithKeyWord FROM   @SearchResults
  END
GO

EXEC sprocSearchKeywordInAllTables  'Hamilton'
EXEC sprocSearchKeywordInAllTables  'Sánchez'
EXEC sprocSearchKeywordInAllTables  'O''Donnell'
EXEC sprocSearchKeywordInAllTables  'Certification'



--------------------------
------------
-- SQL Server Nested Cursors example - transact sql nested cursor
------------
-- SQL nested cursors - transact sql fetch_status - transact sql while loop
-- SQL nesting cursors - transact sql fetch next
-- T-SQL script for execution timing setup
USE AdventureWorks;
DBCC DROPCLEANBUFFERS
DECLARE @StartTime datetime
SET @StartTime = getdate()

-- Setup local variables
DECLARE     @IterationID INT,
            @OrderDetail VARCHAR(max),
            @ProductName VARCHAR(10)

-- Setup table variable
DECLARE @Result TABLE (PurchaseOrderID INT, OrderDetail VARCHAR(max))


-- OUTER CURSOR declaration - transact sql declare cursor
DECLARE curOrdersForReport CURSOR STATIC LOCAL FOR
SELECT PurchaseOrderID
FROM Purchasing.PurchaseOrderHeader
WHERE Year(OrderDate) = 2004
  AND Month(OrderDate) = 2
ORDER BY PurchaseOrderID

OPEN curOrdersForReport
FETCH NEXT FROM curOrdersForReport INTO @IterationID
PRINT 'OUTER LOOP START'

WHILE (@@FETCH_STATUS = 0) -- sql cursor fetch_status
BEGIN
      SET @OrderDetail = ''

-- INNER CURSOR declaration - transact sql declare cursor
-- SQL Nested Cursor - sql cursor nested - cursor nesting

      DECLARE curDetailList CURSOR STATIC LOCAL FOR
      SELECT p.productNumber
      FROM Purchasing.PurchaseOrderDetail pd
      INNER JOIN Production.Product p
      ON pd.ProductID = p.ProductID
      WHERE pd.PurchaseOrderID = @IterationID
      ORDER BY PurchaseOrderDetailID

      OPEN curDetailList
      FETCH NEXT FROM curDetailList INTO @ProductName
      PRINT 'INNER LOOP START'

      WHILE (@@FETCH_STATUS = 0)
      BEGIN
            SET @OrderDetail = @OrderDetail + @ProductName + ', '
            FETCH NEXT FROM curDetailList INTO @ProductName
            PRINT 'INNER LOOP'
      END -- inner while

      CLOSE curDetailList
      DEALLOCATE curDetailList

      -- Truncate trailing comma
      SET @OrderDetail = left(@OrderDetail, len(@OrderDetail)-1)
      INSERT INTO @Result VALUES (@IterationID, @OrderDetail)

      FETCH NEXT FROM curOrdersForReport INTO @IterationID
      PRINT 'OUTER LOOP'
END -- outer while
CLOSE curOrdersForReport
DEALLOCATE curOrdersForReport

-- Publish results
SELECT * FROM @Result ORDER BY PurchaseOrderID

-- Timing result
SELECT ExecutionMsec = datediff(millisecond, @StartTime, getdate())
GO
-- 220 msecs

------------
-- Equivalent set-based operations solution
------------

-- Execution timing setup
DBCC DROPCLEANBUFFERS
DECLARE @StartTime datetime
SET @StartTime = getdate()

-- SQL comma-limited list generation
-- SQL nested select statement
-- SQL FOR XML PATH
SELECT
      poh.PurchaseOrderID,
      OrderDetail = Stuff((
-- SQL correlated subquery
      SELECT ', ' + ProductNumber as [text()]
      FROM Purchasing.PurchaseOrderDetail pod
      INNER JOIN Production.Product p
      ON pod.ProductID = p.ProductID
      WHERE pod.PurchaseOrderID = poh.PurchaseOrderID
      ORDER BY PurchaseOrderDetailID
      FOR XML PATH ('')), 1, 1, '')
FROM Purchasing.PurchaseOrderHeader poh
WHERE Year(OrderDate) = 2004
  AND Month(OrderDate) = 2
ORDER BY PurchaseOrderID ;

-- Timing result
SELECT ExecutionMsec = datediff(millisecond, @StartTime, getdate())
GO
-- 110 msecs


/* Partial results

PurchaseOrderID   OrderDetail
1696              GT-0820, GT-1209
1697              HN-6320, HN-7161, HN-7162, HN-8320, HN-9161, HN-9168
1698              NI-4127
1699              RM-T801
1700              LI-1201, LI-1400, LI-3800
1701              TI-R982, TI-T723
*/

The following example uses @@FETCH_STATUS to control the WHILE loop in a typical cursor application:
-- T-SQL cursor declaration
DECLARE curManager CURSOR  FOR
SELECT EmployeeID,
       Title
FROM   AdventureWorks.HumanResources.Employee
WHERE  Title LIKE '%manager%'
        OR Title LIKE '%super%';

OPEN curManager;
FETCH NEXT FROM curManager;
WHILE @@FETCH_STATUS = 0
  BEGIN
    PRINT 'Cursor loop'
    FETCH NEXT FROM curManager;
  END; -- while
CLOSE curManager;
DEALLOCATE curManager;
GO
/* Partial results

EmployeeID        Title
3                 Engineering Manager

EmployeeID        Title
6                 Marketing Manager
*/

However, the @@FETCH_STATUS is global to all cursors on a connection, therefore using @@FETCH_STATUS to control nested cursors may not be advisable. To play it safe for the case of following triple nested cursors demonstration, we avoid using @@FETCH_STATUS. Instead we order the SELECTs for the cursor and find the max value on one unique column. We use a comparison between the running values and maximum value to control the loop. The OUTER cursor loop is based on OrderDate. The MIDDLE cursor loop is based PurchaseOrderID-s received on a particular date. The INNER cursor loop is based on the products belonging to a particular PurchaseOrderID.
This is the entire triple nested cursors T-SQL script:
-- MSSQL nested cursors
USE AdventureWorks
GO
DECLARE  @DateIteration DATETIME,
         @IterationID   INT,
         @OrderDetail   VARCHAR(1024),
         @ProductNo     VARCHAR(10)
DECLARE  @MaxOrderDate DATETIME,
         @MaxPOID      INT,
         @MaxProdNo    VARCHAR(10)
DECLARE  @Result  TABLE(
                        OrderDate       DATETIME,
                        PurchaseOrderID INT,
                        OrderDetail     VARCHAR(1024)
                        )
DECLARE curOrderDate CURSOR  FOR
SELECT   DISTINCT OrderDate
FROM     Purchasing.PurchaseOrderHeader
WHERE    year(OrderDate) = 2002
         AND month(OrderDate) = 7
ORDER BY OrderDate

SELECT @MaxOrderDate = OrderDate
FROM   Purchasing.PurchaseOrderHeader
WHERE  year(OrderDate) = 2002
       AND month(OrderDate) = 7
OPEN curOrderDate
FETCH NEXT FROM curOrderDate
INTO @DateIteration
PRINT 'OUTER LOOP'
WHILE (1 < 2)
  BEGIN
    DECLARE curOrdersForReport CURSOR  FOR
    SELECT   PurchaseOrderID
    FROM     Purchasing.PurchaseOrderHeader
    WHERE    OrderDate = @DateIteration
    ORDER BY PurchaseOrderID
    
    SELECT @MaxPOID = PurchaseOrderID
    FROM   Purchasing.PurchaseOrderHeader
    WHERE  OrderDate = @DateIteration
    
    OPEN curOrdersForReport
    FETCH NEXT FROM curOrdersForReport
    INTO @IterationID
    PRINT 'MIDDLE LOOP'
    WHILE (1 < 2)
      BEGIN
        SET @OrderDetail = ''
        DECLARE curDetailList CURSOR  FOR
        SELECT   p.ProductNumber
        FROM     Purchasing.PurchaseOrderDetail pd
                 INNER JOIN Production.Product p
                   ON pd.ProductID = p.ProductID
        WHERE    pd.PurchaseOrderID = @IterationID
        ORDER BY p.ProductNumber
         
        SELECT @MaxProdNo = p.ProductNumber
        FROM   Purchasing.PurchaseOrderDetail pd
               INNER JOIN Production.Product p
                 ON pd.ProductID = p.ProductID
        WHERE  pd.PurchaseOrderID = @IterationID
        OPEN curDetailList
        FETCH NEXT FROM curDetailList
        INTO @ProductNo
        PRINT 'INNER LOOP'
        WHILE (1 < 2)
          BEGIN
            SET @OrderDetail = @OrderDetail + @ProductNo + ', '
            IF (@ProductNo = @MaxProdNo)
              BREAK
            FETCH NEXT FROM curDetailList
            INTO @ProductNo
            PRINT 'INNER LOOP'
          END
         CLOSE curDetailList
         DEALLOCATE curDetailList
        
        INSERT INTO @Result
        VALUES     (@DateIteration,@IterationID,@OrderDetail)
        IF (@IterationID = @MaxPOID)
          BREAK
        FETCH NEXT FROM curOrdersForReport
        INTO @IterationID
        PRINT 'MIDDLE LOOP'
      END
    CLOSE curOrdersForReport
    DEALLOCATE curOrdersForReport
    IF (@DateIteration = @MaxOrderDate)
      BREAK
    FETCH NEXT FROM curOrderDate
    INTO @DateIteration
    PRINT 'OUTER LOOP'
  END
CLOSE curOrderDate
DEALLOCATE curOrderDate

SELECT * FROM   @Result
GO
/* Messages (partial)

OUTER LOOP
MIDDLE LOOP
INNER LOOP
INNER LOOP
INNER LOOP
...
*/