[英]Uniqueidentifier as parameter in SQL Server Function
I have created a Function in SQL Server 2012 that I will use in a Check Constraint on a table. 我在SQL Server 2012中创建了一个函数,该函数将在表的检查约束中使用。 The function works as expected if I do:
如果我这样做,该功能将按预期工作:
SELECT [dbo].[CheckValidCardnumberForShellTankingen] ('700678036658047691' ,'2925CA00-6DD5-4F9D-AB0E-AA15DBBD388B')
But when I try to set the expression in Check Constraint so: 但是,当我尝试在Check Constraint中设置表达式时:
([dbo].[CheckValidCardnumberForShellTankingen]([Volledig kaartnummer],[RollBackCode])=(1))
I get a Messaage: "Error validating constraint 'CK_MyConstraint'"
我收到一条消息:
"Error validating constraint 'CK_MyConstraint'"
I use the Uniqueidentifier in a Where clause and the strange thing is if I replace the parameter with string containing the Uniqueidentifier I dont get this error. 我在Where子句中使用Uniqueidentifier,奇怪的是,如果我用包含Uniqueidentifier的字符串替换参数,则不会收到此错误。
Here is the Function: 这是功能:
-- =============================================
-- 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
If @RollBackCode is a uniqueidentifier, I recommend making the parameter a uniqueidentifier and not a varchar. 如果@RollBackCode是uniqueidentifier,则建议将参数设置为uniqueidentifier而不是varchar。
As Rhys Jones points out, you shouldn't use a UDF in a check constraint. 正如Rhys Jones指出的那样,您不应在检查约束中使用UDF。
See 看到
https://dba.stackexchange.com/questions/22297/udf-in-check-constraint-downside https://dba.stackexchange.com/questions/22297/udf-in-check-constraint-downside
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/078b720f-faac-425c-b51a-33bcecb263d2/check-constraint-with-udf-problem-with-lots-of-data?forum=transactsql https://social.msdn.microsoft.com/Forums/sqlserver/en-US/078b720f-faac-425c-b51a-33bcecb263d2/check-constraint-with-udf-problem-with-lots-of-data?forum= TRANSACTSQL
http://sqlblog.com/blogs/tibor_karaszi/archive/2009/12/17/be-careful-with-constraints-calling-udfs.aspx http://sqlblog.com/blogs/tibor_karaszi/archive/2009/12/17/be-careful-with-constraints-calling-udfs.aspx
If you need to check in a trigger and roll back -- SQL Server - After Insert/ For Insert - Rollback 如果需要签入触发器并回滚-SQL Server-插入后/对于插入-回滚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.