Link to home
Start Free TrialLog in
Avatar of earngreen
earngreenFlag for United States of America

asked on

Append data to table after populating virtual table

I need to create a virtual table with the current date and time and append this date and time to a table to show when the query was last run. I am going to do this in a stored procedure to keep track of when the data was last run. if anyone can assist with this that would be great or if anyone knows a better approach please let me know.  Please be specific.

Thanks
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

>with the current date and time
well, that is the function getdate() in sql server

>stored procedure to keep track of when the data was last run.
create table track_procedure ( date_called datetime )


and in the procedure, do this insert:

insert into track_procedure (date_called) select getdate()
Can you show the rest of the code of your stored procedure, and the setup of the destination table?

I would suggest simply adding a line of code in the stored procedure with something to this affect

INSERT INTO destinationTableName
(DateTimeLastRun, ProcedureName)
VALUES
(getdate(), 'This ProcedureName')
yeah what angell said will work.. select getdate() is what I should have said instead of just getdate().. my apologies.
ASKER CERTIFIED SOLUTION
Avatar of bwdowhan
bwdowhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I like that approach, bw.. You can modify that stored procedure and not have to open each stored procedure that you want logged (as long as you aren't changing the input paramaters)..

Very nice.
Avatar of earngreen

ASKER

I like the approaches that you have suggested. I need to log the date aand then use that date as a parameter in my where clause like below. bw in your example, can I just pull the information at that time from the log table.
here is an example of what I have thanks.

CREATE PROCEDURE

SELECT DISTINCT emp.empid,emp.first,emp.last,emp.hiredate,hr.value from emp inner join emp.empid = hr.emdpid
where emp.hiredate between lastrundate(logdate) and today
Avatar of bwdowhan
bwdowhan

You could query the LOG_TABLE to retrieve the last run_date of the stored procedure you are in and store the result in a variable to use in your query:

Before you:
 EXEC LOG_ENTRY 'MY_STORED_PROCEDURE_NAME'

You would get the last run date with something like this:

DECLARE @LAST_RUN Datetime

SET @LAST_RUN = (SELECT MAX(RUN_DATE) FROM LOG_TABLE WHERE SP_NAME = 'MY STORED_PROCEDURE_NAME')

In your query you would just say:

SELECT DISTINCT emp.empid,emp.first,emp.last,emp.hiredate,hr.value from emp inner join emp.empid = hr.emdpid
where emp.hiredate >= @LAST_RUN