Create Stored Procedure in Sqlserver 2008 with date paramters and using if else conditions ||Create Stored Procedure with parameters to select Employee details filtering with Employee joined date in Sqlserver 2008 || Create Stored procedure with parameters in where condition in sqlserver 2008


In this am showing about to select Employee details from employee table filtering with employee joined date.
For that first am Creating one Employee table with the following fields.


Then Inserting some data.After inserting, To select employee table details create one Stored Procedure with the name sp_EmpDetails.

In this procedure am taking two parameters à From date and To date.

Using the above two dates as parameters to select employee details filter with DateOfJoining.


Create procedure sp_EmpDetails
(
@From date,
@To date
)
as
begin

declare @date varchar(500);
declare @sql varchar(3000);

if(@From!='' and @To='')
   begin
      set @date= +' >= '''+ convert(varchar(10),@From,101)+''''
   end
else if(@From!='' and @To!='')
   begin
      set @date= +' between '''+ convert(varchar(10),@From,101) +''' and '''+       convert(varchar(10),@To,101)+''''
   end
else if(@From='' and @To!='')
   begin
      set @date=+' <='''+ convert(varchar(10),@To,101)+''''
   end
else
   begin
      set @date= +' is not null '
   end


set @sql='select Emp_Id, Emp_Name, DateOfJoining, Salary, Address from Employee where DateOfJoining +@date


exec(@sql)
end


For Executing procedure:

Exec  sp_EmpDetails '10/04/2012','20/04/2012'



0 comments:

Post a Comment