0

I have a question regarding stored procedures and SQL. I have a job running everyday containing a stored procedure which makes an insert into a database table.

This is the current situation: I have to insert the data about yesterday, in this stored procedure I take the data of today, decrease 1 day and save the elements in my DB, in this way I have the data of yesterday.

This is the desired situation: I have to insert the data about the previous week, starting from yesterday.

In the stored procedure, I want to do the following steps:

  • I set the data of yesterday and I make the first insert
  • After 25 minutes I decrease one day so that I have the data of 2 days before and I insert the data
  • I repeat the same process until the number of days I've defined is lower than a counter I increase every time I make an insert

Question: how in SQL can I write: wait for 25 minutes then do this?

I couldn't simply run the job every 25 minutes because, when I decrease the date (the date is the name of the table such as data_20210926), I should know the starting date (is it today - 3, today - 4 or else) and I can't know this information and the job in my system can't have in input the result of the output of the previous execution

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You could create a [TempDb permanent table](https://stackoverflow.com/a/2921091/205233) to store information to pass between runs instead of waiting which may prove more robust. – Filburt Sep 27 '21 at 09:06
  • [Waitfor documentation](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/waitfor-transact-sql?view=sql-server-ver15) – Stu Sep 27 '21 at 09:11
  • 3
    Procedural code (like stored procedures) is **highly vendor-specific** - so please add a tag to specify whether you're using `mysql`, `postgresql`, `sql-server`, `oracle` or `db2` - or something else entirely. – marc_s Sep 27 '21 at 09:17

0 Answers0