SQL Server stored procedures, saving - sql-server-2008

I created a stored procedure and tested it.
The results were correct.
However, when I saved it, I do not see it in the folder with the other stored procedures that are on the server (under the Programmability folder)?

You may need to refresh the folder. Right click on the folder and click refresh.

You don't need to save a stored procedure, like a script, you need to execute the stored proc with a create statement at the top.
CREATE PROCEDURE GetAllData()
<define your SP here>
It will then show up in the stored proc folder. To make changes, you "alter" the stored proc and run it again.

Related

Shows Invalid Object Name in Stored Procedure

I am to very new to SQL Server and to the Stored Procedure. I tried to create a Stored Procedure for updating the table. I used the below statement in the stored procedure and executed it, worked fine
CREATE PROCEDURE [dbo].[usp_UpdateDB]
When I changed it to below code
ALTER PROCEDURE [dbo].[usp_UpdateDB]
It shows in the work area like this,
But when I execute the stored procedure, it shows
I even checked, the created usp_UpadteDB Stored Procedure exists under the Stored Procedures folder and did refreshes too. What could this be?
It got solved. I closed the SQL Server Management Studio and open again, checked the Stored Procedure. It is not showing Invalid object name any more.

stored procedure and scheduled job return different data

Has anyone come across an issue where a stored procedure is run manually returns the correct data, but when you run the stored procedure inside a scheduled job it returns a different set of data??
OK so i tried scheduling the job in SSIS to trigger the stored proc, tried using SSIS data flows to replace the stored procedure and even creating the process on another server with the same result every time!
In the end what i have done is create an blank ssrs report that executes a stored procedure for a data set, then set a subscription to automatically run as if it was a normal report. Which in turn appears to mirror running the stored proc manually. job Done.
Would still love to sort this one properly though!

Stored procedure gone missing in SQL Server 2008

I created a stored procedure which ran successfully but I can't seem to find the procedure in the usual place within the database and I can't execute the procedure in Excel but I can drop the procedure.
I am confused.
I have written many stored procedures and never had this problem. Thanks in advance.
Sounds like something I've done before: inadvertently added the stored proc to the master database rather than the database I thought I was working in.
Check the master database under System Databases.
To prevent this in the future, you may want to consider adding a use dbname statement in the script.

How do I save a stored procedure in SQL Server 2008 R2?

I am writing a SQL Server stored procedure for the first time and am unclear on how I can "save" my stored procedure so that it appears under Programmability, Stored Procedures in the Object tree.
The CREATE PROCEDURE procedureName statement creates the procedure.
You just need to execute it once and it will save the procedure to your database.
Make sure to select the correct database you want to save the procedure to, either by selecting it in the top left hand corner of SQL Server Management Studio, or by putting the following at the top of your code:
USE databaseName
Also note, if there are any syntax errors, it won't "save" the procedure.
While you are learning SQL Server and Management Studio, you may find it very helpful to become familiar with the built-in templates for creating everything from databases to tables to stored procedures and more. You locate the templates in Template Explorer under the View menu.
The first example in this walk-through with screenshots shows how to use the template for creating a stored procedure. That template includes a placeholder for the schema name (often just dbo).
You will also want to include a USE statement to make sure that the stored procedure is created in the correct database.
In addition to helping you to learn proper coding practice, using these templates can be a real time-saver and help you to avoid typos and syntax errors even after you becomem proficient in SQL.
And when you get really good at it, you can create your own templates.
Edit: Here is a very basic CREATE PROCEDURE statement:
USE MyDatabase
GO
CREATE PROCEDURE dbo.MyProcedure
AS
SELECT FirstName, LastName, Address, City
FROM Customers
ORDER BY LastName
GO
After you run that, you can run this line to check that the procedure has been created and that it is working correctly:
EXEC dbo.MyProcedure
you just use a create statement:
http://msdn.microsoft.com/en-us/library/aa258259(v=sql.80).aspx

MySQL Stored Procedure that Evaluates Whether Another Procedure Runs or Not

I am writing a stored procedure that needs to execute a .sql source file. The MySQL stored procedure must run the .sql script and provide a return value based on whether the .sql file exists, if an error occurs or if it exists and executes without errors. Can anyone provide me with some direction on the proper syntax to use in my stored procedure to call the .sql file? Is it simply CALL myfile.sql?
Thanks,
Sid
There are no commands in MySQL language to read a .sql file.
The "source" keyword is an internal command of the MySQL command line.
If you want to execute one SQL command from a file, you can do the following:
1) read the file into a table, using the LOAD DATA INFILE command (but some security restrictions apply);
2) load the first record from that file into a variable;
3) create a prepared statement from the variable;
4) execute the prepared statement.
The above procedure is fraught with restrictions and problems, however.
The first and most notable one is that allowing execution of generic code is a security liability. The second one is that you will have little control on what you are executing.
If you really must, you could use MySQL Proxy (http://launchpad.net/mysql-proxy), which can perform the above operations with much more control and flexibility on each step.