Solved

SQL job calling stored procedure with a variable

Posted on 2013-01-14
6
626 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 13

Assisted Solution

by:LIONKING
LIONKING earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Precisely the guidance I was looking for!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

744 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

15 Experts available now in Live!

Get 1:1 Help Now