[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL job calling stored procedure with a variable

Posted on 2013-01-14
6
Medium Priority
?
639 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 1400 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Assisted Solution

by:LIONKING
LIONKING earned 1400 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

656 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