How to schedule a stored procedure in SQL Server 2012? - sql-server

I want to schedule a stored procedure once every night. I've followed steps online the outlines the this process:
SQL Sever Agent > Right click jobs & select New Job > Step > New > Select the stored procedure under type
But there is no option for stored procedure under the Type selection.

Just leave the Type drop down set to Transact-SQL script (T-SQL), and set the Command to EXEC [dbo].[YourStoredProcedureName], then click OK. Then go under Shedules and setup the time when you want it to run.

You should type this query in your job
EXEC [schema].[StoredProcedureName]

Related

SSRS Report Using Stored Procedure

I am working with an SSRS Report that uses a stored procedure.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with ALTER PROCEDURE ...
While I can understand the SQL in a stored procedure, I have never used one in an SSRS Report [I only use 'straight' SQL statements].
When I use SQL as my Dataset, I can copy that SQL into SSMS and run it and see the data it returns.
With this stored procedure, how do I execute it in SSMS to see the data it returns? The stored procedure has a sample 'EXEC ...' statement with all the parameters populated ... but when I run that - no data is returned.
The SSRS report runs fine, but I want to be able to work with the stored procedure in SSMS and see the data it is returning. My goal is to be able to run the stored procedure in SSMS and then tweak it.
How do I work with this stored procedure in SSMS so I can look at the output?
If you just want to execute the procedure in SSMS, locate it in the object browser ([DatabaseName]/Programmability/Stored Procedures). RIght-click the procedure and select 'Execute Stored Procedure'
Fill in the parameters and click OK and a script will be generated to run the procedure.
It's a bit overkill but at least everything is there and you can run it whenever you like.
If you want to edit the proc, right-click and choose modify, a new script will be created (the ALTER PROCEDURE script you mentioned). Make changes as required, run the script and that will modify the procedure, then execute the procedure to see the results.
Of course it would be safer to make a copy and edit that, you can also just run the body of the stored proc by commenting out the ALTER PROCEDURE statement until you are happy with it but you may have to declare and variables that are normally passed in as parameters.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with
ALTER PROCEDURE ...
That's the Alter Procedure script. Use this to edit a stored procedure.
In other words, edit the SQL code you want to optimize, then run the whole script to save the changes.
How do I work with this stored procedure in SSMS so I can look at the
output?
In SSMS use the syntax for stored procedures:
EXEC myspname paramter1, parameter2, param...
Where parameter1, parameter2, etc. are the parameters described in the ALTER Procedure script, directly after the ALTER PROCEDURE myspname. Parameters are preceded by the # symbol.
As you type-in the EXEC procedure command pop-up hints should appear describing the parameter.
Without knowing the code to the stored procedure, it could be doing any number of things based on what is passed to it by parameter. A stored procedure can do DDL and DML queries, and does not necessarily have to select anything at all for output.

Execute Stored Procedure with multiple result sets

I am using SSIS 2016. I need to execute a stored procedure that returns 4 result sets. I only need to keep the first result set and write this to a table. I can not modify the stored procedure. I do not care about any of the data returned in the other result sets. The stored procedure is in a SQL Server 2016 database. Results will also reside in SQL Server 2016.
I currently have this process running in SSIS 2008 using the "SQL Command" data access mode in an OLE DB Source like below. I have this in a For Each Loop Container to pass a series of param values to the stored procedure as I execute it multiple times for different param values on a daily basis.
SET FMTONLY OFF;
EXEC myProc
#Param1 = ?,
#Param2 =?,
#Param3 = ?;
By default SSIS 2008 is only returning the first result set, which has worked for me as I only care about the first result set.
I am using the Native OLEDB SQL Server client. From what I have read, it has changed the way it handles multiple result sets. I have used the WITH RESULT SETS to define the first result set but if I execute SSIS will fail indicating other result sets need to be defined.
In short, what is the best approach to duplicate what works in SSIS 2008 in SSIS 2016?
Solution Overview
I made 2 Experiments on that issue, the first experiments showed that in case of stored procedures with no parameters, nothing changed in SQL Server 2016 and SSIS 2016, the first Result Set is returned and others are ignored.
The second experiment showed that when using parameters, this will throw an exception, so you have to define metadata using WITH RESULT SETS option, then remove this option.
Detailed Solution
Experiment 1
The following experiment are made using SQL Server 2016 and Visual Studio 2015 with SSDT 2016
First i created this stored procedure
CREATE PROCEDURE sp_Test
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 10 PersonType,NameStyle,Title
FROM [AdventureWorks2016CTP3].[Person].[Person]
SELECT TOP 10 PersonType,Firstname,Lastname
FROM [AdventureWorks2016CTP3].[Person].[Person_json]
END
GO
Then i added a Data flow task to SSIS package
Added an OLEDB Source, Recordset destination
In OLEDB source i select the Data access mode to SQL command
an use the following commnad
EXEC sp_Test
When clicking on Columns Tab it shows the first ResultSet structure
And we i executed the package it runs succesfully
Experiment 2
I changed the stored procedures to the following:
ALTER PROCEDURE [dbo].[sp_Test]
#param1 varchar(10),
#param2 varchar(10),
#param3 varchar(10)
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 10 PersonType,NameStyle,Title ,#param2 as 'Param'
FROM [AdventureWorks2016CTP3].[Person].[Person]
SELECT TOP 10 PersonType,Firstname,Lastname,#param3 as 'Param'
FROM [AdventureWorks2016CTP3].[Person].[Person_json]
END
And i used the following SQL Command in the OLEDB Source:
EXEC sp_Test ?,?,?
WITH RESULT SETS (
(
PersonType NVarchar(10),
NameStyle NVarchar(10),
Title NVarchar(10),
Param Varchar(10)
)
)
And i mapped the parameters correctly.
When running the package it throws the following exception.
[OLE DB Source 2] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E14.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80040E14 Description: "EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), and the statement tried to send more result sets than this.".
After that i tried to remove the With RESULT SETS option, so the command is :
EXEC sp_Test ?,?,?
I tried to execute the package again, so it is executed with no errors.
Conclusion
Try to use the WITH RESULT SETs option to define the OLEDB Source metadata, after that the metadata is defined, just remove this option and run the package, so it will just take the first Result Set succesfully.
I know it's not exactly what you are asking for, but maybe you could create another stored proc that will return only the first result set for you? (Without touching the first stored proc.) Or insert the data into a table for you and then you just read the data?

