Solved

Move & Delete SP

Posted on 2011-03-07
8
231 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
Comment Utility
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
Comment Utility
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
Comment Utility
Yup, looks right to me
0
 

Author Closing Comment

by:garethtnash
Comment Utility
thank you :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Expert Comment

by:bcopping
Comment Utility
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
Comment Utility
Would that not delete from both tables?
0
 
LVL 1

Expert Comment

by:bcopping
Comment Utility
shouldnt do
0
 

Author Comment

by:garethtnash
Comment Utility
ok thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Copy Database Wizard 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.
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.
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…
Viewers will learn how the fundamental information of how to create a table.

744 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

17 Experts available now in Live!

Get 1:1 Help Now