簡體   English   中英

刪除相關記錄

[英]Delete Related Records

我正在嘗試編寫一個刪除相關記錄的存儲過程 如果您看下面的圖片,您將看到一個攤位如何具有攤位類型和大廳,以及攤位類型如何具有productid ,然后將其包含在產品表中。

本質上,我要刪除一個大廳,這也意味着要刪除展位,展位類型和展位類型的產品。

我什至不知道從哪里開始,因為我是sql的新手,所以任何指導或鏈接都值得贊賞。

在此處輸入圖片說明

如果您在表DDL上設置了級聯刪除,那么如果您在父表上刪除,則刪除將級聯到子表及其子級遞歸。

更多信息: http : //www.techonthenet.com/sql_server/foreign_keys/foreign_delete.php

要從每個表中手動刪除,您需要從依賴度最低的表到依賴度最高的表中刪除,從子級刪除到父級刪除,並使用聯接來幫助。

因此,我最終按照@TheMadDBA的建議使用了join和DELETE FROM,因此為他/她提供了支持。 始於唯有依靠

USE [DB]
GO
/****** Object:  StoredProcedure [Exhibit].[DeleteHallAndRelatedData]    Script Date: 7/7/2015 2:30:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


----------------------  DeleteHallAndRelatedData ----------------------------------------------------------------------------
ALTER PROC [Exhibit].[DeleteHallAndRelatedData]
    @OwnerId UniqueIdentifier,
    @HallId UniqueIdentifier    
AS
-----------------------------------------------------------------------------------------------------
SET NOCOUNT ON

Print N'DELETING PRICE SPLITS'

delete spl from 
Shopping.PriceSplit spl
join Shopping.Price prc on prc.id=spl.priceId
join Shopping.Product prd on prd.Id = prc.ProductId
join Exhibit.BoothType bt on bt.BoothTypeProductId=prd.Id
Join Exhibit.Booth b on b.BoothTypeId=bt.BoothTypeProductId
where
b.HallId= @HallId and b.OwnerId = @OwnerId

Print N'DELETING PRICES'
delete prc from 
Shopping.Price prc
join Shopping.Product prd on prd.Id = prc.ProductId
join Exhibit.BoothType bt on bt.BoothTypeProductId=prd.Id
Join Exhibit.Booth b on b.BoothTypeId=bt.BoothTypeProductId
where
b.HallId= @HallId and b.OwnerId = @OwnerId

Print N'DELETING PRODUCTS'
delete prd from 
Shopping.Product prd
join Exhibit.BoothType bt on bt.BoothTypeProductId=prd.Id
Join Exhibit.Booth b on b.BoothTypeId=bt.BoothTypeProductId
where
b.HallId= @HallId and b.OwnerId = @OwnerId

Print N'DELETING BOOTH TYPES'
delete bt from
Exhibit.BoothType bt
Join Exhibit.Booth b on b.BoothTypeId=bt.BoothTypeProductId
where
b.HallId= @HallId and b.OwnerId = @OwnerId

Print N'DELETING BOOTHS'
delete from Exhibit.Booth where HallId = @HallId

Print N'DELETING HALL'
delete from Exhibit.Hall where Id = @HallId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM