连接字段时,我在 SQL 函数中得到 NULL 输出

发布于 2024-07-06 03:50:50 字数 1354 浏览 4 评论 0原文

我有以下函数:

CREATE FUNCTION fGetTransactionStatusLog
(
    @TransactionID  int
)
RETURNS varchar(8000) AS  
BEGIN 

declare StatusChanges cursor for
    select  NewStatusID, FirstName + ' ' + LastName AS UserName, Stamp, CAST(Notes AS varchar(8000)) AS Notes
      from TransactionStatusChanges tsc
        left join Users us ON tsc.UserID = us.UserID
      where TransactionID = @TransactionID ORDER BY StatusNum

declare @output varchar(8000)

declare @NewStatusID char(2)
declare @UserName varchar(255)
declare @Stamp datetime
declare @Notes varchar(8000)

set @output = ''

OPEN StatusChanges
  FETCH NEXT FROM StatusChanges INTO @NewStatusID, @UserName, @Stamp, @Notes
  WHILE @@FETCH_STATUS = 0
  BEGIN

    set @output = @output + RTRIM(CAST(@Stamp AS varchar(30))) + ': ' + @NewStatusID + ' by ' + @UserName +  CHAR(13) + CHAR(10)

    IF @Notes IS NOT NULL
    BEGIN
        set @output = @output + '---' + @Notes +  CHAR(13) + CHAR(10)
    END

    FETCH NEXT FROM StatusChanges INTO @NewStatusID, @UserName, @Stamp, @Notes
  END
CLOSE StatusChanges
DEALLOCATE StatusChanges

RETURN @output

END

现在,该函数返回的正是我想要的任何记录中没有任何注释的交易... 对于 TransactionStatusChanges 中至少有一条记录具有非 NULL Notes 字段的事务,我得到 NULL。

我不太明白,因为我在连接之前检查 @Notes 是否不为 NULL。

有任何想法吗?

注意:我使用 varchar(8000) 因为我无法在函数内使用文本。

I have the following function:

CREATE FUNCTION fGetTransactionStatusLog
(
    @TransactionID  int
)
RETURNS varchar(8000) AS  
BEGIN 

declare StatusChanges cursor for
    select  NewStatusID, FirstName + ' ' + LastName AS UserName, Stamp, CAST(Notes AS varchar(8000)) AS Notes
      from TransactionStatusChanges tsc
        left join Users us ON tsc.UserID = us.UserID
      where TransactionID = @TransactionID ORDER BY StatusNum

declare @output varchar(8000)

declare @NewStatusID char(2)
declare @UserName varchar(255)
declare @Stamp datetime
declare @Notes varchar(8000)

set @output = ''

OPEN StatusChanges
  FETCH NEXT FROM StatusChanges INTO @NewStatusID, @UserName, @Stamp, @Notes
  WHILE @@FETCH_STATUS = 0
  BEGIN

    set @output = @output + RTRIM(CAST(@Stamp AS varchar(30))) + ': ' + @NewStatusID + ' by ' + @UserName +  CHAR(13) + CHAR(10)

    IF @Notes IS NOT NULL
    BEGIN
        set @output = @output + '---' + @Notes +  CHAR(13) + CHAR(10)
    END

    FETCH NEXT FROM StatusChanges INTO @NewStatusID, @UserName, @Stamp, @Notes
  END
CLOSE StatusChanges
DEALLOCATE StatusChanges

RETURN @output

END

Now, that function returns exactly what I want for the Transactions that don't have any Notes in any records...
For transaction that have at least one record in TransactionStatusChanges with a non-NULL Notes field, I get NULL.

I don't quite get it, since I AM checking that @Notes is not NULL before concatting it.

Any ideas?

NOTE: I'm using varchar(8000) because I can't use text inside Functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛泛之交 2024-07-13 03:50:50

其中之一是 NULL

   set @output = @output + RTRIM(CAST(@Stamp AS varchar(30))) + ': ' + @NewStatusID + ' by ' + @UserName +  CHAR(13) + CHAR(10)

另外,您可以通过使用 ISNULL 或 COALESCE 来处理包含 NULL 的列,从而使代码更简单

CREATE FUNCTION fGetTransactionStatusLog
(
    @TransactionID  int
)
RETURNS varchar(8000) AS  
BEGIN 

declare @output AS varchar(8000)

select @output = ISNULL(@output, '')
        + ISNULL(RTRIM(CAST(Stamp AS varchar(30))), '<NULL>')
        + ISNULL(NewStatusID, '<NULL>') + ' by '
        + ISNULL(FirstName + ' ' + LastName, '<NULL>') + CHAR(13) + CHAR(10)
        + ISNULL('---' + Notes +  CHAR(13) + CHAR(10), '')
from TransactionStatusChanges tsc
left join Users us ON tsc.UserID = us.UserID
where TransactionID = @TransactionID ORDER BY StatusNum

RETURN @output

END

One of these is NULL

   set @output = @output + RTRIM(CAST(@Stamp AS varchar(30))) + ': ' + @NewStatusID + ' by ' + @UserName +  CHAR(13) + CHAR(10)

Also, you can make your code simpler by using ISNULL or COALESCE to handle columns which contain NULLs

CREATE FUNCTION fGetTransactionStatusLog
(
    @TransactionID  int
)
RETURNS varchar(8000) AS  
BEGIN 

declare @output AS varchar(8000)

select @output = ISNULL(@output, '')
        + ISNULL(RTRIM(CAST(Stamp AS varchar(30))), '<NULL>')
        + ISNULL(NewStatusID, '<NULL>') + ' by '
        + ISNULL(FirstName + ' ' + LastName, '<NULL>') + CHAR(13) + CHAR(10)
        + ISNULL('---' + Notes +  CHAR(13) + CHAR(10), '')
from TransactionStatusChanges tsc
left join Users us ON tsc.UserID = us.UserID
where TransactionID = @TransactionID ORDER BY StatusNum

RETURN @output

END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文