Posts

Collections and Generics: .net 2.0

Name Space: System.Collections.Generic When we look at the term "generic", unrelated to the programming world, it simply means something that is not tied to any sort of brand name. For example, if we purchase some generic dish soap, soap that has no brand name on it, we know that we are buying dish soap and expect it to help us clean our dishes, but we really don't know what exact brand (if any) will be inside the bottle itself. We can treat it as dish soap even though we don't really have any idea of its exact contents. Think of Generics in this manner. We can refer to a class, where we don't force it to be related to any specific Type, but we can still perform work with it in a Type-Safe manner. A perfect example of where we would need Generics is in dealing with collections of items (integers, strings, Orders etc.). We can create a generic collection than can handle any Type in a generic and Type-Safe manner. For example, we can have a single array class tha...

MARS - Multiple Active Result Sets

Multiple Active Result Sets - MARS In a nutshell, it is the ability to have more than one pending request under a given SQL Server connection. For most cases this will directly translate to the ability to have more than one default result set (firehose cursor) outstanding while other operations can execute within the same session. It is probably as important to delimit what MARS is not: Parallel execution: Though MARS enables more than one request to be submitted under the same connection, this does not imply that they will be executed in parallel inside the server. MARS will multiplex execution threads between outstanding requests in the connection, interleaving at well defined points. Cursor replacement: As described earlier, there are some scenarios where cursors represented a suitable workaround for a lack of MARS; it may be valid to migrate those scenarios to use MARS. However, this does not imply that all current usages of cursors should be moved to MARS. By default, all o...

Find Nth max/min Salary in SQL server

SELECT * FROM temp1 t1 WHERE n - 1 = ( SELECT COUNT(DISTICNT ( sal )) FROM temp1 t2 WHERE t2.sal < t1.sal ) SELECT TOP 1 * FROM ( SELECT DISTINT TOP n sal FROM temp1 ORDER BY sal DESC) AS t ORDER BY t.sal ASC In sql server 2005 WITH MyCTE AS(  SELECT t_id,   sal,   DENSE_RANK() OVER(ORDER BY sal DESC) as rank1  FROM temp1  ) SELECT TOP 1 t_id, sal, rank1 FROM MyCTE WHERE rank1 = n Any updates/comments are appreciated.

Triggers

Definitions: It is special kind of Stored Procedure which is executed whenever specific event is occurred in/on a table or database 2 types in Sql server 2000: 1. DML Trigger Instead of triggers After triggers 3 types in Sql server 2005: •  DML Triggers Instead of triggers After triggers •  DDL Triggers •  CLR Triggers 2 types in Oracle: Row Level Triggers Statement Triggers

Data Integrity

Data Integrity falls into 1. Entity Integrity 2. Domain Integrity 3. Referential integrity 4. User defined integrity Entity Integrity: Defines row as unique instance of an entity for a particular table Domain Integrity: Is the validity of entries for a given column enforced by i. restricting Type ii. format iii. Range of possible E.g.: CHECK constraints, UNIQUE constraints, and DEFAULT constraints Referential Integrity: Preserves defined relationships between tables when altered or deleted. E.g.: Check, Foreign key User defined Integrity: Business rules can be enforced using triggers and stored procedures. Reference: http://odetocode.com/Articles/79.aspx

Sortings

Selection Sort : 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) Eg: 64 25 12 22 11 11 25 12 22 64 11 12 25 22 64 11 12 22 25 64 11 12 22 25 64 Bubble sort Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. First Pass: ( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them. ( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass: ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) ( 1 ...

Delete vs Truncate in SQL SERVER

SQL Server commands Delete vs Truncate Delete Command: 1.We can delete all or specific rows using WHERE clause in a table 2. Logs are maintained for deleted records 3. Slower than Truncate because of logging 4. Identity column value wouldn’t be reset after deleting all the records 5. We can rollback the deleted records, since logs are maintained in the temp db. Eg: DELETE "table name" Truncate Command: 1.We can not use WHERE clause in a table 2.Logs are not maintained for deleted records 3.Faster than Delete because of logging is not done 4.Identity column value will be reset after truncating records 5. We can't roll back records which are truncated Eg: TRUNCATE TABLE "table name"