views:

94

answers:

4

Hello,

I've been playing today with stored procedures. I'm stoned 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

+2  A: 

you have to actually run the create proc statement.

Mladen Prajdic
@Mladen Prajdic: I've run it using EXEC command, and it worked. When I didn't see the SPROC under the Stored Procedure folder, I though I didn't create it. So I tried to create it again, but I was told that the object with the same name exists already. I guess I was able to create it. But, why it is not visible under the Programmability --> Stored Procedure folder?
Richard77
@Richard77, you need to hit the Refresh button to see it. Highlight the Stored Procedure folder, then click Refresh and you should see it. SQL Server Management Studio (and probably Server Explorer too) doesn't refresh automatically.
Kyralessa
+7  A: 

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.

Justin Niessner
+1 For the *.sql recommendation.
systempuntoout
@Justin Niesser (and to all): Thanks, it worked. However, I'm sorry I didn't really get your recommendation. I need to keep the CREATE PROCEDURE script somewhere in case I need to run what again? the stored procedure? Do I need to create it each time I need to use it?
Richard77
You don't need to create it each time you use it, but if you need to modify it or rebuild your database...it's nice to have backed up. You could also keep using the *.sql file to make changes and drop/recreate the procedure rather than running the ALTER PROCEUDRE command.
Justin Niessner
@Justin Nielssen: I see what you mean
Richard77
Also the .sql file should be in source control, so you can deploy it with the rest of your application changes.
HLGEM
A: 

You need to run CREATE PROCEDURE command.
Try with this simple Stored Procedure example:

CREATE PROCEDURE TestSP
AS
SELECT GETDATE() AS MyDate
GO
systempuntoout
A: 

Based on the OP's comment:

I've run it using EXEC command, and it worked. When I didn't see the SPROC under the Stored Procedure folder, I though I didn't create it. So I tried to create it again, but I was told that the object with the same name exists already. I guess I was able to create it. But, why it is not visible under the Programmability --> Stored Procedure folder?

When you run the stored procedure CREATE PROCEDURE.... for the first time, you may need to refresh your stored procedure list in SSMS. Right click then REFRESH.

KM