stored procedure, msg 2812, - sql

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

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.

What have i gotten wrong for this stored procedure not to execute?

Here is the stored procedure and how I created it
CREATE OR REPLACE STORED PROCEDURE SimpleSelect()
AS
BEGIN
SELECT * FROM Permissions
END;
/
CALL SimpleSelect();
When creating the procedure I get the error
"Success with some compilation errors"
and when running the CALL command I get that the SQL command not properly ended.
It is important to know which database do you use. Some of this advice's will come in handy in many databases but some will not...
Do you have a table called Permissions already created ? If not, create it.
Please put a ; after the SELECT statement. Like this:
SELECT * FROM Permissions;
In MySQL this will not return error:
CREATE PROCEDURE SimpleSelect()
BEGIN
SELECT * FROM Permissions;
END;
/
When you fix your procedure the call command will work just fine...
Cheers!
Here is the DEMO

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.

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.

create stored procedure if doesn't exist in sql server

Oracle does "create or replace" statements. Sql server does not seem to - if you are scripting out from Enterprise Manager, it instead suggests "drop and create" instead. Drop and create is undesirable in any situation where you've done grants on the stored procedure, because it tosses out any grants your database administration team has done. You really need "create or replace" to help with separation of conerns between developers and administrators.
What I've been doing recently is this:
use [myDatabase]
go
create procedure myProcedure as
begin
print 'placeholder'
end
go
alter procedure myProcedure as
begin
-- real sproc code here
end
go
This does what I want. If the procedure doesn't exist, create it then alter in the correct code. If the procedure does exist, the create fails and the alter updates the code with the new code.
It creates a different problem for the administrators, because the create throws a misleading error if the stored procedure already exists. Misleading, of course, in the fact that you shouldn't see red error text when the desired outcome has occured.
Does anyone have a way to suppress the red text? Everything I've tried leads to a 'CREATE/ALTER PROCEDURE must be the first statement in a query batch' error in some way or another.
This will work and keep the permissions intact:
use [myDatabase]
go
if object_id('dbo.myProcedure', 'p') is null
exec ('create procedure myProcedure as select 1')
go
alter procedure myProcedure as
SET NOCOUNT ON
-- real sproc code here. you don't really need BEGIN-END
go
Like this:
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[myProcedure]')
AND type in (N'P', N'PC'))
BEGIN
EXEC('
create procedure myProcedure as
begin
print ''placeholder''
end
')
END
EXEC('
alter procedure myProcedure as
begin
-- real sproc code here
end
')
NOTES:
remember to double up your quotes in the dynamic SQL strings.
I have indented it for readability, but that will also add the extra indent spaces to your actual procedures listings. If you don't wnat that, then just reduce the indentation level on the dynamic SQL text.
Finally the day is here where SQL Server has implemented an equivalent to Create or Replace. Their equivalent is "Create or Alter". This is available as of SQL Server 2016 SP1. Example usage:
use [myDatabase]
go
Create or Alter procedure myProcedure as
begin
-- procedure code here
end
go