Solved

Move & Delete SP

Posted on 2011-03-07
8
232 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
 

Author Closing Comment

by:garethtnash
ID: 35060963
thank you :)
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.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now