Here is an one very intresting thing about SQL.
Even we don't specify the column name in where condition of SQL query, till we can filter the record from the table.
How is that possible ?
Let's go through this scenario.
Here is an Products table having two colum CategoryId and ProductId.
create table Products(CategoryId varchar(20),ProductId varchar(20))
insert into Products values ('C01',1)
insert into Products values ('C02',2)
insert into Products values ('C03',3)
insert into Products values ('C04',4)
insert into Products values ('C05',5)
insert into Products values ('C01',6)
insert into Products values ('C02',7)
insert into Products values ('C03',8)
insert into Products values ('C04',9)
insert into Products values ('C05',2)
insert into Products values ('C01',11)
insert into Products values ('C02',12)
insert into Products values ('C03',13)
insert into Products values ('C04',14)
insert into Products values ('C05',15)
Now I will not specify the CategoryId column in where condition of below query, still I would get the desired result.
declare @CategoryId as varchar(20)
set @CategoryId = 'C04'
select * from Products where @CategoryId=case when ProductId in('15','2','5')
then 'C05'
When ProductId in('14','9','4')
then 'C04'
end
Here is a result of above query.
isn't it intresting ?
I have recently come across such scenario. so I thought to share it with you.
Send me your feedback on this.
Thank you