Link to home
Start Free TrialLog in
Avatar of garethtnash
garethtnashFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Move & Delete SP

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

ASKER CERTIFIED SOLUTION
Avatar of derekkromm
derekkromm
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of garethtnash

ASKER

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

Yup, looks right to me
thank you :)
Avatar of bcopping
bcopping

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

Would that not delete from both tables?
shouldnt do
ok thanks