我正在创建一个接收@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
有谁知道如何实现这一目标?
答案 0 :(得分: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