Can't execute stored procedure - sql

I have created a stored procedure and can see it under the stored procedure node, but when trying to execute it doesn't find the procedure.
Under stored procedure node it is called dbo.CopyTable
exec CopyTable
CopyTable is undefined in red saying it does not exist. Why?
Even if I right-click on the procedure and say script stored procedure as execute to - the code it generates is underlined in red and cant find stored procedure either.

Ensure that the database selected contains the stored procedure CopyTable
USE YourDatabase
EXEC CopyTable

Try adding dbo and selecting the right database,
USE databaseName
GO
EXEC dbo.CopyTable
GO
Execute a Stored Procedure

Most likely you are just in the wrong database in the query window, you can specify the database like this:
EXEC [yourDBName].dbo.CopyTable
Reading on how to Execute a Stored Procedure
Considering your updated question:
Even if i rightclick on the procedure and say script stored procedure
as execute to - the code it generates is underlined in red and cant
find stored procedure either.
This could happen if your stored procedure is invalid. Please double-check the validity of the SPROC and ensure the tables it references exist, etc.

Try running your CREATE PROCEDURE. Highlight it, f5 it, and then make sure it runs before you call it elsewhere.

Maybe in your procedure you've accidentally cut-pasted your script name (dbo.CopyTable), say something like...
SELECT * FROM dbo.CopyTable
WHERE ClientId = #ClientId
RETURN
Then when you call your proc you get 'invalid object name dbo.CopyTable' and assume sql is having trouble finding the stored-proc ... which isn't the problem, its finding and running the proc but its actually a problem within the proc.

Related

Using MSDB stored procedures in application's database stored procedure

This seems like it would be trivial, but I have not been able to come up with a solution to this small problem.
I am attempting to create a stored procedure in my application's database. This stored procedure just executes a job that has been set up in the SSMS on the same server (seemed to be the only way to programmatically execute these jobs).
The simple code is shown below:
USE ApplicationsDatabase
GO
CREATE PROCEDURE [dbo].[procedure]
AS
BEGIN
EXEC dbo.sp_start_job N'Nightly Download'
END
When ran as is, the procedure technically gets created but cannot be executed due to it not being able to find the 'sp_start_job' since it is using the ApplicationsDatabase. If I try to create the procedure again (after deleting previously created) but updating the USE to MSDB, it tries to add it to that system database for which I do not have permissions to do. Finally, I attempted to keep the original create statement but added the USE MSDB within the procedure (just to use the 'sp_start_job' procedure), but it would error saying USE statements cannot be placed within procedures.
After pondering on the issue for a little (I'm obviously no SQL database expert), I could not come up with a solution and decided to solicit the advice of my peers. Any help would be greatly appreciated, thanks!
You will have to fully qualify the path to the procedure. Of course, you can only execute this is the application has permissions.
Try this:
USE ApplicationsDatabase
GO
CREATE PROCEDURE [dbo].[procedure]
AS
BEGIN
EXEC msdb.dbo.sp_start_job N'Nightly Download'
END

What does an EXEC command do in SQL

I am presently updating a procedure with multiple EXEC lines such as:
EXEC databasename.tablename.pr_sys_drop_Object 'zt_Staging_of_class'
yet nowhere have I found the definition of EXEC in this context.
If it's a full three part name where the middle part is not the name of a table but of a schema a kind of SQL namespace. So in that context pr_sys_drop_Object is a stored procedure in a separate schema.
If you look in the named database, in the named schema you'll probably find a stored procedure called pr_sys_drop_Object.
Execute is a sql server keyword see the docs here for more details. It is used to execute stored procedures or raw sql.
In your case it seems to be executing the procedure databasename.tablename.pr_sys_drop_Object and passing in 'zt_Staging_of_class' as a parameter to that procedure.

Executing Stored Proc in Pervasive Control Center

I am relatively new to Pervasive Control Center and I was wondering if I wanted to test a stored Procedure to see its results, how would I simply select that stored proc? I have:
Select SP_test_getMeasure06
I am sure I am missing something because I know this is legal my syntax must be off slightly.
Thanks in advance!
You can execute a stored procedure using either Call or Exec. For example:
exec SP_test_getMeasure06
You'll need to make sure your stored procedure uses the RETURNS clause to get data back. For more information check out the Stored Procedure docs.

Does modifying a stored procedure just update the stored procedure definition?

I need to modify a stored procedure, and I wanted to get some insight on what "modifying" a stored procedure actually does. I have a stored procedure, and in it is a statement like:
ALTER PROCEDURE [dbo].[get_orders]
INSERT INTO customer (id, date, name)
VALUES(#id, getdate(), #name)
SELECT
full_id,
fname,
lname,
...
FROM orders
If I modify this stored procedure (right click the sp in SSMS, and select "Modify") by adding a column to the Select statement for example, and then click "Execute" (or press F5), will this just update the stored procedure definition, or will it also "run" the code in it, for example, run the "INSERT" statement (or if there was a "DELETE") and actually do some inserting (or deleting)?
I'm assuming that it will just update the stored procedure, and not actually run the queries in it, but I just want to be sure. Sorry if this question seems basic, but I couldn't easily find an answer.
Thanks in advance!
When you alter the definition of the stored procedure that is all that you're doing — altering the definition/code inside said procedure. It is not going to execute the procedure, to do so you'll need to exec your procedure after it has been altered.
if you execute on that statement it should only store the procedure changes.
When you find your procedure in the explorer then hit execute it will then run the procedure.

How to execute multiple stored procedures at a time?

I want to know that is there any way through which I can execute all my stored procedure at a time.
Presently I am executing each stored procedure using exec "Stored Procedure name" command.
And I have more than 200 stored procedure to be executed on my database.
Is there any easy way out to execute all these stored procedure at a single time as it is difficult for me to keep a track of them?
I suggest you write a stored procedure which calls the other ones.
Put all stored procedures inside a stored procedure,
CREATE PROCEDURE CallAllProcedure
AS
BEGIN
CALL Proc1
CALL Proc2
END
Assuming that you are using Query Analyzer, just put a GO in between all those stored proc and run script.
If you want to execute them all in parallel you could create a SQLJob and schedule them all to execute at the same time. Link below is general usage of SQL Jobs.
http://msdn.microsoft.com/en-us/library/ms190268.aspx
you can select all stored procedure names from sys.objects table querying type='P'. After you can use cursor for every stored procedure name to execute. But how about stored procedures with parameters? you must provide parameter values as well to avoid from errors.
You can use Service broker to do this async but I dont think it is a great idea to run 200 stored procs at the same time unless you are sure there will not be any contention on the DB