Solved

Move & Delete SP

Posted on 2011-03-07
8
234 Views
Last Modified: 2012-05-11
Hello,

I have the SP in the code section, which when called copies records that meet a criteria from the 'active' table too the 'expired' table and then deletes the original records.

I want to build a variation of this that accepts three criteria --

Del
JBAID
JBEUsername

The first part of this is that the SP should only run if Del='Y' that is the only purpose of the Del variable

The second part is that the delete function only deletes from the advert table where jbaid is the unique id and jbeusername is equal on a joined table like so--

"select *
from dbo.JBAdvert
where JBAID = 111
AND JBAEmployeeID IN
(Select JBAEmployeeID
from dbo.JBAdvert A
inner join dbo.JBEmployee E
on A.JBAEmployeeID = E.JBEID
where A.JBAID = 111 AND E.JBEUsername = 'account@domain.co.uk')"

Grateful for thoughts as to how I can do this please
USE [JobBoardRegion]
GO

/****** Object:  StoredProcedure [NetSolva].[Duplicate_Advert_Removal]    Script Date: 03/07/2011 19:59:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [NetSolva].[Duplicate_Advert_Removal] 
(
@JBASiteID integer,
@JBEmployeeID integer, 
@JBATitle nvarchar(150),
@JBALocation nvarchar(50),
@JBACategory nvarchar(50),
@JBAReference nvarchar(350),
@JBAEmplymentType nvarchar(50) 
)
AS
BEGIN
insert into dbo.JBExpiredAdvert
(JBAID, 
JBAEmployeeID, 
JBAClientID, 
JBASiteID, 
JBATitle, 
JBADescription, 
JBAPayRate, 
JBALocation, 
JBACategory, 
JBAEmplymentType,
JBAReference,
JBAStartDate, 
JBADatePosted, 
JBAConkersEmail, 
JBAFeaturedJob, 
JBAOverWrite)
Select 
JBAID, 
JBAEmployeeID, 
JBAClientID, 
JBASiteID, 
JBATitle, 
JBADescription, 
JBAPayRate, 
JBALocation, 
JBACategory, 
JBAEmplymentType,
JBAReference,
JBAStartDate, 
JBADatePosted, 
JBAConkersEmail, 
JBAFeaturedJob, 
JBAOverWrite
from dbo.JBAdvert
where JBASiteID = @JBASiteID
And JBAEmployeeID = @JBEmployeeID
And JBATitle = @JBATitle
And JBALocation = @JBALocation
And JBACategory = @JBACategory
And JBAReference = @JBAReference
And JBAEmplymentType = @JBAEmplymentType       
Delete from dbo.JBAdvert
where JBAEmployeeID = @JBEmployeeID
And JBATitle = @JBATitle
And JBALocation = @JBALocation
And JBACategory = @JBACategory
And JBAReference = @JBAReference
And JBAEmplymentType = @JBAEmplymentType      
END


GO

Open in new window

0
Comment
Question by:garethtnash
  • 4
  • 2
  • 2
8 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 35060854
Not sure if you want to also include the additional where clauses on the insert - currently its only on the delete, but it is easy enough to copy over.

USE [JobBoardRegion]
GO

/****** Object:  StoredProcedure [NetSolva].[Duplicate_Advert_Removal]    Script Date: 03/07/2011 19:59:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [NetSolva].[Duplicate_Advert_Removal] 
(
@Del varchar(1),
@JBAID int,
@JBEUsername varchar(100)
)
AS
BEGIN

if @Del<>'Y'
	return

insert into dbo.JBExpiredAdvert
(JBAID, 
JBAEmployeeID, 
JBAClientID, 
JBASiteID, 
JBATitle, 
JBADescription, 
JBAPayRate, 
JBALocation, 
JBACategory, 
JBAEmplymentType,
JBAReference,
JBAStartDate, 
JBADatePosted, 
JBAConkersEmail, 
JBAFeaturedJob, 
JBAOverWrite)
Select 
JBAID, 
JBAEmployeeID, 
JBAClientID, 
JBASiteID, 
JBATitle, 
JBADescription, 
JBAPayRate, 
JBALocation, 
JBACategory, 
JBAEmplymentType,
JBAReference,
JBAStartDate, 
JBADatePosted, 
JBAConkersEmail, 
JBAFeaturedJob, 
JBAOverWrite
from dbo.JBAdvert
where JBASiteID = @JBASiteID
And JBAEmployeeID = @JBEmployeeID
And JBATitle = @JBATitle
And JBALocation = @JBALocation
And JBACategory = @JBACategory
And JBAReference = @JBAReference
And JBAEmplymentType = @JBAEmplymentType     
  
Delete from dbo.JBAdvert
where where JBAID = @JBAID
AND JBAEmployeeID IN 
(Select JBAEmployeeID 
from dbo.JBAdvert A 
inner join dbo.JBEmployee E 
on A.JBAEmployeeID = E.JBEID   and e.JBEUsername = @JBEUsename
END


GO

Open in new window

0
 

Author Comment

by:garethtnash
ID: 35060906
So  the following?
USE [JobBoardRegion]
GO

/****** Object:  StoredProcedure [NetSolva].[Duplicate_Advert_Removal]    Script Date: 03/07/2011 19:59:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [NetSolva].[Duplicate_Advert_Removal] 
(
@Del varchar(1),
@JBAID int,
@JBEUsername varchar(100)
)
AS
BEGIN

if @Del<>'Y'
	return

insert into dbo.JBExpiredAdvert
(JBAID, 
JBAEmployeeID, 
JBAClientID, 
JBASiteID, 
JBATitle, 
JBADescription, 
JBAPayRate, 
JBALocation, 
JBACategory, 
JBAEmplymentType,
JBAReference,
JBAStartDate, 
JBADatePosted, 
JBAConkersEmail, 
JBAFeaturedJob, 
JBAOverWrite)
Select 
JBAID, 
JBAEmployeeID, 
JBAClientID, 
JBASiteID, 
JBATitle, 
JBADescription, 
JBAPayRate, 
JBALocation, 
JBACategory, 
JBAEmplymentType,
JBAReference,
JBAStartDate, 
JBADatePosted, 
JBAConkersEmail, 
JBAFeaturedJob, 
JBAOverWrite
from dbo.JBAdvert
where JBAID = @JBAID
AND JBAEmployeeID IN 
(Select JBAEmployeeID 
from dbo.JBAdvert A 
inner join dbo.JBEmployee E 
on A.JBAEmployeeID = E.JBEID   and e.JBEUsername = @JBEUsename
Delete from dbo.JBAdvert
where JBAID = @JBAID
AND JBAEmployeeID IN 
(Select JBAEmployeeID 
from dbo.JBAdvert A 
inner join dbo.JBEmployee E 
on A.JBAEmployeeID = E.JBEID   and e.JBEUsername = @JBEUsename
END


GO

Open in new window

0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35060941
Yup, looks right to me
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Closing Comment

by:garethtnash
ID: 35060963
thank you :)
0
 
LVL 1

Expert Comment

by:bcopping
ID: 35061003
dont think you realy need the sub query, wont a join suffice:


if @Del = 'Y'
begin
	insert into dbo.JBExpiredAdvert
		(JBAID, 
		JBAEmployeeID, 
		JBAClientID, 
		JBASiteID, 
		JBATitle, 
		JBADescription, 
		JBAPayRate, 
		JBALocation, 
		JBACategory, 
		JBAEmplymentType,
		JBAReference,
		JBAStartDate, 
		JBADatePosted, 
		JBAConkersEmail, 
		JBAFeaturedJob, 
		JBAOverWrite)
	Select 
		jba.JBAID, 
		jba.JBAEmployeeID, 
		jba.JBAClientID, 
		jba.JBASiteID, 
		jba.JBATitle, 
		jba.JBADescription, 
		jba.JBAPayRate, 
		jba.JBALocation, 
		jba.JBACategory, 
		jba.JBAEmplymentType,
		jba.JBAReference,
		jba.JBAStartDate, 
		jba.JBADatePosted, 
		jba.JBAConkersEmail, 
		jba.JBAFeaturedJob, 
		jba.JBAOverWrite
	from dbo.JBAdvert jba
	inner join dbo.JBEmployee e on e.JBEID = jba.JBAEmployeeID
	where jba.JBAID = @JBAID and e.JBEUsername = @JBEUsername


	delete jba
	from dbo.JBAdvert jba
	inner join dbo.JBEmployee e on e.JBEID = jba.JBAEmployeeID
	where jba.JBAID = @JBAID and e.JBEUsername = @JBEUsername
end

Open in new window

0
 

Author Comment

by:garethtnash
ID: 35061063
Would that not delete from both tables?
0
 
LVL 1

Expert Comment

by:bcopping
ID: 35061186
shouldnt do
0
 

Author Comment

by:garethtnash
ID: 35072608
ok thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question