user2603688
user2603688

Reputation: 171

Second IF condition not working properly

i have stored procedure like this:

ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode, t.Status, [dbo].[keyloc](t.Status) as 'Key location'
from Transaction_tbl t 
 where t.TBarcode=@carid
end

also i have one function like this:

ALTER function [dbo].[keyloc](@status numeric(18,2)) RETURNS varchar(50)
as
begin

declare 
@Ename nvarchar(50),
@keylocation  Varchar(50) 

if @status= 1 
select @Ename= e1.Ename from Transaction_tbl t
 join EmployeeMaster_tbl e1
ON t.Ecode=e1.Ecode
select @keylocation='With PAIC'+'('+@Ename+')'
return @keylocation
if @status= 4 
select @Ename= e2.Ename from Transaction_tbl t
 join EmployeeMaster_tbl e2
ON t.DelEcode=e2.Ecode
select @keylocation='With Driver'+'('+@Ename+')'
return @keylocation
end

If the status = 4,Then i am getting keylocation null. if the status is 1 then answer is getting proper,any wrong with my code.

Please help me find out solution

Upvotes: 1

Views: 89

Answers (1)

jazzytomato
jazzytomato

Reputation: 7214

Surround the code inside the condition with BEGIN and END keywords, like this :

if @status= 1 
BEGIN
  select @Ename= e1.Ename from Transaction_tbl t
   join EmployeeMaster_tbl e1
  ON t.Ecode=e1.Ecode
  select @keylocation='With PAIC'+'('+@Ename+')'
END

if @status= 4 
BEGIN
  select @Ename= e2.Ename from Transaction_tbl t
   join EmployeeMaster_tbl e2
  ON t.DelEcode=e2.Ecode
  select @keylocation='With Driver'+'('+@Ename+')'
END

return @keylocation

Upvotes: 2

Related Questions