我想让用户只输入一个具有特定ReqNo
的数据条目,即Type
等于'SS'。有两个问题:
Msg 4145,Level 15,State 1,Line 21非布尔类型的表达式 在预期条件的上下文中指定,在')'附近。
列出ENDS(我添加此行,以便解析器将开始解析代码,否则它不会)
CREATE FUNCTION dbo.ReqNoIsUniquePerReserve
(
@ReqNo AS BIGINT,
@Type AS CHAR
)
RETURNS BIT
AS
BEGIN
IF UPPER(@Type) = 'SS'
BEGIN
IF EXISTS (SELECT TOP 1 Id FROM dbo.LiaQueue WHERE @ReqNo = ReqNo)
RETURN 0;
ELSE
RETURN 1;
END
ELSE IF @ReqNo IS NOT NULL
BEGIN
IF EXISTS (SELECT TOP 1 ID FROM dbo.LiaQueue WHERE @ReqNo = ReqNo)
RETURN 1;
ELSE
RETURN 0;
END
RETURN 1;
END
ALTER TABLE dbo.LiaQueue
ADD CONSTRAINT CK_LiaQueue_ReqNo_Unique
CHECK (dbo.[ReqNoIsUniquePerReserve](ReqNo,[Type]))
USE [Liaison]
GO
/****** Object: UserDefinedFunction [dbo].[ReqNoIsUniquePerReserve] Script Date: 10/03/2016 12:48:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[ReqNoIsUniquePerReserve]
(
@Id AS BIGINT,
@Command AS NVARCHAR,
@ReqNo AS BIGINT
)
RETURNS BIT
AS
BEGIN
IF UPPER(@Command) = 'SS'
BEGIN
IF EXISTS (SELECT TOP 1 Id FROM dbo.LiaQueue WHERE @ReqNo = ReqNo AND Id <> @Id)
RETURN 0;
ELSE
RETURN 1;
END
ELSE IF @ReqNo IS NOT NULL
BEGIN
IF EXISTS (SELECT TOP 1 ID FROM dbo.LiaQueue WHERE @ReqNo = ReqNo)
RETURN 1;
ELSE
RETURN 0;
END
RETURN 1;
END
ALTER TABLE dbo.LiaQueue
ADD CONSTRAINT CK_LiaQueue_ReqNo_Unique
CHECK (dbo.[ReqNoIsUniquePerReserve](Id, Command, ReqNo) = 1)
答案 0 :(得分:1)
1)
你试过了吗?
ALTER TABLE dbo.LiaQueue
ADD CONSTRAINT CK_LiaQueue_ReqNo_Unique
CHECK (dbo.[ReqNoIsUniquePerReserve](ReqNo,[Type]) = 1)
对于2),我不太清楚你的意思。