473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stored Procedure for deleting multiple records from 3 tables

Thanks in advance for your help. I need a Stored Procedure that will do the following steps:

1. delete a single record from table GROUP

2. delete all records from table SUBGROUP with a matching 'groupID'
from the deleted GROUP
2. as each record from SUBGROUP is deleted there are multiple associated
records in the table PROFILE with a matching 'subGroupID' that also
need to be deleted


Table definitions: ---------------------------------------------------


CREATE TABLE [dbo].[tblGroup](
[groupID] [int] IDENTITY(100000,1) NOT NULL,
[groupName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
) ON [PRIMARY]


CREATE TABLE [dbo].[tblSubGroup](
[subGroupID] [int] IDENTITY(100000,1) NOT NULL,
[subGroupName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[groupID] [int] NOT NULL,
[profileID] [int] NOT NULL,
) ON [PRIMARY]


CREATE TABLE [dbo].[tblProfile](
[profileID] [int] IDENTITY(100000,1) NOT NULL,
[profileName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[subGroupID] [int] NOT NULL,
) ON [PRIMARY]
Feb 7 '08 #1
2 9436
amitpatel66
2,367 Expert 2GB
Thanks in advance for your help. I need a Stored Procedure that will do the following steps:

1. delete a single record from table GROUP

2. delete all records from table SUBGROUP with a matching 'groupID'
from the deleted GROUP
2. as each record from SUBGROUP is deleted there are multiple associated
records in the table PROFILE with a matching 'subGroupID' that also
need to be deleted


Table definitions: ---------------------------------------------------


CREATE TABLE [dbo].[tblGroup](
[groupID] [int] IDENTITY(100000,1) NOT NULL,
[groupName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
) ON [PRIMARY]


CREATE TABLE [dbo].[tblSubGroup](
[subGroupID] [int] IDENTITY(100000,1) NOT NULL,
[subGroupName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[groupID] [int] NOT NULL,
[profileID] [int] NOT NULL,
) ON [PRIMARY]


CREATE TABLE [dbo].[tblProfile](
[profileID] [int] IDENTITY(100000,1) NOT NULL,
[profileName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[subGroupID] [int] NOT NULL,
) ON [PRIMARY]
Why you need a procedure to do that in case if it is going to be one time process?

And is there any primary key - foreign key relation shp between these tables?
Feb 7 '08 #2
scripto
143 100+
here is a quick solution - we could add error checking between deletes

create procedure dbo.usp_DeleteRecords
@id_to_delete int

as

delete from [dbo].[tblGroup]
where [groupID] = @id_to_delete


delete from [dbo].[tblProfile]
where [subGroupID] in (select [subGroupID] from [dbo].[tblSubGroup]
where [GroupID] = @id_to_delete)

delete from [dbo].[tblSubGroup]
where [GroupID] = @id_to_delete
Feb 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: deprins | last post by:
Hello, I have wrote a stored procedure but its real slow. Its activated by a button on web page but its takes to long to process and the web server gives a timeout message after 5 minutes. Is...
18
by: Jarrod Morrison | last post by:
Hi All I was wondering if there is a way to call a stored procedure from inside another stored procedure. So for example my first procedure will call a second stored procedure which when...
1
by: Brad H McCollum | last post by:
I've looked through many suggestions and partial examples all over this newsgroup and still am not coming up with anything that does specifically what I'm wanting to accomplish. I'm writing a VB...
10
by: Eric E | last post by:
Hi all, I am using an Access client linked to a PG 7.4 server via ODBC. I have a stored proc on the server that inserts rows into a table.particular table, accomplished via an INSERT within the...
6
by: Catch_22 | last post by:
Hi, I have a large SQL Server 2000 database with 3 core tables. Table A : 10 million + records Table B : 2 million + records Table C : 6 million + records One of the batch tasks that I...
9
by: vikram.mankar | last post by:
I have a stored procedure thats transferring/processing data from one table to two different tables. The destination tables have a unique value constraint as the source tables at times has...
4
by: yin_n_yang74 | last post by:
I am new to SQL and SQL Server world. There must be a simple solution to this, but I'm not seeing it. I am trying to create a crystal report (v8.5) using a stored procedure from SQL Server...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
2
by: padmaneha | last post by:
Hi I have created two tables 'TrainsMaster' & 'TransArrvlDepinfo' Columns which I have created in 'TrainsMaster' are 'trainName,TrainNo, StartStaionId, & EndstationId' Columns which I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.