Skip to main content

Posts

Showing posts from January, 2013

Singleton Design Pattern

"A class of which only a single instance can exist" Singletons can be lazy loaded. Only when it is actually needed. That's very handy if the initialization includes expensive resource loading or database connections. Singletons offer an actual object. Singletons can be extended into a factory. The object management behind the scenes is abstract so it's better maintainable and results in better code. You can use it to create a connection pool. It’s not wise to create a new connection every time a program needs to write something to a database; instead, a connection or a set of connections that are already a pool can be instantiated using the Singleton pattern. For example , public class Singleton {    private static Singleton instance;       private Singleton(){}    public static Singleton getInstance() {      if (instance == null )        instance = new Singleton ();      return instance;    }   }

Difference Temporary Table and Table Variable –SQL SERVER?

Temporary Table Table Variable create table #T (…) declare @T table (…) Temporary Tables are real tables so you can do things like CREATE INDEX, Table variable is not real table but you can have indexes by using PRIMARY KEY or UNIQUE constraints. CREATE TABLE statement. SELECT INTO statement. DECLARE statement   Only Maximum 116 characters. Maximum 128 characters Temp tables might result in stored procedures being recompiled, Table variables will not. #temp_tables are created explicitly when the TSQL CREATE TABLE statement is encountered and can be dropped explicitly with DROP TABLE or will be dropped implicitly when the batch ends. @table_variables are created implicitly when a batch containing a DECLARE @.. TABLE statement is executed (before any user code in that batch runs) and are dropped implicitly at the

How to use rank function in SQL Server ?

The ROW_NUMBER () function in SQL Server  returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition. The RANK() function in SQL Server returns the position of a value within the partition of a result set, with gaps in the ranking where there are ties. The DENSE_RANK() function in SQL Server returns the position of a value within the partition of a result set, leaving no gaps in the ranking where there are ties. The NTILE() function in SQL Server return distributes the rows in an ordered partition into a specified number of groups. Example CREATE TABLE Students (       Stud_ID     INT IDENTITY ( 1 , 1 ),       Stud_Name   VARCHAR ( 100 ),       Stud_Mark   INT ) INSERT INTO Students ( Stud_Name , Stud_Mark ) VALUES ( 'a' , 60 ), ( 'b' , 94 ), ( 'c' , 70 ), ( 'd' , 63 ), ( 'e' , 60 ), ( 'f' , 60 ), ( 'g&#

What is the difference between static, internal and public constructors?

Static Constructor Static constructor runs only ones - before the first use of the class and it can access only the static members of the class Used to initialize the static members of a class. Can’t access non-static members. Executes before the first instance of a class. We can’t determine the time of execution. Executes by the CLR not by the object of a class. There are no parameterized static constructors since it is handled by the CLR not by the object. Time of execution might be at the loading of contained assembly. Public Constructor Public constructor runs every time when you create an object of the class using new  Public one can be called from other assemblies as well. Used to restrict a class to be instantiated and to be inherited. Used whenever a class contains only static members. Internal Constructor The internal constructor is only accessible to types in the same assembly Internal is just another access modifier for the constructor above. It c