?
Solved

Create a stored procedure

Posted on 2014-02-23
2
Medium Priority
?
196 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

741 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