Stored procedure with optional parameter that doesn't show in SQL Server Management Studio "execute stored procedure" or "script stored procedure"

In SQL Server Management Studio (SSMS), you can execute a stored procedure using the GUI for assistance. This can done by right-clicking on the stored procedure in the Object Explorer and selecting either "Execute Stored Procedure" or "Script Stored Procedure as > CREATE to > New Query Editor Window".
Both of these result in a pre-built SQL query to execute the SP, and both of them include optional parameters from the SP. Is there a way to make it so the optional parameters are "hidden"?
I have some user support folks who use SSMS to run certain SPs, and I don't want them providing values for certain optional parameters. I want to be able to provide them myself, if needed, when I run the SP, but not the user support people.
I've tagged SQL Server 2014 and 2008 R2 in case there's some option I can set in the SP itself.
You could wrap your stored procedure with another:
CREATE PROCEDURE dbo.my_orig_proc
#id INT
,#some_param_default INT = 10
AS
BEGIN
...
END
Wrapper:
CREATE PROCEDURE dbo.my_wrapper_proc
#id INT
AS
BEGIN
EXEC dbo.my_orig_proc #id;
END
I would also restrict access to orignal procedures if necessary.
Another way is to add check and don't allow specific user to override value:
CREATE PROCEDRUE dbo.my_orig_proc
#id INT,
,#some_param_default INT = 10
AS
BEGIN
IF USER_NAME() = 'user_name' AND #some_param_default <> 10
RAISERROR('You cannot change #some_param_default value' ,16,1);
END
Drawback: You need to change parameter value in two places and if user has impersonate privilige he still can execute it.

How do I pass parameter to set value in SQL Server agent job from stored procedure?

I am calling a SQL Server job from a stored procedure like below
CREATE PROC StartMyMonthlyInventoryJob
AS
EXEC msdb.dbo.sp_start_job N'_TIME_ACCESSABILITY_HOURS'
GO
and I want to change the set values for this job (I know how to configure in graphical way). How do I pass parameters to set value when I call that job from a stored procedure? Thanks
You don't.
Instead you create a table that holds all the parameters that the job would pass to the SSIS package.
Add a new first step to the package to read the parameters from the table and populate the package variables.
Add a new last step to the package to update the parameters-table and either delete the row, or update it with a "completed" flag of some kind.

sql server cannot create stored procedure sp_start_job

I have a job I need to launch from a SP. To do that I need a SP in my msdb system database called sp_start_job. When I try to execute this code in SSMS:
Use msdb
go
CREATE PROCEDURE [dbo].[sp_start_job]
[#job_name]
[,#error_flag ]
[,#server_name]
[,#step_name ]
[,#output_flag ]
I get this error:
Incorrect syntax near '#job_name'.
This is all from the SQL books on line. So what am I doing wrong?
You don't need to create the sp_start_job sproc, it should already exist in your msdb database.
You need to execute the stored procedure using the relevant parameters. For example:
EXEC msdb.dbo.sp_start_job
'Your Job Name'; -- Add any other arguments if you need them
See the MSDN documentation page for further info.

Resources