Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a Stored Procedure?

I've been playing today with stored procedures. I'm stoked that I picked the basic of it up so easily. (I'll also try triggers very soon).

Now, I'd like to know how to save my SPROC under the stored procedure folder (Programmability -> Stored Procedure) so that I can access it by name (eventually from my C# application). So far, when I press the save icon, I'm proposed to save the whole query. That's not what I want.

Thanks for helping

like image 911
Richard77 Avatar asked Apr 19 '10 19:04

Richard77


People also ask

Where do stored procedures get saved?

You can find the stored procedure in the Object Explorer, under Programmability > Stored Procedures as shown in the following picture: Sometimes, you need to click the Refresh button to manually update the database objects in the Object Explorer.

Will stored procedure be saved in database?

A stored procedure can return multiple values using the OUT parameter, or return no value. A stored procedure saves the query compiling time. A stored procedure is a database object.

How are stored procedures stored?

A stored procedure is a set of Structured Query Language (SQL) statements with an assigned name, which are stored in a relational database management system (RDBMS) as a group, so it can be reused and shared by multiple programs.

How do I download a stored procedure in SQL Server?

Export Stored Procedure in SQL ServerIn the Object Explorer, right-click on your database. Select Tasks from the context menu that appears. Select the Generate Scripts command.


1 Answers

You actually have to run the CREATE PROCEDURE command so that the Stored Procedure builds.

Create Procedure - MSDN

Here's an example straight from the MSDN page:

USE AdventureWorks;
GO

-- If procedure exists already, drop it
IF OBJECT_ID ( 'HumanResources.uspGetAllEmployees', 'P' ) IS NOT NULL 
    DROP PROCEDURE HumanResources.uspGetAllEmployees;
GO

-- Create (or Re-create) the procedure
CREATE PROCEDURE HumanResources.uspGetAllEmployees
AS
    SET NOCOUNT ON;
    SELECT LastName, FirstName, JobTitle, Department
    FROM HumanResources.vEmployeeDepartment;
GO

Remember that after you create the Stored Procedure, you will have to Right Click -> Refresh the Stored Procedure folder for the new procedure to appear.

I would also suggest saving the *.sql file somewhere so you have the CREATE PROCEDURE script somewhere in case you need to run again.

like image 71
Justin Niessner Avatar answered Oct 20 '22 17:10

Justin Niessner