Solved

Create a stored procedure

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

777 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