views:

80

answers:

3

I'm trying to setup a scheduled job that with one step that would insert the results from a sproc into a table.

INSERT INTO results_table EXEC sproc

The job executes and reports a success. Yet nothing gets inserted into a table. When I execute the same script from the SSMS the results are inserted. What might cause the problem?

** EDIT the job is owned by sa and the step is executed as dbo. All the runs in the history are reported as finished successfully. I've tried changing the step to

INSERT INTO results_table(field_names) (SELECT values FROM table GROUP BY column_name) 

and it behaves in a similar way

** EDIT the problem only occurs when I select from the master database. Selecting from other tables works fine.

+1  A: 

Check if you are inserting in the Master database or the the database that you want to insert. Or call the SP with database instanse inside the job step

Insert Into Results_Table
EXEC <DBNAME>.<SchemaName>.<ProcedureName>
Baaju
@Baaju: That doesn't make sense - if it DID try to insert into the master database he'd get an error because that particular table doesn not exist within the database.
ajdams
Right ! had happened to us before while deploying the scripts the table also happened to be in the master DB. What does the job execution history say ?
Baaju
A: 

Have you tried inserting the results of the stored procedure into a temp table first then inserting them into your results_table? I would suggest that as well as this article which reviews this concept in-depth: http://www.sommarskog.se/share_data.html

ajdams
A: 

The problem was that in the scheduled job the stored procedure was executed not in the context of master database.

sumek