Solved

Create a stored procedure

Posted on 2014-02-23
2
190 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
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 142

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
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

707 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

12 Experts available now in Live!

Get 1:1 Help Now