Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:20:51+00:00 2026-06-12T08:20:51+00:00

I hasn’t been writing any MySql stored procedures before, so I don’t know them

  • 0

I hasn’t been writing any MySql stored procedures before, so I don’t know them at all. I have one database table which has some records in it. The main column is dateTime – when this record was saved in the database.

I have wrote the MySql stored procedure to select every record from the specified date:

CREATE PROCEDURE getMessages(IN dateFrom DATETIME)   
    SELECT * FROM EsbMessage WHERE dateTime <= dateFrom;

And this is the call:

CALL getMessages('2012-10-04 13:11:09');

This works correctly, it returns me the records from the specified date.

What I need to do is:

  1. If the record is over one week old, I need to update the other
    column.
  2. If the record is over one year old, I need to delete that record.

I can easily do this programmatically, but in this case I have to do this using stored procedure.

So I’am thinking of something like this:

CREATE PROCEDURE updateMessages(IN dateFrom DATETIME)   
    BEGIN
      SELECT * FROM EsbMessage WHERE dateTime <= dateFrom;
      #for each message
      #if the message is over one week old but not over one year old:
      UPDATE EsbMessage SET body = '';
      #if message is over one year old:
      DELETE FROM EsbMessage WHERE dateTime = #message.dateTime
END

But I don’t know how to use for loop in stored procedure, and how to write if statements depending on my requirements and the other thing I don’t now how to count the dates in MySql. For e.g. If I have the current date then I need to subtract the 365 days from the current date.

Could somebody help me with this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:20:52+00:00Added an answer on June 12, 2026 at 8:20 am

    How to loop and to use if clauses is described here: http://www.mysqltutorial.org/stored-procedures-loop.aspx

    I would do it without loops:

    CREATE PROCEDURE updateMessages(IN dateFrom DATETIME)   
    BEGIN      
      UPDATE EsbMessage SET body = '' where dateTime <= dateFrom -(86400*7); //86400 = 1 day
      #if message is over one year old:
      DELETE FROM EsbMessage where dateTime <= dateFrom -(86400*365);
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this hasn't been answered before. I have a stored procedure which enters
I know that this hasn't always been the case, so I have changed something
I hope this hasn't been asked before. I have a nullable boolean called boolIsAllowed
hoping this hasn't been asked before, but I have the following XML: <Company id=1000
I hope this question (or one similar) hasn't been asked before. Sincere apologies if
There hasn't been any books about ElasticSearch (that I know of), and http://www.elasticsearch.org/guide/ seems
All the googling around hasn't found an answer for me yet... I have one
I'm surprised this hasn't been posted yet. Any interesting tricks that you know about
I can't believe that this hasn't been covered before, but I have been having
I have an object that hasn't been created yet (so it has no ID),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.