Solved

SQL job calling stored procedure with a variable

Posted on 2013-01-14
6
628 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Make Temp Table Query Faster 5 40
export sql results to csv 6 34
SSIS with VPN COnnection 2 70
Need help how to find where my error is in UFD 6 26
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

785 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