在sql中将一个存储过程转换为另一个存储过程

在sql中将一个存储过程转换为另一个存储过程,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有这样的存储过程: ALTER procedure [dbo].[fetchkey] @carid nvarchar(50) =null as begin select t.TBarcode,t.Status from Transaction_tbl t where t.TBarcode=@carid end ALTER function [dbo].[keyloc](@status numeric(18,0)) RETURNS varchar(50) as begin declare

我有这样的存储过程:

ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode,t.Status
from Transaction_tbl t 
 where t.TBarcode=@carid
end
ALTER function [dbo].[keyloc](@status numeric(18,0)) RETURNS varchar(50)
as
begin
declare 
@keylocation  Varchar(50) 
if @status=1
select @keylocation= e1.Ename from Transaction_tbl t left join EmployeeMaster_tbl e1
ON e1.ECode = t.ECode AND t.Status = 1 or e1.Ecode=t.DelEcode and t.Status=4
return @keylocation
end
ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode,[dbo].[keyloc](t.Status)
from Transaction_tbl t 
 where t.TBarcode=@carid
end
我的输出:

TBarcode             Status
51891044554          1
我想根据我的状态再显示一列。因此,我编写了一个如下函数:

ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode,t.Status
from Transaction_tbl t 
 where t.TBarcode=@carid
end
ALTER function [dbo].[keyloc](@status numeric(18,0)) RETURNS varchar(50)
as
begin
declare 
@keylocation  Varchar(50) 
if @status=1
select @keylocation= e1.Ename from Transaction_tbl t left join EmployeeMaster_tbl e1
ON e1.ECode = t.ECode AND t.Status = 1 or e1.Ecode=t.DelEcode and t.Status=4
return @keylocation
end
ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode,[dbo].[keyloc](t.Status)
from Transaction_tbl t 
 where t.TBarcode=@carid
end
然后,我尝试执行以下存储过程:

ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode,t.Status
from Transaction_tbl t 
 where t.TBarcode=@carid
end
ALTER function [dbo].[keyloc](@status numeric(18,0)) RETURNS varchar(50)
as
begin
declare 
@keylocation  Varchar(50) 
if @status=1
select @keylocation= e1.Ename from Transaction_tbl t left join EmployeeMaster_tbl e1
ON e1.ECode = t.ECode AND t.Status = 1 or e1.Ecode=t.DelEcode and t.Status=4
return @keylocation
end
ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode,[dbo].[keyloc](t.Status)
from Transaction_tbl t 
 where t.TBarcode=@carid
end
但我的输出出错了:

TBarcode            
51891044554          Null
我的代码有什么问题?我的预期输出是:

TBarcode             Status  key location
51891044554          1       Raheem.

我如何才能做到这一点?

在您更改的过程中,您忘记了(?)包含状态字段,这就是为什么它没有包含的原因。这:

select t.TBarcode,[dbo].[keyloc](t.Status)
应该是:

select t.TBarcode, t.Status, [dbo].[keyloc](t.Status) as 'Key location'
如果您还需要一列
状态
关键位置的标题

为什么你的函数返回null我真的不能说,数据正确吗?您是否尝试过单独执行它以查看它是否返回预期的输出?可能是函数中的WHERE子句的计算方式错误。尝试将谓词放在括号中,使计算更加明确,如:

ON (e1.ECode = t.ECode AND t.Status = 1) or (e1.Ecode=t.DelEcode and t.Status=4)
如果这是应该评估的方式


另外,
状态
是否真的是一个
数值(18,0)
值,如果不是,您可能希望将函数参数更改为更合适的值(如smallint或int)。

问题是您在函数中进行了左连接,请尝试以下操作:

select @keylocation= e1.Ename 
from Transaction_tbl t
join EmployeeMaster_tbl e1
ON e1.ECode = t.ECode AND t.Status = 1 
or e1.Ecode=t.DelEcode and t.Status=4

长官,我的数据是正确的。但我得到的密钥位置表是空的。我得到了输出。我将左连接更改为连接。现在我得到了输出