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

このストアド プロシージャに関連付けられた関数が 1 つあります。

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

私はcaridmy として渡していますTbarcodeが、出力の実行中に put が間違っています

my output:

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

TbarcodeWith 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

したがって、関数はTBarcodefromの最初の値を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 に答える