Sunday, 27 August 2017

How to write a Stored Procedure for Optional Search?

How to write a Stored Procedure for Optional Search?

--Create Table

create table stu_details(id int primary key identity,namenvarchar(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

No comments:

Post a Comment

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...