SQL 服务器 - 如何创建 IF EXISTS...SELECT AND SELECT
SQL Server - How to create IF EXISTS...SELECT AND SELECT
我正在尝试对我的代码创建一个 IF/ELSE
检查,以确保参数 @BookTitle
和 @GenreName
是表 BookGenre
和尝试删除它们之前的流派。我试图编写这行代码,但收到错误,
An expression of type non-Boolean type specified where condition is expected.
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle AND
SELECT Id FROM Library.Genre WHERE Name = @GenreName)
ELSE
我需要一些指导,了解如何创建包含两个单独的 select 语句的 IF EXISTS
语句表达式作为 IF 检查的一部分。
下面是完整的存储过程(没有 IF 语句):
PROCEDURE [Library].[DeleteDateFromBookGenre]
-- Parameters to select which record delete
@BookTitle nvarchar(200),
@GenreName nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle)
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle)
-- Deletes the specified and selected data from the Genre and Book table.
DELETE FROM Library.BookGenre
WHERE BookId IN (
SELECT Id
FROM Library.Book
WHERE Title = @BookTitle
) AND
GenreId IN (
SELECT Id
FROM Library.Genre
WHERE Name = @GenreName
)
END
你的括号不对;你需要两个 EXISTS
条件:
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle
) AND
EXISTS (SELECT Id FROM Library.Genre WHERE Name = @GenreName
)
BEGIN
. . .
END
我正在尝试对我的代码创建一个 IF/ELSE
检查,以确保参数 @BookTitle
和 @GenreName
是表 BookGenre
和尝试删除它们之前的流派。我试图编写这行代码,但收到错误,
An expression of type non-Boolean type specified where condition is expected.
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle AND
SELECT Id FROM Library.Genre WHERE Name = @GenreName)
ELSE
我需要一些指导,了解如何创建包含两个单独的 select 语句的 IF EXISTS
语句表达式作为 IF 检查的一部分。
下面是完整的存储过程(没有 IF 语句):
PROCEDURE [Library].[DeleteDateFromBookGenre]
-- Parameters to select which record delete
@BookTitle nvarchar(200),
@GenreName nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle)
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle)
-- Deletes the specified and selected data from the Genre and Book table.
DELETE FROM Library.BookGenre
WHERE BookId IN (
SELECT Id
FROM Library.Book
WHERE Title = @BookTitle
) AND
GenreId IN (
SELECT Id
FROM Library.Genre
WHERE Name = @GenreName
)
END
你的括号不对;你需要两个 EXISTS
条件:
IF EXISTS (SELECT Id FROM Library.Book WHERE Title = @BookTitle
) AND
EXISTS (SELECT Id FROM Library.Genre WHERE Name = @GenreName
)
BEGIN
. . .
END