Sql Tips

sql tips

1.  Assume we have a table, and every time we have to query different records and check the entries of the table.

In this case, We can make use of newid() function in sql server.

 select top (5) * from [tablename] order by newid()

Use: Mainly for audit purpose we may have to show only sample records from the table.

2. In Asp.Net we show the datas in form of the grid.

In the grid how we will show the Serial number which is autogenerated.

We will write code in Itemdatabound() event , We will have a lable inside that event and we use ItemIndex+1 to display Serial number starting from 1.

protected void gridsample_ItemDataBound(object sender, GridItemEventArgs e)
{

if (e.Item is GridDataItem)
{
Label lbl_sno = e.Item.FindControl("numberLabel") as Label;
lbl_sno.Text = (e.Item.ItemIndex + 1).ToString();
}
} 

Instead of writing the above code we can use some other way through sql.

select row_number() over(order by [column_name]) as serialNo,* from [table]

Using row_number() over() function we can generate serial number for grid and bind like other values in grid.

Posted in : Tutorials


Comments are closed.