Solved

Create a stored procedure

Posted on 2014-02-23
2
195 Views
Last Modified: 2014-02-23
I have the following SQL statement which I would like to put into a stored procedure so that it can be executed once a day on a table:

--------------------------------------
DECLARE @maxid int
SELECT     @maxid = (SELECT MAX(ID) FROM tblAnalytics)
delete FROM tblAnalytics WHERE ID < @maxid-1000
--------------------------------------

Can anyone help me put this into a SP? I clicked new SP in SQL server 2012 and it has all sorts of things I am not familiar with.

Thanks you
0
Comment
Question by:net-workx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39880587
CREATE PROCEDURE deleteAnalytics
AS
BEGIN
    DECLARE @maxid int
    SELECT     @maxid = (SELECT MAX(ID) FROM tblAnalytics)
    DELETE FROM tblAnalytics WHERE ID < @maxid-1000
END
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39880649
I agree with the above, though I would put the code only slightly different :
CREATE PROCEDURE deleteAnalytics
AS
BEGIN
    DECLARE @maxid int
    SELECT  @maxid = MAX(ID) - 1000 FROM tblAnalytics
    DELETE FROM tblAnalytics WHERE ID < @maxid 
END 

Open in new window

it's though only a matter of style, as the @maxid - 1000 should not make any difference.
also, the subquery for the setting of the variable is not needed.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

705 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