Stored Procedure is not appearing in SSMS - sql-server-2005

In SQL Server Management Studio 2008, I created a Stored Procedure, but its not appearing in the 'Stored Procedures' list when viewing in Object Explorer.
I've also restarted the server, and restarted my machine, and tested on other machines, and the Stored Procedure still isn't appearing.
I know that this Stored Procedure exists in the system because if I execute the following query:
exec dbo.sp_Orders
I get the following error:
Msg 201, Level 16, State 4, Procedure sp_Orders, Line 0
Procedure or Function 'sp_Orders' expects parameter '#OrderID', which was not supplied.
Therefore its recognising that the Stored Procedure exists, and even returns an error about a Parameter which was not supplied.
Why is this happening and how can I access the sproc?

you can try followign to access your sp:
exec sp_helptext xxxx where xxxx is your sp name.

Related

SQL Server : weird Invalid object name

I am trying to create a stored procedure and I get this error:
Msg 208, Level 16, State 6, Procedure SP_MergeStagedPoliticalPartyAgents, Line 1 [Batch Start Line 0]
Invalid object name 'SP_MergeStagedPoliticalPartyAgents'.
SQL is as follows, minus the content
CREATE OR ALTER PROCEDURE SP_MergeStagedPoliticalPartyAgents
AS
BEGIN
-- Content removed for brevity
END
If I alter the name in anyway, i.e. adding an extra s, or removing the s at the end. It works 100% fine, so my question is what is it with this particular name that I am using that is causing it to fail?
Does SQL Server have a name validation regex that this name is violating? Is it a reserved name?
Weirder addition IMO: for the sake of testing if I go:
CREATE PROCEDURE SP_MergeStagedPoliticalPartyAgents
AS
BEGIN
-- Content removed for brevity
END
It runs fine and creates the stored procedure on top of that from then onwards the CREATE OR ALTER PROCEDURE SP_MergeStagedPoliticalPartyAgents statement works fine.
It seems as if a stored procedure with this name doesn't exist it will fail on the CREATE OR ALTER PROCEDURE SP_MergeStagedPoliticalPartyAgents but pass on the CREATE SP_MergeStagedPoliticalPartyAgents if it is being initialized for the first time.
Note:
I already have other stored procedures created in the same mannerism that are fine
It is not a permissions issue as I am able to create stored procedures fine.
Running the script via VS causes the same issue so it is not related to SMSS
It also isn't the content. If I replace the content with a basic select * from table it still has the issue.
Just after I posted the issue, I found the solution.
https://bornsql.ca/blog/remember-this-if-you-want-to-use-sp_/
Create or alter
You can quite easily create a stored procedure with the prefix, both in the master database and any user databases you have, using CREATE PROCEDURE. However what Erik shows is that if you already have a stored procedure with a name that has the sp_ prefix in the master database, and then use CREATE OR ALTER syntax to create a stored procedure in a user database with the same name, you’ll get an “Invalid object name” error in the user database:*
I found that someone had created these stored procedures in the master database also.
I've got the exact same issue. SQL 2019 (15.0.4053.23)
I have run the exact same stored procedure on another database with no issues, yet on this particular database it errors. Changing the name works, removing the ALTER and only using CREATE works. if I CREATE the procedure and then use CREATE OR ALTER, it works.
CREATE OR ALTER PROCEDURE dbo.sp_bt_annl_stmt_members_select
AS
SELECT * FROM sysusers
Msg 208, Level 16, State 6, Procedure sp_bt_annl_stmt_members_select,
Line 1 [Batch Start Line 0] Invalid object name
'dbo.sp_bt_annl_stmt_members_select'.
CREATE PROCEDURE dbo.sp_bt_annl_stmt_members_select
AS
SELECT * FROM sysusers
Commands completed successfully.
Running the CREATE OR ALTER again works
CREATE OR ALTER PROCEDURE dbo.sp_bt_annl_stmt_members_select
AS
SELECT * FROM sysusers
Commands completed successfully.

