连接字段时,我在 SQL 函数中得到 NULL 输出
我有以下函数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其中之一是 NULL
另外,您可以通过使用 ISNULL 或 COALESCE 来处理包含 NULL 的列,从而使代码更简单
One of these is NULL
Also, you can make your code simpler by using ISNULL or COALESCE to handle columns which contain NULLs