Solved

SQL job calling stored procedure with a variable

Posted on 2013-01-14
6
627 Views
Last Modified: 2013-01-14
Hello-
I'd like to find a way to have a Job call a stored procedure with the Job providing a 'variable' to the SP dicatating where the SP updates its data.

I have SQLServer 2008 running on SERVER1. I have SQLExpress running on 3 Win7 workstations [WS1, WS2, WS3].  I have linked servers setup on SERVER1 that connect to the SQLExpress instances on each of the 3 workstations.  
An example instance name is WS1\SQLExpress

Each workstation has the same database on it [LOCAL_DB].  
SERVER1 hosts a SERVER_DB.  I need to update a table in the LOCAL_DB database(s) with data from the SERVER_DB database on a daily basis.

I have a Stored Procedure (named SP_update) in SERVER_DB that clears the LOCAL_DB table and then copies records from SERVER_DB to the LOCAL_DB.  The SP currently calls the specific instance name.

--Stored Procedure example:
BEGIN
SET NOCOUNT ON;

   DELETE [WS1\SQLEXPRESS].LOCAL_DB.dbo.Table1

   INSERT INTO [WS1\SQLEXPRESS].LOCAL_DB.dbo.Table1
   SELECT Field1
      ,Field2
      ,Field3
   FROM [SERVER_DB].[dbo].[Table1]

END
--End of Example


I'd like to be able to use 1 Stored Procedure to update all 3 of the workstation databases.
I'd like to be able to call the SP from a Job and provide a 'variable' that tells the SP which workstation to do the update on.

In other words, I'd call the SP from a job and provide a variable of 'WS1\SQLExpress' for the first iteration, so workstation 1 data gets updated.  Then run Job again and then provide a variable of 'WS2\SQLExpress' so workstation 2 data gets updated.  And so on.


--Job step code:
     exec SP_UPDATE
--End of Job step


How would I need to update my SP to do this?
And how to I structure my Stored Procedure call in the Job?

Thank you for your assistance.
0
Comment
Question by:JohnZeek
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 350 total points
ID: 38775048
In order to do that you'd need dynamic SQL.
You would create a variable in which you will store the SQL statement that you need to execute, add your "parameter" as part of the SQL and execute it.

It would resemble something like:

CREATE PROCEDURE UpdateData
@wsName VARCHAR(100)
BEGIN
SET NOCOUNT ON;

DECLARE @sql VARCHAR(2000)

SET @sql = 'DELETE [' + @wsName + '].LOCAL_DB.dbo.Table1'
EXEC(@sql)

SET @sql = 'INSERT INTO [' + @wsName + '].LOCAL_DB.dbo.Table1
   SELECT Field1
      ,Field2
      ,Field3
   FROM [SERVER_DB].[dbo].[Table1]'

EXEC(@sql)

END

Open in new window


You can then create your job with one step for each workstation. In each one you would perform an EXEC UpdateData 'WS1\SQLExpress' for example, and change it accordingly.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 38775126
I agree on above.
only difference I would make it to run 1 single @sql instead of 2, for transaction sake...

CREATE PROCEDURE UpdateData
@wsName VARCHAR(100)
BEGIN
SET NOCOUNT ON;

DECLARE @sql VARCHAR(2000)

SET @sql = '
  DELETE [' + @wsName + '].LOCAL_DB.dbo.Table1  

  INSERT INTO [' + @wsName + '].LOCAL_DB.dbo.Table1
   SELECT Field1
      ,Field2
      ,Field3
   FROM [SERVER_DB].[dbo].[Table1]'

EXEC(@sql)

END

Open in new window

0
 

Author Comment

by:JohnZeek
ID: 38775306
Thank you thus far.

The dynamic SQL works fine as long as I:
1. Add DECLARE in front of @wsName VARCHAR(100).  If don't do this, I get a Must declare the scalar variable "@wsName" error when compiling the SP.
2. Use a SET @wsName = 'WS1\SQLEXPRESS' inside the stored procedure.  If I don't SET the @wsName variable to the proper workstation/instance, the update doesn't happen.  Obviously, I don't want to set that variable in the SP, but from the Job.  I only did this for testing.

It doesn't appear that I am properly populating the @wsName variable from the Job.
My job step syntax is currently:
declare @wsName varchar (100)
set @wsName = WS1\SQLEXPRESS'
exec UpdateData


Do I need to declare @wsName inside the SP code?
What is missing from my Job step code?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 13

Assisted Solution

by:LIONKING
LIONKING earned 350 total points
ID: 38775377
If you put DECLARE before @wsName, you're declaring it as a variable within the procedure, which is not the intended behavior.

@wsName must be a parameter for the procedure, that way it will work with the statement that angelIII and myself posted.
0
 

Author Comment

by:JohnZeek
ID: 38775651
Success!  It is working now.  I've found the remaining issues:
1.  I had the @wsName VARCHAR(100) line on the wrong line in the SP.  I needed to move it to just above the AS and BEGIN lines.  No trouble compiling the SP now.
2.  I had to add @wsName to the end of the exec UpdateData line in the Job step. Correct syntax is:  exec UpdateData @wsName

Thank you both!
0
 

Author Closing Comment

by:JohnZeek
ID: 38775670
Precisely the guidance I was looking for!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now