Solved

SQL job calling stored procedure with a variable

Posted on 2013-01-14
6
631 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

749 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