0

我有一个这样的存储过程:

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 
@car nvarchar(100), @keylocation  Varchar(50)
 if @status=0
begin
select @car =  t1.TBarcode from Transaction_tbl t1 
  select @keylocation='With Employee'+'('+@car+')'
return @keylocation
end

我是carid我的Tbarcode,但是在执行输出时出错了

my output:

    TBarcode             Status      Key location
    -------------------- ----------- ----------------------
    53012364813          0           With Employee(717164016319).

我想Tbarcode在 With Employee中得到同样的结果

Expected output

    TBarcode             Status      Key location
    -------------------- ----------- ----------------------
    53012364813          0           With Employee(53012364813).

我的代码有什么问题?

4

1 回答 1

0

你回来了

select @car =  t1.TBarcode from Transaction_tbl t1  

这来自你的功能。因为它没有适当的where条款@car is assigned by the first value of TBarcode

TBarcode因此,该函数返回from的第一个值Transaction_tbl作为输出

尝试这个

ALTER function [dbo].[keyloc](@status numeric(18,2),@cardID VARCHAR(50)/*Newly added*/)
 RETURNS varchar(50)
ASbegin  declare 
  @car nvarchar(100), @keylocation  Varchar(50)
   if @status=0
  begin
    select @car =  t1.TBarcode 
    from Transaction_tbl t1 
    WHERE t1.TBarcode =@cardID 
    select @keylocation='With Employee'+'('+@car+')'

return @keylocation
end

程序

ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode, t.Status,
 [dbo].[keyloc](t.Status,@carid/*newly added*/) as 'Key location'
from Transaction_tbl t 
 where t.TBarcode=@carid
end
于 2013-07-24T12:44:22.320 回答