?
Solved

Create a stored procedure

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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