SQl Server Jobs

  • Is there a way to view the results of a query in a stored procedure that is run by a SQL Job? I have a code that inserts records from one tabel to another and then deletes the inserted record from the original table. I want to see the number of records that was inserted and deleted to mak sure thay match

  • If the procedure generates an output then it will usually be stored in the job history, if not unless you have some custom logging on the tables then I dont think you can.

  • The job history does not record the results of the query in the SP.

    Nice to hear from you again.

    Thanks. 🙂

  • I think what I would do is alter your stored procedure so that it gives a return value of the number of rows inserted, then either insert that value into a table that you can query at your leisure, or have it e-mailed to you.

    John

  • ouwanogho (6/13/2012)


    Is there a way to view the results of a query in a stored procedure that is run by a SQL Job? I have a code that inserts records from one tabel to another and then deletes the inserted record from the original table. I want to see the number of records that was inserted and deleted to mak sure thay match

    Hi,

    the following query may help you to check the status of your executed query..

    The following query may help you...

    Select

    *

    From

    msdb..sysjobhistory as sysjobhistory

    Join msdb..sysjobs as sysjobs on sysjobhistory.job_id=sysjobhistory.job_id

    Where

    Name='Your Schd. Package Name'

    Order By

    Run_Date Desc,

    run_time Desc

    Thanks

  • How about deleting records from original table and inserting to another table using OUTPUT..DELETED.. INTO..

    On error rollback the transaction. This approach will make sure you are deleting in one table what you are inserting in another table.

  • You could trying using powershell and output results within Job step probably

    http://msdn.microsoft.com/en-us/library/cc280490.aspx

    Or use xp command shell .. (but that generally is turned off for security purposes-- You will have to use sp_configure to turn it on) Your SQL Server service account will need access to the folder you're trying to write to as well.

    declare @Command varchar(500)

    declare @sqlcount varchar(20)

    select @sqlcount = COUNT(SomeID) from SomeTable

    SET @Command = 'echo Results from Query count:'+@sqlcount+' > c:\temp\output.txt'

    EXEC xp_cmdshell @Command

    Hope this was helpful

    --------------------------------------------------
    ...0.05 points per day since registration... slowly crawl up to 1 pt per day hopefully 😀

  • This code does not show the result of the query within the stored procedure

  • What's in the job step? What code is there?

  • If you're trying to write out the results to a file to log... ( others may have more elegant method)

    But one way is to specify an "OUTPUT" parameter in your stored sproc (that means changing your stored procedure)

    Taking the Output result value... assigning it a variable - You could store the rowcount results maybe from your insertion?

    Write that out using powershell or xp_cmd (though xp_cmd is not recommended method)

    --------------------------------------------------
    ...0.05 points per day since registration... slowly crawl up to 1 pt per day hopefully 😀

  • Can i get a sample powershell script that outputs the result of a stored procedure run by a sql job

  • You're confused.

    There is no output of the job here. You get logging from the job, or you get the output from a stored proc, but don't conflate the idea that there's something special about the job.

    If you want the output of a stored proc, you have to execute it in a way that records the results, whether from SSMS or inside a job. Powershell can do that, so can SQLCMD. You can switch to a SQLCMD that stores the results, and then run that in the job.

  • I'm not sure why Powershell is the tool of choice here.

    Why don't you just alter the Proc. to log the required output to a Table?

    I don't believe the Job will automatically do that for you.

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply