Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Create and then execute a stored procedure in a job step Expand / Collapse
Author
Message
Posted Friday, September 25, 2009 8:20 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, June 17, 2011 10:05 AM
Points: 85, 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
Post #793842
Posted Friday, September 25, 2009 8:28 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 12:52 PM
Points: 201, Visits: 389
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.
Post #793850
Posted Friday, September 25, 2009 8:41 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, June 17, 2011 10:05 AM
Points: 85, 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!
Post #793862
Posted Friday, September 25, 2009 8:58 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 12:52 PM
Points: 201, Visits: 389
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.:)
Post #793882
Posted Friday, September 25, 2009 9:03 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, June 17, 2011 10:05 AM
Points: 85, 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.
Post #793884
Posted Friday, September 25, 2009 9:19 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 12:52 PM
Points: 201, Visits: 389
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.
Post #793892
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse