簡體   English   中英

在SQL Server函數中將Uniqueidentifier作為參數

[英]Uniqueidentifier as parameter in SQL Server Function

我在SQL Server 2012中創建了一個函數,該函數將在表的檢查約束中使用。 如果我這樣做,該功能將按預期工作:

SELECT [dbo].[CheckValidCardnumberForShellTankingen] ('700678036658047691' ,'2925CA00-6DD5-4F9D-AB0E-AA15DBBD388B')

但是,當我嘗試在Check Constraint中設置表達式時:

([dbo].[CheckValidCardnumberForShellTankingen]([Volledig kaartnummer],[RollBackCode])=(1))

我收到一條消息: "Error validating constraint 'CK_MyConstraint'"

我在Where子句中使用Uniqueidentifier,奇怪的是,如果我用包含Uniqueidentifier的字符串替換參數,則不會收到此錯誤。

這是功能:

-- =============================================
-- Author:      Anders Pedersen
-- Create date: 2015-02-13
-- Description: Check of the Cardnumber of a transaction is valid.
-- =============================================
CREATE FUNCTION [dbo].[CheckValidCardnumberForShellTankingen]
(
    -- Add the parameters for the function here
    @Cardnumber NvarChar(50),
    @RollBackCode NvarChar(200)
)
RETURNS BIT
AS
BEGIN
    -- Declare the return variable here
    DECLARE 
     @Result                BIT
    ,@ResultLenght          BIT
    ,@ResultPrefix          BIT
    ,@CardLenght            INT
    ,@SupplierID            INT
    ,@UseCardnumber         BIT
    ,@Prefix                NvarChar(50)

    -- Add the T-SQL statements to compute the return value here
     SET @Result                = 0
     SET @ResultLenght          = 0
     SET @ResultPrefix          = 0
     SET @CardLenght            = -1
     SET @SupplierID            = -1
     SET @UseCardnumber         = 0
     SET @Prefix                = ''

    -- Get the UseCardnumber and the SupplierID
    SELECT @UseCardnumber = C.UseCardNumber, @SupplierID = F.SupplierID
    FROM Client C INNER JOIN
        ClientFileUploads F ON C.ClientID = F.ClientID
    WHERE F.RollBackCode = @RollBackCode
    --WHERE F.RollBackCode = '2925CA00-6DD5-4F9D-AB0E-AA15DBBD388B' 

    -- Only carry out the check if the Client use Cards else set the check to True (1)
    IF @UseCardnumber = 1
    BEGIN
        SELECT  @CardLenght =  [CardNumberLenght], @Prefix = ISNULL([Prefix],'') FROM [dbo].[Supplier] AS S WHERE S.SupplierID = @SupplierID

        IF (@CardLenght IS NULL) OR (@CardLenght = 0)
        BEGIN
            SET @ResultLenght = 1
        END
        ELSE
        BEGIN
        IF  (LEN(@Cardnumber) - @CardLenght)= 0
            BEGIN
                SET @ResultLenght = 1
            END
            ELSE
            BEGIN
                SET @ResultLenght =  0
            END
        END

        IF  SUBSTRING(@Cardnumber, 1, LEN(@Prefix)) = @Prefix
        BEGIN
            SET @ResultPrefix = 1
        END
        ELSE
        BEGIN
            SET @ResultPrefix =  0
        END

        IF ((@ResultLenght = 1) AND (@ResultPrefix = 1))
        BEGIN
            SET @Result = 1
        END
        ELSE
        BEGIN
            SET @Result = 0
        END     
    END
    ELSE
    BEGIN
        SET @Result = 1
    END

    -- Return the result of the function
    RETURN @Result

END

GO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM