Solved

Move & Delete SP

Posted on 2011-03-07
8
235 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Throw Error 7 31
SQL Server Sum Over Multiple Tables 20 30
Sql case statement to calculate totals 5 32
SQL 2014 missing dll from Bin? 3 31
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

685 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