You can do all of that using a T-SQL script. Probably the easiest way to generate the script is as follows: Bring up the "Restore Database" window as you normally would if you were to do it manually, make all the changes you normally do, but instead of clicking OK to proceed, click the script button near the top left of the right panel. That will generate a script to do the restore.

Once you have that script, you can run that script each day, or you can schedule a SQL Server Agent job t run that script.

But, I did not follow the reason you need to restore the prior day's database to do this query if you still have the database on the server. The query I posted earlier was showing you how to get the data for a prior day from the current database without having to restore from yesterday's backup.

for that why you need to restore db? sounds like what you need is a audit table to track data changes that happened each day for use in the next day. your current data will reside in main table and previous data in your audit table.

How could i access to audit tables? Could i access to old data in a specific moment? For example, yesterday 4:00 a.m.? I have to change all references to tables or could i only change the reference to an audit DATABASE in that specific moment?

How could i access to audit tables? Could i access to old data in a specific moment? For example, yesterday 4:00 a.m.? I have to change all references to tables or could i only change the reference to an audit DATABASE in that specific moment?

Thanks

you dont need to change database reference

Assuming all your audit data for a table is captured to a corresponding table in audit db with date field indicating dateyou can simply use query like

SELECT *
FROM AUDITDB.dbo.AuditTable
WHERE Datefield>=@Start
AND Datefield<= @End

here @Start and @End represent range for which you want to capture audit