我有这样的存储过程:
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
如果status = 4,那么我的keylocation为null。如果状态为1则答案正确,我的代码有任何错误。
请帮我找出解决方案
答案 0 :(得分: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