065 EXISTS

SQL

DECLARE @ProductId int = 10

IF NOT    EXISTS(SELECT * FROM Products WHERE ProductId = @ProductId)
    SELECT 'The product doesn''t exists.'

IF EXISTS(SELECT * FROM Products WHERE ProductId = @ProductId and Quantity > 0)
    SELECT 'The product is in stock.'
ElSE
    SELECT 'The product is out of stock.'

select * from Products

select * 
from Categories C
where not exists(
    select *
    from Products p
    where C.CategoryId = P.CategoryId
    and P.Quantity > 0
)

猜你喜欢

转载自blog.csdn.net/KevinHuang2088/article/details/143372824