1

我有这样的存储过程:

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

我也有一个这样的功能:

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

如果状态 = 4,那么我得到的 keylocation 为空。如果状态为 1,那么答案是正确的,我的代码有任何问题。

请帮我找出解决方案

4

1 回答 1

2

BEGIN用和END关键字包围条件内的代码,如下所示:

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
于 2013-07-24T11:34:08.967 回答