Create and then execute a stored procedure in a job step

  • Hi All,

    This is probably a simple process, but I don't know the most efficient way to handle it.

    We have a database that is restored each morning from a production backup (the database is used to trouble-shoot production problems in an exact replica of our production database.)

    There is some sensitive information we do not want available in this trouble-shooting database, so we created a stored procedure that encrypts the columns. The stored procedure does not exist on the production database, so when the "fix" database is created, the stored procedure doesn't exist there either.

    Is the best way to create the stored procedure to add the actual SP script directly into a step in the restore job? And then the next step would execute the newly-created SP? Or is there a more elegant way to create that procedure within the restore job?

    Open to suggestions!

    Thanks,

    Kay

  • Hmm..

    If you are looking for more simple way, I can see two:

    1. Create this SP in a master database instead, so it will be always there regardless of your restores

    2. Or just grab a code of your SP and put it as a script inside a step of your scheduled job.

  • Thanks for the prompt reply.

    I actually performed step 1 and created the stored procedure at the Master level and added a job step to EXEC the SP. However, when the job ran this morning when it reached that step, the job failed because it couldn't find the table that was being updated in the SP (it doesn't exist in the Master database, obviously.)

    Therefore, I thought your step 2 was the only option left to me. It sounds like the simplest way to go. Thank you!

  • You can fix your problem with SP like that.

    Just point your SP to the right database placing this code at the beginning of code in your SP:

    USE YourDB_name

    GO

    Don't forget to replace the YourDB_name with a real one.:)

  • This was a good piece of information. I actually thought about adding the "USE" statement, but I thought that controlled where the SP would be created. Your statement leads me to believe the stored procedure would still be created on the Master but when executed would run over the "USE" database. I'm displaying my lack of basic T-SQL knowledge.

    I'll use that approach instead of copying the script into a job step. I could see that getting missed if we ever modify the SP.

    Again, many thanks. You've been a great help.

  • You are welcome.

    Just remember that code of SP doesn't care about physical location of SP, only a job does. So if you put the USE YourDB_name... statement in a code of SP, the code will be running against YourDB_name database.

Viewing 6 posts - 1 through 5 (of 5 total)

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