find record in sql database with parameter or without parameter using store procedure

/*
 exec usp_finddatawithout_para ''
 exec usp_finddatawithout_para '1'
*/ 
create proc usp_finddatawithout_para
(
 @empid varchar(50) = null

as
begin   
      declare @sql varchar(max),@eid varchar(500)
      if @empid <>''
               select @eid='and e.id=' + char(39) + @empid + char(39) 
      else
               select @eid=''
      select @sql='select name from emp e where 1=1 '+@eid+''
      execute(@sql) 
end

--or:--

if exists (select name from emp where id = @empid)
begin
    select name from emp where id = @empid
end
begin
    select name from emp
end

Post a Comment

0 Comments