Stored Procedures Are Precompiled So They Are Faster1
Stored Procedures Are Precompiled So They Are Faster1
Stored Procedures Are Precompiled So They Are Faster1
Ask any one why he prefers stored procedures as compared to inline queries and most will reply
back with a standard statement:
“Stored procedures are precompiled and cached so the performance is much better.”
Let me just explain the above sentence more diagrammatically. When we fire SQL for the first
time, three things happen:
The above statement states that when you run a stored procedure for the first time it will go
through all the above steps and the plan will be cached in-memory. So the next time when the
stored procedure is executed it just takes the plan from the cache and executes the same. This
increases performance as the first two steps are completely eliminated.
The above statement also says / implies that for inline queries all the above steps are repeated
again and again which brings down the performance considerably.
The above explanation was pretty valid and true for older versions of SQL Server, but from 2005
onwards, all SQL statements, irrespective of it’s a SQL coming from inline code or stored
procedure or from anywhere else, they are compiled and cached.
Using Coalesce() in sqlserver
Introduction
When we have multi-value attribute with single or more null values in a Table, the Coalesce()
function is very useful.
The above Employee table may have single value or three values. If it has single value, then it
fills null values with remaining attributes.
When we retrieve the number from employee table, that number Should Not be Null value. To
get not null value from employee table, we use Coalesce() function. It returns the first
encountered Not Null Value from employee table.
It returns:
id Name Contactnumber
101 Albert 999999
102 khan 123455
103 victor 112121
104 lovely 1897321
Thank you!
<script type="text/javascript">
$(document).ready(function(){
if(getParameterByName('TopMenu') == 1)
$(".menu-con2").hide();
$(".icon-con").show();
$(".menu-con").show();
$("a.f-ag").removeClass("selected");
$(".logo-col").removeClass("logo-ag");
$("a.com-ser").addClass("selected");
if(getParameterByName('TopMenu') == 2)
$(".icon-con").hide();
$(".menu-con").hide();
$(".logo-col").addClass("logo-ag");;
$(".menu-con2").show();
$("a.com-ser").removeClass("selected");
$("a.f-ag").addClass("selected");
});
function getParameterByName(name) {
results = regex.exec(location.search);
</script>
Default.aspx: sorting example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
<asp:TemplateField HeaderText="Designation">
<EditItemTemplate>
<asp:TextBox ID="txtDesg" runat="server" Text='<%#Eval("Designation") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDesg" runat="server" Text='<%#Eval("Designation") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrDesignation" runat="server"/>
<asp:RequiredFieldValidator ID="rfvdesignation" runat="server"
ControlToValidate="txtftrDesignation" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Configuration;
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Sort Gridview Cloumns with Up and Down Arrows</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:11pt;
font-weight:normal;
color:black;
width:350px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmpDetails" runat="server" CssClass="Gridview"
AllowPaging="true" AllowSorting="true" PageSize="4" HeaderStyle-BackColor="#7779AF"
HeaderStyle-ForeColor="White" AutoGenerateColumns="false" ShowFooter="true"
DataSourceID="dsEmpdetails" onrowdatabound="gvEmpDetails_RowDataBound">
<PagerSettings Mode= "NumericFirstLast" FirstPageText="First"
PreviousPageText="Previous" NextPageText="Next" LastPageText="Last" />
<PagerStyle BackColor="#7779AF" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location"
/>
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsEmpdetails" runat="server"
ConnectionString="<%$ConnectionStrings:ApplicationServices1 %>"
SelectCommand="select * from EmployeeSalary" >
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
Default3.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show gridview Rows Total in </title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmp" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold ="true" DataKeyNames="EmpID"
runat="server" ShowFooter="true" AllowPaging="true" PageSize="5"
AutoGenerateColumns="false" DataSourceID ="sqldsEmp" onrowdatabound="gvEmp_RowDataBound">
<FooterStyle Font-Bold="true" BackColor="#61A6F8" ForeColor="black" />
<Columns>
<asp:BoundField DataField="EmpID" HeaderText="Emp ID" />
<asp:BoundField DataField="EmpName" HeaderText="Emp Name" />
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltxttotal" runat="server" Text="Total Amount"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblamount" runat="server" Text='<%# Eval("Amount") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqldsEmp" runat="server" SelectCommand="select * from
EmployeeSalary" ConnectionString="<%$ ConnectionStrings:dbconnection %>">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
Default4.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Gridview onmouseover and onmouseout</title>
<script type="text/javascript">
var oldgridcolor;
function SetMouseOver(element) {
oldgridcolor = element.style.backgroundColor;
element.style.backgroundColor = '#ffeb95';
element.style.cursor = 'pointer';
element.style.textDecoration = 'underline';
}
function SetMouseOut(element) {
element.style.backgroundColor = oldgridcolor;
element.style.textDecoration = 'none';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvrecords" AutoGenerateColumns="false"
onrowdatabound="gvrecords_RowDataBound">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
<columns>
<asp:boundfield headertext="ACount" datafield="ACount" />
<asp:boundfield headertext="BCount" datafield="BCount" />
<asp:boundfield headertext="DCount" datafield="DCount" />
<asp:templatefield headertext="Status">
<itemtemplate>
<asp:label id="aCount" runat="server" />
<asp:label id="bCount" runat="server" />
<asp:label id="dCount" runat="server" />
</itemtemplate>
</asp:templatefield>
</columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Default5.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Delete Rows in Gridview with Checkbox</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
<script type="text/javascript">
function Confirmationbox() {
var result = confirm('Are you sure you want to delete selected User(s)?');
if (result) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserDetails" runat="server" DataSourceID="objUsers"
DataKeyNames="UserId" CssClass="Gridview" AutoGenerateColumns="false"
HeaderStyle-BackColor="#61A6F8" HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkdelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="objUsers" runat="server"
ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from UserInformation"
DeleteCommand="delete from UserInformation where UserId=@UserId" >
<DeleteParameters>
<asp:Parameter Name="UserId" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
</div>
<asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" />
</form>
</body>
</html>
Default6.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="sourceProducts"
runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT ProductID, ProductName, UnitPrice,
UnitsInStock FROM Products">
</asp:SqlDataSource>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
CellPadding="4"
DataKeyNames="ProductID"
DataSourceID="sourceProducts"
Font-Names="Verdana"
Font-Size="Small"
ForeColor="#333333"
GridLines="None"
AllowPaging="True"
OnRowCommand="GridView1_RowCommand">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center"
/>
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl='<%# GetStatusPicture(Container.DataItem)
%>'
CommandName="StatusClick"
CommandArgument='<%# Eval("ProductID") %>'
/></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductID"
HeaderText="ID"
InsertVisible="False"
ReadOnly="True"
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName"
HeaderText="Product"
SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice"
DataFormatString="{0:C}"
HeaderText="Price"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock"
HeaderText="UnitsInStock"
SortExpression="Units In Stock" />
</Columns>
</asp:GridView>
<br />
<asp:Label ID="lblInfo" runat="server" Font-Names="Verdana" Font-
Size="Small"></asp:Label>
</div>
</form>
</body>
</html>
Default7.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
USE [umbracodev_new]
GO
/****** Object: StoredProcedure [dbo].[UpdateNPEPaymentTransaction]
Script Date: 05/30/2014 15:05:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: IQR_RHR
-- Create date: 20101228
-- Description:
-- Update Date : 2014-02-12
-- Updated By : Hardik Pithadiya
-- Description : Added new field in Update Product Id
-- =============================================
ALTER PROCEDURE [dbo].[UpdateNPEPaymentTransaction]
-- Add the parameters for the stored procedure here
@UID uniqueidentifier OUTPUT,
@Status varchar(10) = null,
@MessageLog varchar(max) = null,
@CompletedOn datetime = null,
@XmlSerializedObjectResponse xml = null,
@ProductId VARCHAR(255) = NULL,
@ContactId INT = NULL,
@SplitTransactionID BIGINT = NULL,
@PaymentReferenceId VARCHAR(255) = NULL,
@TransactionTypeId INT = NULL,
@IsEmailNotificationSent BIT = NULL,
@CreditCardType VARCHAR(50) = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
UPDATE [dbo].[NPEPaymentTransaction]
SET
[Status] = @Status
,[MessageLog] = @MessageLog
,[CompletedOn] = @CompletedOn
,[XmlSerializedObjectResponse]=COALESCE(@XmlSerializedObjectResponse,XmlSeria
lizedObjectResponse)
,[ProductId] = COALESCE(@ProductId,ProductId)
,[ContactId] = COALESCE(@ContactId, [ContactId])
,[PaymentTransactionId] =
COALESCE(@PaymentReferenceId,[PaymentTransactionId])
,[TransactionTypeId] =
COALESCE(@TransactionTypeId,TransactionTypeId)
,[SplitTransactionId] =
COALESCE(@SplitTransactionId,SplitTransactionId)
,[IsEmailNotificationSent] =
COALESCE(@IsEmailNotificationSent,IsEmailNotificationSent)
,[CreditCardType] = COALESCE(@CreditCardType,CreditCardType )
WHERE [UID] = @UID
END
USE [umbracodev_new]
GO
/****** Object: StoredProcedure [dbo].[GetNPEPaymentTransaction] Script
Date: 05/30/2014 15:31:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: IQR_RHR
-- Create date: 20101228
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[GetNPEPaymentTransaction]
-- Add the parameters for the stored procedure here
@UID uniqueidentifier
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
END
USE [umbracodev_new]
GO
/****** Object: StoredProcedure [dbo].[SaveNPEPaymentTransaction] Script
Date: 05/30/2014 15:32:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: IQR_RHR
-- Create date: 20101228
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[SaveNPEPaymentTransaction]
-- Add the parameters for the stored procedure here
@UID uniqueidentifier OUTPUT,
@Description varchar(max) = null,
@XmlSerializedObject xml = null,
@Amount decimal(18,2) = null ,
@PayPalAuthId varchar(50) = null,
@Status varchar(10) = null,
@OnSuccessRedirect varchar(200) = null,
@MessageLog varchar(max) = null,
@IP varchar(50) = null,
@ProviderId int = null,
@AuthorizedOn datetime = null,
@CompletedOn datetime = null,
@CreatedBy varchar(200) = null,
@IsRecurring bit = null,
@RecurringStartDate datetime = null,
@RecurringFrequency int = null,
@TotalRecurringPayments int = null,
@OnCancelRedirect varchar(200)= null,
@FirstName nvarchar(max) = null,
@LastName nvarchar(max) = null,
@AddressLine1 nvarchar(max) = null,
@AddressLine2 nvarchar(max) = null,
@City nvarchar(max) = null,
@State nvarchar(max) = null,
@Country nvarchar(max) = null,
@Zip nvarchar(max) = null,
--inserted by Tushar Vaja for Get User Agent Information
@UserAgent varchar(200) = null,
@XmlSerializedObjectResponse xml = null,
@SplitTransactionId BIGINT = NULL,
@ContactID INT = NULL,
@PaymentTransactionId VARCHAR(255) = NULL,
@CustomDataSetDetails NVARCHAR(MAX) = NULL,
@CreditCardType VARCHAR(50) = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
END