stored procedure, msg 2812,

I've made a database and I'm trying to make some stored procedures for it,
I added a new query file to my database and i wrote the following code in it,
create procedure SEL_STUDENT
as
begin
select * from student
end
execute SEL_STUDENT
go
But anytime i try to execute the following line
execute SEL_STUDENT
it returns an error, saying,
Msg 2812, Level 16, State 62, Line 53
Could not find stored procedure 'SEL_STUDENT'
Please help me fix it.
Thanks.
I ran your code using a table from the AdventureWorks2012 database.
create procedure SEL_STUDENT
as
begin
select * from [Person].[Person]
end
go
execute SEL_STUDENT
And it works fine.
Although I have moved GO above execute SEL_STUDENT that is not the issue here as #CoOl points out because you specifically say you execute the stored procedure after your block of code.
The only possible explanation would be that you are querying execute SEL_STUDENT on the wrong database.
Try the following code -
USE [DatabaseName]
GO
execute SEL_STUDENT
Here, [DatabaseName] is the database where the stored procedure SEL_STUDENT table is stored.
Make sure your table is also stored in the same database or else you would have to modify select * from student to select * from [DatabaseName].[SchemaName].[student]
Additionally, you can use the Object Explorer to identify where your stored procedure has been saved. I am unable to post a snapshot of how to do this as my reputation is below 10.
EDIT : Now that my reputation is in two digits -
Kindly note that [dbo] is the default schema in SQL Server.
Because you are trying to execute the Stored Procedure which is not there yet.
Move GO above execute SEL_STUDENT
create procedure SEL_STUDENT
as
begin
select * from student
end
go
execute SEL_STUDENT

Can't execute stored procedure

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.

SQL Server OpenQuery Fails with stored procedure

I've got a stored procedure which takes two parameters. I'm trying to use OpenQuery in SQL Server to join on the result set of my stored procedure dbo.TwoDrugs. The SQL Server is only a local machine with no one else having access. I'm trying to do
select *
from OpenQuery (AHCTW208D02,'exec [i 3 sci study].dbo.TwoDrugs ''X'',''Y''')
and I get the error
Msg 7357, Level 16, State 2, Line 1
Cannot process the object "exec [i 3 sci study].dbo.TwoDrugs 'X','Y'". The OLE DB provider "SQLNCLI10" for linked server "AHCTW208D02" indicates that either the object has no columns or the current user does not have permissions on that object.
I can't think of a reason why I wouldn't have permission (since I created the stored procedure), and I configured the server for data access.
When I run the stored procedure it does in fact return a result set. Any ideas on what might be the problem with this?
OPENQUERY cannot be used to execute extended stored procedures on a linked server. However, an extended stored procedure can be executed on a linked server by using a four-part name. For example:
exec AHCTW208D02.[i 3 sci study].dbo.TwoDrugs 'X','Y';
please read Remaks
Reference: http://msdn.microsoft.com/en-us/library/ms188427.aspx

Call update Stored procedure in SQL Linked server?

I have SQL server and It has another SQL server Linked server [DEV]. Now I want call a stored procedure in Linked server and get the result value. It is always giving error. I tried below both statements none of the did not worked.
ALTER PROCEDURE [dbo].[UpdateMemebership_Lock_Count]
#v_constit_id_in as varchar(100)
AS
BEGIN
Exec ('Call [Members_Directory_Search_DEV].[dbo].[update_dirsearch_notes]
(?)',#v_constit_id_in) AT [DEV]
--Exec [DEV]..[Members_Directory_Search_DEV].[update_dirsearch_notes]
#v_constit_id_in
END
Error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.
Not sure what syntax you are used to using (CALL is used for Analysis Services, not SQL Server), but linked servers have a four part notation:
ServerName.DatabaseName.owner.object
Try:
Exec [DEV].[Members_Directory_Search_DEV].dbo.[update_dirsearch_notes] #v_constit_id_in