Monday, May 29, 2017

IDENT_CURRENT (Transact-SQL) / IDENT_CURRENT( 'table_name' )

IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.

When the IDENT_CURRENT value is NULL (because the table has never contained rows or has been truncated), the IDENT_CURRENT function returns the seed value.

Let's Start With the Below Process:
---Create Table-----
 Create Table empTable 
 (
 empId numeric(4),
 empname varchar(100)

 )

--Insert the record in the table
Now Insert some values (values for 'empid' is not required as this is an identity column ).
insert into dbo.empTable values ('employee 1')
insert into dbo.empTable values ('employee 2')
insert into dbo.empTable values ('employee 3')
insert into dbo.empTable values ('employee 4')
insert into dbo.empTable values ('employee 5')
insert into dbo.empTable values ('employee 6')
just to confirm run the below query:
select * from dbo.empTable
output:
1 employee 1
2 employee 2
3 employee 3
4 employee 4
5 employee 5
6 employee 6


Now run the below query:
select IDENT_CURRENT( 'empTable' )
NOTE: The above function accepts table / view name.
Output:
6
Now insert one more row and then check this:
insert into dbo.empTable values ('employee 7')
select IDENT_CURRENT( 'empTable' )
Output:
7.
What does the function do ? This always give you the current 'empid' value from the table.
Now run the query below.
insert into dbo.empTable values ('employee 8')
select IDENT_CURRENT( 'empTable' )
select @@IDENTITY
what is the output :
8
That means @@IDENTITY does the same thing!!!!
Wait and watch:
Open a new Query window and run the below query again:
select IDENT_CURRENT( 'empTable' )
select @@IDENTITY
Output:
8
NULL
Oops Strange ! RIGHT???
The difference between them is
·  IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
·  @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
One more observation which is very important:
Run the below query in seperate query window:
select MAX(empid) from dbo.empTable
select IDENT_CURRENT( 'empTable' )
delete from dbo.empTable where empid = 8
select MAX(empid) from dbo.empTable
select IDENT_CURRENT( 'empTable' )
What is the output we get?
8
8
7
8
What we see is MAX(empid) always give the max of the row but the
IDENT_CURRENT( 'empTable' ) gives the last inserted value. this is very useeful for transactions table where we have some data retrieving logic between more than one table but having dependency on the identity column.
IDENT_CURRENT( 'tablename/viewname')  decoded.

Thursday, May 11, 2017

What is difference between Factory and Abstract factory design pattern?


Try to understand the two design patter with some simple examples factory design pattern:

You have some raw materiel (Input) , you provided it to one factory (factory class) and the factory gives you a product (object) so based on your raw material and factory implementation you get a product this is a simple illustration of factory design pattern


Now for the Abstract factory design: It’s like you have raw material, you provided it to one factory as input but instead of processing your input this factory delegates the task to create product to its sub factories. 

So simply, an abstract factory means multiple factories.

Implementing Service Locator (To Resolve Dependency)

using System; /// <summary> /// Summary description for Class1 /// </summary> public class serviceLocator {     public s...