传递给SUBSTRING函数的长度参数无效

时间:2009-07-21 10:38:53

标签: sql-server database stored-procedures

使用SQL和事务进行一些运行...希望你们中的一些人可以发现问题。 (甚至可能修复我搞砸的HTML!这个网站是否支持不同语言的格式化?Perl,SQL,Java?)这个服务器在SQL Server 2005上运行,最近从SQL Server 2000升级。我会留意这个全天发布。干杯

ALTER PROCEDURE [dbo].[sp_AddRequest] 
    -- Add the parameters for the stored procedure here
    @Message TEXT,
    @RequestId VARCHAR(20),
    @StatusCode CHAR(1),
    @StatusText VARCHAR(255),
    @AddedDate DATETIME,
    @MessageTimestamp DATETIME
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Direction is incoming (i.e. Client -> WEBAPP)
    DECLARE @Direction VARCHAR(50)
    SET @Direction = 'Client -> WEBAPP'

    -- Type is derived from:
    -- a) MessageType Element value OR
    -- b) Data Element first child OR
    -- c) Root Element name
    DECLARE @Type VARCHAR(50)
    SELECT @Type = dbo.fnGetValue('MessageType', @Message)
    IF @Type IS NULL SELECT @Type = dbo.fnGetFirstChild('Data', @Message)
    IF @Type IS NULL SELECT @Type = dbo.fnGetFirstChild(NULL, @Message) 

    -- MessageStatus is retrieved from the lookup table
    DECLARE @Status VARCHAR(50)
    SELECT @Status = Description FROM MessageStatus WHERE MessageStatusCode = @StatusCode

    -- Examine the Message root element
    IF dbo.fnGetFirstChild(NULL, @Message) = 'RequestMessage'
    BEGIN
        -- Insert values into the Transaction table
        INSERT INTO tblTransaction (RequestID, Direction, [Type], Status, StatusText, Sent, Received, Body)
        VALUES (@RequestId, @Direction, @Type, @Status, @StatusText, @MessageTimestamp, @AddedDate, @Message)
        RETURN @@IDENTITY
    END
    ELSE
    BEGIN
        -- Transaction is linked using the RequestId
        DECLARE @TransactionID INT
        SELECT @TransactionID = dbo.fnFindTransaction(@RequestId)

        -- Insert values into the RelatedMessage table
        INSERT INTO tblRelatedMessage (TransactionID, RequestID, Direction, [Type], Status, StatusText, Sent, Received, Body)
        VALUES (@TransactionID, @RequestId, @Direction, @Type, @Status, @StatusText, @MessageTimestamp, @AddedDate, @Message)
        RETURN @@IDENTITY
    END
END

此致

2 个答案:

答案 0 :(得分:1)

你的子串必须是函数中的一个,例如fnGetValue或fnGetFirstChild

假设它是,您可以通过将负数传递给length参数来生成相同的错误。传递NULL或'bob'或20亿或浮动:它可以工作或给出不同的错误。

SELECT SUBSTRING ('ffggg', 1, -1)

Msg 536, Level 16, State 1, Line 1
Invalid length parameter passed to the substring function.

另一点:不要使用@@ IDENTITY。永远。使用SCOPE_IDENTITY()。

答案 1 :(得分:0)

这里有一个缺少的结尾引用(在WEBAPP之后):

 SET @Direction = 'Client -> WEBAPP'

(我在列表中修复了)但是我找不到任何对SUBSTRING的引用........问题是什么,真的吗?

马克

相关问题