Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Create and then execute a stored procedure in a job step


Create and then execute a stored procedure in a job step

Author
Message
kwilt
kwilt
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 394
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
Sergey Vavinskiy
Sergey Vavinskiy
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 392
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.
kwilt
kwilt
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 394
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!
Sergey Vavinskiy
Sergey Vavinskiy
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 392
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.Smile
kwilt
kwilt
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 394
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.
Sergey Vavinskiy
Sergey Vavinskiy
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 392
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search