Friday, October 26, 2012

How to get the table size in SQL server 2008


To get the table size in SQL Server 2008, we need to use a system stored procedure viz. sp_spaceused
If we pass Table Name as an argument, it gives the disk space used by the table and some other information like: Number of rows existing in the table, Total amount of reserved space for Table, Total amount of space reserved for table but not yet used, Total amount of space used by indexes in Table.








                                                                       


Example: 
For the ADDRESS table in our database, if we run

  sp_spaceused      'TADDRS'
it will give following result:

name
rows
reserved
data
index_size
unused
TADDRS
4726
392 KB
320 KB
16 KB
56 KB


               
                                               

Friday, October 19, 2012

How to Get the Selected Cells and Rows in the Windows Forms DataGridView Control


Here basically I am showing how to get selected cells/rows in DataGridView Control and use of IEnumerator while accessing them.



                DataGridViewSelectedRowCollection m = dataGrid_ZipList.SelectedRows;
                DataGridViewRow row;
 
                IEnumerator Enumerator = m.GetEnumerator();
                Enumerator.Reset();
                while (Enumerator.MoveNext())
                {
                    row = (DataGridViewRow)Enumerator.Current;
                    lstPostalCodes.Add(row.Cells["POSTAL_CODE"].Value.ToString());                   
                }

Tuesday, October 16, 2012

SQL Query to find out the frequency of each element in a column

Suppose we have a table "Person" as:

IdLastNameFirstNameAgeCity
1SmithJohn33Newyork
2SmithMike34Boston
3PaulRyan39Chicago
4WilliamBrian45Baltimore
5RaoSunita41Dayton
6ZingSue33Owings Mills
7SmithRobert45Newyork
8PaulZean33Chicago
9SrivastavaHemant33Baltimore
10RaoVenkat45Dayton


and we need to find out the frequency of the each age.


   SELECT Age, COUNT(Age)AS Frequency    
   FROM Persons   
   GROUP BY Age   
   ORDER BY    
   COUNT(Age) DESC
 

We get following result:
Age
Frequency
33
4
45
3
34
1
39
1


Now suppose we want to get the ‘Age’ which occurs more than equal to 2. For this query, we need to add HAVING clause after GROUP BY clause.



SELECT Age, COUNT(Age)AS Frequency
FROM Persons
GROUP BY Age 
HAVING  COUNT(Age) >= 2

That gives result as:
Age
Frequency
33
4
45
3