SQL Server : weird Invalid object name - sql

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.

Related

Incorrect Syntax near the keyword 'OR' in CREATE OR ALTER PROCEDURE

I have a master script that runs and create all stored procedures, but when I run the script I error out at the only two places that use the syntax CREATE OR ALTER PROCEDURE
The code is below:
CREATE OR ALTER PROCEDURE [dbo].[P_ReinitializeStorePlans]
#storeId INT
AS
BEGIN
RETURN;
END
GO
And the error is as follows, I have another CREATE OR ALTER PROCEDURE later at line 2713 as well with the same error.
I have checked my ##VERSION which returns SQL 2016 as well as 130 for the compatibility version. Any idea why this is giving me an error? I am assuming its a compatibility setting or flag for the syntax?
I believe this one comes with SP1 installed, this has a version of
13.0.4001.0 KB3182545
https://support.microsoft.com/en-us/help/3177312/sql-server-2016-build-versions
I suggest you go straight to SP2 you can get it from here https://go.microsoft.com/fwlink/?linkid=869608

Why does the stored procedures not execute the rest of the code?

I have a stored procedure, which was written by another developer whi I have subsequently replaced. At the beginning of their store procedures they delete / drop the table using the following code:
EXEC DBA.usp_DeleteTableByName 'Customer_Import'
It appears though that the above line of the code is the only one, which gets executed as at the rest of the code within this stored procedure performs a SELECT INTO to recreate and populate the Customer_Import table.
Why would the stored procedure stop executing after the above line of code has been successfully executed?

SQL server Find/delete hidden trigger

So I was playing around with triggers and stored procedures.
For the life of me I cannot find or delete this trigger I setup.
This trigger now runs on any table i create. The message I get is
Msg 2812, Level 16, State 62, Procedure tr_test, Line 6 Could not find
stored procedure 'sp_test'.
I cannot find tr_test for the life of me. I try and drop the trigger and it says it doesn't exist or I don't have permissions. I am signed in with SA.
Msg 3701, Level 11, State 5, Line 4 Cannot drop the trigger 'tr_test',
because it does not exist or you do not have permission.
I've ran several queries to see if i can figure out where this trigger may be but none have returned any results.
select * from sysobjects where xtype = 'TR'
I've even restarted the server thinking it may be held in memory etc.
Any idea how I can find this hidden trigger.
I am guessing this in reference to your recent question about creating ddl triggers. You have to use a slightly different syntax to drop ddl triggers. https://msdn.microsoft.com/en-us/library/ms173497.aspx

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.