How to write a Stored Procedure for
Optional Search?
--Create Table
create table stu_details(id int primary key identity,name nvarchar(30),class int)
--**************************************************************
--insert record
insert into stu_details(name,class) values
('rahul',14),
('Ramu',20),
('Rahul',13),
('rahul',14),
('rani' ,15),
('RANI' ,15),
('RANI' ,15),
('Raju' ,12),
('Raju' ,12),
('rashmi',18),
('Roza' ,17),
('rani' ,15),
('kush',23)
--***************************************************************
--Show Record
select * from
stu_details
--***************************************************************
--Creating stored Procedure for Optional Search
Create proc usp_search
@id int=0,
@name nvarchar(30)=null,
@class int=0
as
BEGIN
select * from
stu_details where
(id=@id or COalesce(@id,'')=0)
and (name=@name or COALESCE(@name,'')='')
and (class=@class or COALESCE(@class,'')=0)
END
--***************************************************************