純粋な SQL では、一致するとすぐに検索を停止するため、これが最も効率的です。
... ストアド プロシージャとして:
CREATE PROCEDURE EmailExists
@email varchar(254) = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @emailExists bit
SET @emailExists = 0
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Customer WHERE email = @email)
IF @emailExists = 0
BEGIN
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Member WHERE email = @email)
IF @emailExists = 0
BEGIN
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Instructor WHERE email = @email)
IF @emailExists = 0
BEGIN
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Employee WHERE email = @email)
END
END
END
SELECT @emailExists
END
... スカラー値関数として:
CREATE FUNCTION EmailExists
(
@email varchar(254)
)
RETURNS bit
AS
BEGIN
DECLARE @emailExists bit
SET @emailExists = 0
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Customer WHERE email = @email)
IF @emailExists = 0
BEGIN
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Member WHERE email = @email)
IF @emailExists = 0
BEGIN
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Instructor WHERE email = @email)
IF @emailExists = 0
BEGIN
SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Employee WHERE email = @email)
END
END
END
-- Return the result of the function
RETURN @emailExists
END
Linq を使用した C# では、Any 拡張子と || を使用できます。オペレーター。Any は通常、SQL で EXISTS に変換され、|| の評価が行われるためです。C# の演算子は怠惰なため、電子メールの最初の発生に達するとすぐに評価が停止します。
bool emailExists = customerEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase))
|| memberEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase))
|| instructorEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase))
|| employeeEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase));