Cursor in SQL

We can say that cursor is row pointer in a set of rows (result set).Which points a single row at time in result set. But can move to other rows of the (result set) when required.

 To use cursor in SQL Store procedures. You need to follow the given steps.
  1.  Define a cursor
  2. Open the cursor to establish the result set
  3. Fetch the data into local parameter  as needed from the cursor.(one row at a time)
  4. Close the cursor when done.

Remove All User Defined Store procedures, Views and Triggers

interesting things  in SQL if you have a list of Store Procedures ,Views and triggers  in your database and reason  being you want to delete all Store procedures means you want to delete all the thing from data base. In that condition what you will do? It quiet simple you will delete one by one all the things. But it is so boring and time wasting. Let see how to write a simple Script to delete all Store procedures, views and triggers from database.

Remove All User Defined Store procedures.

Create  PROCEDURE [dbo].[usp_RemoveAllStoreProceduers]
AS
BEGIN
               
                declare @spName varchar(150);
  --Step 1 : Define Cursor (Create a result set of all procedures in database).
                declare cur cursor for select [name] from sys.objects where type='p'
                --Step 2 : Open Cursor
                open cur
  --Step 3 : Fatch data into local parameter
                fetch next from cur into @spName
                while @@fetch_Status =0
                begin
 -- Drop current store procedure in Cursor
                exec ('drop procedure '+ @spName)
                fetch next from cur into @spName
                end
                close cur
  --Close Cursor After done
                deallocate cur
  --Deallocate Cursor After done
END
Remove All User Defined View.

Create PROCEDURE [dbo].[usp_RemoveAllUserDefinedViews]
AS
BEGIN
               
                declare @viewName varchar(150);
  --Step 1 : Define Cursor (Create a result set of  all views in database).
                declare cur cursor for select [name] from sys.objects where type='v'
                --Step 2 : Open Cursor
                open cur
  --Step 3 : Fatch data into local parameter
                fetch next from cur into @viewName
                while @@fetch_Status =0
                begin
 -- Drop current view in Cursor
                exec ('drop view '+ @viewName)
                fetch next from cur into @ viewName
                end
                close cur
  --Close Cursor After done
                deallocate cur
  --Deallocate Cursor After done
E

.net training in jaipur

Example DataAdapter : use DataAdapter in asp.net

DataAdapter Example :  This is a example of  dataadapter.


<%@ Page Language="C#" %>  
<%@ Import Namespace="System.Data" %>  
<%@ Import Namespace="System.Data.SqlClient" %>  
<%@ Import Namespace="System.Configuration" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e) {  
        if (!Page.IsPostBack) {  
            SqlConnection MyConnection;  
            SqlCommand MyCommand;  
            SqlDataAdapter MyAdapter;  
            DataTable MyTable;  
  
            MyConnection = new SqlConnection();  
            MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings

["AppConnectionString1"].ConnectionString;  
  
            MyCommand = new SqlCommand();  
            MyCommand.CommandText = "SELECT TOP 8 * FROM PRODUCTS";  
            MyCommand.CommandType = CommandType.Text;  
            MyCommand.Connection = MyConnection;  
  
            MyTable = new DataTable();  
            MyAdapter = new SqlDataAdapter();  
            MyAdapter.SelectCommand = MyCommand;  
            MyAdapter.Fill(MyTable);  
  
            GridView1.DataSource = MyTable.DefaultView;  
            GridView1.DataBind();  
  
            MyAdapter.Dispose();  
            MyCommand.Dispose();  
            MyConnection.Dispose();  
              
        }  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>DataAdapter example: how to use DataAdapter in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
    </div>  
    </form>  
</body>  

</html>