SQL函数调用返回意外结果

时间:2010-11-11 06:01:12

标签: c# sql sql-server tsql

请问我的函数调用有问题。我有一个我无法弄清楚的情况。我有一个简单的函数调用。 。

If(@confirm ='Y') 
 BEGIN 
  SELECT  dbo.ReplaceString(@rawText, '2342345432', 'radefr', @User_no, @password,@email,' ',' ',GetDate() ,@company, @end,  @start, @remove) as messagetext 
 END

我已经能够验证该函数是否正常工作,因为所有操作都是使用传入的参数进行简单的字符串替换。我遇到的问题是,当我按上面的方式插入调用时,我得到一个null值返回而不是最初按预期传入的字符串。除非代码中出现任何错误,否则当函数返回意外结果时是否还有其他情况?

  Function [dbo].[ReplaceString]
(
            @rawtext As Varchar(400), 
            @numbernum As Varchar(15), 
            @name As Varchar(25),   
            @userno As Bigint,  
            @password As Varchar(50) ,
            @email As Varchar(50) ,
            @keyword As VARCHAR(40),
            @litext As Varchar(500), 
            @datecreated As DateTime, 
            @company As Varchar(30), 
            @end As Varchar(140), 
            @start  As Varchar(140), 
            @remove As Varchar(200)

)
RETURNS VARCHAR(450)
AS 
BEGIN 

            SELECT @rawtext  = Replace( @rawtext , ''@@name@@'', @ name)
            SELECT @rawtext  = Replace( @rawtext , ''@@number@@'', @numbernum)
            SELECT @rawtext  = Replace( @rawtext , ''@@company@@'', @company )
            SELECT @rawtext  = Replace( @rawtext , ''@@ssn@@'', @numbernum )
            SELECT @rawtext  = Replace( @rawtext , ''@@message@@'', @littext )
            SELECT @rawtext  = Replace( @rawtext , ''@@date@@'', CAST(@datecreated AS VARCHAR(10)) )

            SELECT @rawtext  = Replace( @rawtext , ''@@keyword@@'', @ keyword )
            SELECT @message_text = Replace(@littext, @ keyword, '''' )
            SELECT @rawtext  = Replace( @rawtext , ''@@withoutkeyword@@'', @littext)
            SELECT @remove= Replace(@remove ''@@company@@'', @company)
            SELECT @start= Replace(@start, ''@@company@@'', @company)  
            SELECT @end = Replace(@end, ''@@company@@'', @company ) 
            SELECT @rawtext  = Replace( @rawtext , ''@@Settings[END]@@'',@end )
            SELECT @rawtext  = Replace( @rawtext , ''@@Settings[START]@@'', @start )
            SELECT @rawtext  = Replace( @rawtext , ''@@Settings[REMOVE]@@'', @remove)


         RETURN(@rawtext )

5 个答案:

答案 0 :(得分:3)

任何其他值的NULL varchar值concat都为NULL。

因此传入的其中一个连接参数可能是NULL

答案 1 :(得分:3)

NULL习惯在数据库服务器中传播;如果输入为NULL,则需要NULLISNULL,许多操作(特别是连接)将返回COALESCE。所以

  • @confirm的值是多少?什么是db collat​​ion (区分大小写?'y'在区分大小写的排序规则中不匹配)
  • ReplaceString 做什么(理想情况下:代码)
  • @rawText@User_no@password@email等的价值是什么......

答案 2 :(得分:2)

如果替换中使用的任何变量都为null,并且您没有用isnull或coalesce包装它们,那么最终可能会遇到这类问题。

答案 3 :(得分:1)

根据您最近的编辑,无法创建您描述的功能。这接近你的意图吗?如果您可以提供函数的工作示例以及传递给它的 @rawtext 值,那么这将有助于解决您的问题。

CREATE FUNCTION [dbo].[ReplaceString]
(
     @rawtext       VarChar(400)
    ,@numbernum     VarChar(15)
    ,@name          VarChar(25)  
    ,@userno        BigInt 
    ,@password      VarChar(50)
    ,@email         VarChar(50)
    ,@keyword       VarChar(40)
    ,@litext        VarChar(500)
    ,@datecreated   DateTime
    ,@company       VarChar(30)
    ,@end           VarChar(140)
    ,@start         VarChar(140)
    ,@remove        VarChar(200)
)
RETURNS VarChar(450)
As
Begin

    Declare @result VarChar(450)

    -- Set default values for null parameters
    Select
             @rawtext       = IsNull(@rawtext, '')
            ,@numbernum     = IsNull(@numbernum, '')
            ,@name          = IsNull(@name, '')
            ,@userno        = IsNull(@userno, 0)
            ,@password      = IsNull(@password, '')
            ,@email         = IsNull(@email, '')
            ,@keyword       = IsNull(@keyword, '')
            ,@litext        = IsNull(@litext, '')
            ,@datecreated   = IsNull(@datecreated, GetDate())
            ,@company       = IsNull(@company, '')
            ,@end           = IsNull(@end, '')
            ,@start         = IsNull(@start, '')
            ,@remove        = IsNull(@remove, '')

    Select @result  = Replace( @rawtext , '@@name@@', @name) 
    Select @result  = Replace( @result , '@@number@@', @numbernum) 
    Select @result  = Replace( @result , '@@company@@', @company ) 
    Select @result  = Replace( @result , '@@ssn@@', @numbernum ) 
    Select @result  = Replace( @result , '@@message@@', @litext ) 
    Select @result  = Replace( @result , '@@date@@', CAST(@datecreated As VarChar(10)) )

    Select @result          = Replace( @result , '@@keyword@@', @keyword ) 
    Select @result          = Replace(@litext, @keyword, '''' ) 
    Select @result          = Replace( @result , '@@withoutkeyword@@', @litext) 
    Select @remove          = Replace(@remove, '@@company@@', @company) 
    Select @start           = Replace(@start, '@@company@@', @company)   
    Select @end             = Replace(@end, '@@company@@', @company )  
    Select @result          = Replace( @result , '@@Settings[END]@@',@end ) 
    Select @result          = Replace( @result , '@@Settings[START]@@', @start ) 
    Select @result          = Replace( @result , '@@Settings[REMOVE]@@', @remove)

    Return @result
End
Go

REPLACE上的文档表明,如果任何一个参数为NULL ,将返回NULL。您应该考虑让您的ReplaceString函数使用ISNULL函数将任何输入替换为非空值,例如空字符串。

Declare @value nvarchar(10),
        @expression nvarchar(10),
        @replacement nvarchar(10)

Select  @value          = 'Some Value',
        @expression     = Null,
        @replacement    = 'Value2'

-- Result is NULL
Select REPLACE(@value, @expression, @replacement) as 'Result'

Select  @expression = 'Value'

-- Result is 'Some Value2'
Select REPLACE(@value, @expression, @replacement) as 'Result'

答案 4 :(得分:0)

除非是学术练习,否则我认为这不适合使用SQL。这是不可维护的。它应该是CLR函数或外部函数。