When you execute a "Create Procedure" query, it is saved in the database at that time. You can save the code to a file if you want, but the only way to use a stored procedure is to create it and then you can run it.
Try this:
Open SQL Server Management Studio.
Connect to your user database.
Open a new query window.
Copy/paste the following code.
Code:
Create Procedure HelloWorld
AS
If DatePart(Hour,GetDate()) < 12
Select 'Good Morning'
Else If DatePart(Hour,GetDate()) < 5
Select 'Good Afternoon'
Else
Select 'Good Evening'
Now run the code. When running it, you will not see the results of the query, but the stored procedure will be saved to the database. To run the code, press the F5 key, or click "Query" -> Execute, or click the green triangle pointing to the right.
At this point, the query is saved. Now do this...
Click the "New Query" button.
Copy/paste the following code to the window.
Now run this code. You will see the results of the query (Good morning, Good afternoon, or Good Evening).
If you close the SQL Server Management Studio application and then open it again, you will be able to re-run the "Exec HelloWorld" procedure again without having to do anything extra.
In SQL Server Management Studio, you probably see an "Object Explorer" window. If you don't, click the View menu item, and then Object Explorer. Expand "Databases", Expand your user database, expand programability, expand "Stored Procedures". You should see the "Hello World" stored procedure.
You can expand the stored procedure to see the parameters. You can right click on the stored procedure modify, execute, delete, rename etc....
When you are done with this example, please do not forget to delete the HelloWorld stored procedure.
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom