1

我正在制作一个接收@UserId(int) 和@CategoryId(int) 的存储过程。我现在必须切断用户和类别之间的任何连接,但是 - 用户仅直接连接到一个或多个子类别(此连接位于名为“HelpWith”的表中),而这些子类别又连接到单个每个类别(一个类别可以包含多个子类别)。为了找到值,我做了以下 SELECT 语句:

SELECT SubcategoryId
FROM [Subcategory] as s
LEFT JOIN [Category] as c ON c.CategoryId = s.CategoryId
WHERE c.Categoryid = @CategoryId 

上面的 SELECT 语句可以“找到”多个 SubcategoryIds,因此,我想我必须以某种方式将值插入到表中,以便我可以执行以下操作:

DELETE FROM HelpWith

WHERE SubcategoryId IN (SELECT ID FROM @TempTable)
AND userId = @UserId

有谁知道这可能是如何实现的?

4

1 回答 1

0

通过在 DELETE 中使用 JOIN 解决

USE [MAPP]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[MA_SetTutor_RemoveCategory] 

    @UserId int,
    @CategoryId int

AS
BEGIN

DELETE FROM HelpWith

WHERE SubcategoryId IN
(
    SELECT SubcategoryId
    FROM [Subcategory] AS s
    LEFT JOIN [Category] AS c ON c.CategoryId = s.CategoryId
    WHERE c.CategoryId = @CategoryId
    AND userId = @UserId
)
END 
于 2013-05-28T11:09:19.480 回答